When adding logging to your application, it is important that you log the right information. Otherwise you have no use of your log when examining it afterwards. In
Microlog you have the possibility to format the logging output. The formatting is not formatting the log message, but rather you add information like the time when the logging was done.
The formatting in Microlog is done using a an instance of a
Formatter
. There are three formatters available out-of-the-box when downloading Microlog:
SimpleFormatter
ConfigurableFormatter
PatternFormatter
The
SimpleFormatter
is the default formatter for Microlog. Is is the smallest and fastest formatter. It prints the time, the level, the message and the
Throwable
object if there is one available. This is always a good starting point when using Microlog for the first time. You could set the formatter in code like this:
ConsoleAppender appender = new ConsoleAppender();
SimpleFormatter formatter = new SimpleFormatter();
appender.setFormatter(formatter);
The final string looks like this:
494:[INFO]-Starting app
The
ConfigurableFormatter
gives you a little bit more control of the formatting. The order is pre-defined, but you get to choose what to log. You get to choose if you want to log the level, message and the name of the logger. You could set the time format and specify which delimiter that is used between each printed field. If you choose to print the time as date it looks like this:
2009-02-10 19:38:00.188:[INFO]:Starting app
The available properties are:
- PrintName - Set to true if you wish to print the name of the Logger instance.
- PrintLevel - Set to true if you wish to print the current Level.
- TimeFormat - Specifies which time format to use. The possible values are:
NO_TIME
, DATE_TO_STRING
and TIME_IN_MILLIS
. The DATE_TO_STRING
uses the toString()
of the current Date
object (now). The TIME_IN_MILLIS
writes the time in milliseconds. - PrintMessage - Set to true if you wish to print the logged message.
- Delimiter - Set the delimiter to separate each printed part. Use a
String
of your choice.
The
PatternFormatter
is the Formatter with the most opportunities. It works by defining your own formatting pattern. The pattern could be set in your code or by a property in a properties file. For example it could look like this in your property file:
microlog.formatter=net.sf.microlog.format.PatternFormatter
microlog.formatter.PatternFormatter.pattern=%d [%P] %m %T
The formatted string look like this:
15:38:49,199 [INFO] Starting app
The available formatting options are:
- %c - prints the name of the Logger
- %d - prints the date (absolute time)
- %m - prints the logged message
- %P -prints the priority, i.e. Level of the message.
- %r - prints the relative time of the logging. (The first logging is done at time 0.)
- %t - prints the thread name.
- %T - prints the Throwable object.
- %% - prints the '%' sign.
If you are not satisfied with the supplied Formatter implementations, you could create your own Formatter. This is done by creating a class that implements the Formatter interface. The interface looks like this:
public interface Formatter {
/**
* Format the given message and the Throwable object.
*
* @param name
* the name of the logger.
* @param time
* the time since the first logging has done (in milliseconds).
* @param level
* the logging level
* @param message
* the message
* @param t
* the exception.
* @return a String that is not null.
*/
String format(String name, long time, Level level, Object message,
Throwable t);
/**
* Configure the appender.
*
* @param properties
* Properties to configure with
*/
void configure(PropertiesGetter properties);
}
The
format()
method is where the actual formatting is done. It creates a
String
object that is the actual
String
that is logged. Nothing more, nothing less. The
configure()
method is used for configuration of the Formatter. The
PropertiesGetter
object is used for fetching the properties that the Formatter is interested in. Thus you could define your own properties that you use for your Formatter implementation.
I hope that you have learned how to master the art of formatting in Microlog by reading this. If you still have any questions about this, please use the official Microlog forums to find your answers.