Module 03 Lab Assignment – Contrast SystemsYou will add a .java file containing the logging code to your project and then in
your own code, you can call one of the logging methods to record whatever
information you think is useful to a file. You then use the appropriate utility
program to pull that file back to your development PC where you can examine it.
Remember there are many ways to develop logs. It is recommended either for you
to use the JAVA built-in logger utility or the one is straight into Eclipse.
Here is an example of code using a logger
We can create java Logger with very simple code as;
Logger logger = Logger.getLogger(MyClass.class.getName());
We can set the logger level using following code. Java has several different levels –
logs will be developed for the level stated and higher. In our example we us
WARNING – so logs would be developed for warning and severe.
logger.setLevel(Level.WARNING);
1.
2.
3.
4.
5.
6.
7.
SEVERE (highest)
WARNING
INFO
CONFIG
FINE
FINER
FINEST
You would then want to add handlers to a java logger – this way any log message
will processed it accordingly. There are two default handlers provided by Java
Logging API.
•
•
ConsoleHandler: This handler writes all the logging messages to console
FileHandler: This handler writes all the logging messages to file in the XML
format.
You will then want to add formatters are used to format the log messages. There
are two available formatters in java logging API.
1. SimpleFormatter: This formatter generates text messages using the Console
Handler to print a log message to the screen.
2. XMLFormatter: This formatter generates XML message for the log,
FileHandler uses XMLFormatter as a default formatter.
java.util.logging.LogManager is the class that reads the logging configuration, create
and maintains the logger instances. We can use this class to set our own application
specific configuration.