Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1.Log different levels of messages (MessageLevel?) using enums. a.Examples: Proc

ID: 3661723 • Letter: 1

Question

1.Log different levels of messages (MessageLevel?) using enums.

a.Examples: Process, Info, Warning, Error, Fatal Error, Others?

2.Log messages containing variable data

a.Example: Log(Error, “Unable to open file [%s] for reading. ”, filename);

b.Output: Unable to open file [config.txt] for reading.

3.Log messages containing up to four variables (& easily extendable for more!)

a.Example: Log(Info, “%d players with average level %f killed %f%% of the monsters in %s dungeon. ”, numPlayers, avgPlayerLevel, percentMobsKilled, dungeonName);

b.Output: 3 players with average level 49.5 killed 12% of the monsters in OOP dungeon.

4.Initially log to System.out, but eventually log to a file.

a.Make a private method (WriteLog?) that initially logs to System.out and is used by all the logging methods. This will minimize where changes must be made and is good practice. Eventually that method will be modified to write to a file instead.

b.Make sure each log message only calls this message once. That way the method could perhaps timestamp the messages. How can you do that?

i.Called Once:     2016.May.03 04:12:17.12 Error: Ack!

ii.Called Twice:    2016.May.03 04:12:17.12 Error: 2016.May.03 04:12:17.13 Ack!

5Write defensive code! Test for and properly handle exceptions. Protect yourself from yourself!

a. What happens if you pass a float but your message uses “%d” ?

b. What happens if you pass a float but your message uses “%f -- %f” ?

c. What happens if you pass two floats but your message uses “%f” ?

d. What happens if you add a new MessageLevel to the enum, but you don’t change the method that converts each MessageLevel to a string?

that is what I have so far

Explanation / Answer

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.IllegalFormatConversionException;
import java.util.MissingFormatArgumentException;

import com.sun.jmx.snmp.Timestamp;

enum MessageLevel{Info, FatalError, Warning, Error,NewLevel }
public class Logger {
   static BufferedWriter bw;
   static{
String logFileName ="LogFile.log";
   File file = new File(logFileName);
   FileWriter fw = null;
       try {
           fw = new FileWriter(file.getAbsoluteFile());
       } catch (IOException e) {
           e.printStackTrace();
       }
       bw = new BufferedWriter(fw);
   }
public void WriteLog(String msg) {
java.util.Date date = new java.util.Date();
System.out.print(msg);
System.out.print(new Timestamp(date.getTime()));
try {
           bw.write(msg);
           bw.write(new Timestamp(date.getTime()).toString());
           bw.flush();
       } catch (IOException e) {
           e.printStackTrace();
       }
System.out.print(new Timestamp(date.getTime()));
}
public String GetMsgLevelString(MessageLevel level) {
switch (level) {
case Info:
return "Info: ";
case FatalError:
return "Fatal Error: ";
case Warning:
return "Warning: ";
case Error:
return "Error: ";
}
return "Programmer ERROR: No message";
}

public void Log(MessageLevel level, String msg) {
Logger log = new Logger();
log.WriteLog((String.format(log.GetMsgLevelString(level), msg)));
}

public void Log(MessageLevel level, String msg, Object... value) {
Logger logger = new Logger();
try {
logger.WriteLog(String.format(msg, value) + GetMsgLevelString(level));
} catch (IllegalFormatConversionException e) {
logger.WriteLog(String.format("Programmer Error: Bad Format ")+e.getMessage());
} catch (MissingFormatArgumentException e) {
logger.WriteLog(String.format("Programmer Error: Missing ")+e.getMessage());
}
}


}