Logging howto
From Wiki@comjat.net
Contents |
Introduction
This howto guides you step-by-step on how to use the logger. You might first want to read about Logging for the theory. In this howto just the most basic flavour of the log system is introduced. There are different flavours already implemented, also an event driven logger (each message being logged results in en event which you may handle).
Author
Siegfried Steiner User:steiner
Logging howto
Important: For the example you need the COMJAT.NET Libraries found here. Get the Log example source code here.
Let's take a look on how to create the most basic logger:
public static void main( String[] argv ) {
BlueprintLogMaster logWriterMaster =
new LogWriterMaster( System.out, true );
logWriterMaster.setLogLevel( logWriterMaster.LOG_LEVEL_2_WARNING );
logWriterMaster.setLogSource( LogExample.class.getName() );
...
}
Figure 3: Instantiate the logger
As you see we created an object of type BlueprintLogMaster. What is such a master? If there is a master, then there must be a slave? Indeed, there is s slave. Why do we have a master and a slave? The master is used to setup the logger, the slave is just for logging. Actually, the master is the private part of the logger and the slave is the public part of the logger.
As you usually create exactly on instance of the logger in your applications and pass it to all subsystems which log any messages, it is important that nobody fusses around with your logger settings! You probably do not know what a subsystem might do with your logger! Then the output of your logger may look funny, and you cannot intrude!
Therefore the logger has a public part, which gets its configuration settings from the master. Only the system having access to the master can fuss around with the settings, i.e. on how the messages are to be logged and which messages are to be logged.
The public part of the logger (the slave) is just there for logging! No critical methods are accessible here.
Ok, you saw how the master is being created ? now let us take a look at the slave: In the main method above we created the logger and now we pass it to our subsystems ? in this case an instance of the example class:
<!--StartFragment -->... new LogExample( logWriterMaster.getLogSlave() ); ...
Figure 4: The public part of the logger to be safely used by other systems for logging
Using the 'getLogSlave()' method you retrieve the public part of the logger. In the constructor of our class we now store the logger in a global variable and use it for some experimenting:
private BlueprintLogPublic _logPublic;
public LogExample( BlueprintLogPublic aLogPublic ) {
_logPublic = aLogPublic;
_logPublic.log( "$packageName$", "$className$", "$methodName$",
LogValues.LOG_LEVEL_5_DEBUG,
LogValues.LOG_TYPE_RUNTIME,
"Log-Level = LOG_LEVEL_5_DEBUG, Log-Type = LOG_TYPE_RUNTIME" );
_logPublic.log( "$packageName$", "$className$", "$methodName$",
LogValues.LOG_LEVEL_1_ERROR,
LogValues.LOG_TYPE_NETWORK,
"Log-Level = LOG_LEVEL_1_ERROR, Log-Type = LOG_TYPE_NETWORK" );
_logPublic.log( "$packageName$", "$className$", "$methodName$",
LogValues.LOG_LEVEL_3_STATUS,
"myOwnLogType",
"Log-Level = LOG_LEVEL_3_STATUS, Log-Type = \"myOwnLogType\"" );
}
Figure 5: Some logging
Running this example you see only one message being logged. We have already done basic filtering. Adjust the settings of the master in the main method to get all log messages! Do it by using 'setLogLevel()' ? you'll find the possible pre-defined values in the interface 'LogValues'.
