MSKKernel – Logging

One of the features of the MSKKernel is the Logging-feature. This feature gives you the possibility to log information from your application into different targets, like Transcript, files or memory.

LogManager

The main class is MSKLogManager.The user will in general use several class methods like e.g. DefaultDebug:, DefaultError:, DefaultLog: or DefaultWarning:.

LogMessages
Log-Messages are defined my subclasses of MSKGeneralLogMessage. Their main attributes are the text and the “log level” – a integer number from 0 to 100. As an example a “normal” log message has the level 5, an error message the level 15, a warning message the level 10.

Some might see the “log level” as an information channel number, which is ok.

LogConnector
The target for all the log messages are instances of subclasses of MSKGeneralLogConnector. A connector must have at least a single “log level”, or several distinct “log levels”, one or more ranges of “log levels”. The log levels (-ranges) are defined by instances of MSKLogLevel.
Each connector has its own execution thread with userBackgroundPriority. The logging is therefore asynchronous to the running application. The output at the target location (Transcript or file) is done in FIFO-order.

As targets (instances of connectors) you might have the Transcript-Window, InMemory or file destinations.

Examples

The first example shows a creation of a Transcript-Connector and some output to it:

	| aMSKLogManager |

	aMSKLogManager := MSKLogManager new initialize.

	aMSKLogManager
		addConnector: (MSKTranscriptLogConnector logLevelWithName: 'Transcript').

	1 to: 10 do: [ :anIndex |
	  aMSKLogManager log: 'Test: ', anIndex asString ].

	aMSKLogManager terminateAll

and you might see an output like this in your Transcript window:

10.06.2008 12:42:17 L: Test: 1
10.06.2008 12:42:17 L: Test: 2
10.06.2008 12:42:17 L: Test: 3
10.06.2008 12:42:17 L: Test: 4
10.06.2008 12:42:17 L: Test: 5
10.06.2008 12:42:17 L: Test: 6
10.06.2008 12:42:17 L: Test: 7
10.06.2008 12:42:17 L: Test: 8
10.06.2008 12:42:17 L: Test: 9
10.06.2008 12:42:17 L: Test: 10

The “L:” gives you an info, that its a normal “L”og message. Another possibilities are “W:” or “E:” for “Warning” or “Error”. The code adds a Transcript-Connector to the LogManager. This connector has the normal log-level (in contrast to #errorLevelWithName: or #warnLevelWithName:).

In the last example a specific LogManager was created. You might also work with the default instance and the source code for that is:

	MSKLogManager Default
		addConnector: (MSKTranscriptLogConnector logLevelWithName: 'Transcript').

	1 to: 10 do: [ :anIndex |
	  MSKLogManager DefaultLog: 'Test: ', anIndex asString ].

	MSKLogManager ClearDefault

You may work with several connectors as the following source code suggests:

	| aMSKLogManager |

	aMSKLogManager := MSKLogManager new initialize.

	aMSKLogManager
		addConnector: (MSKTranscriptLogConnector logLevelWithName: 'Transcript') ;
		addConnector: (MSKTranscriptLogConnector errorLevelWithName: 'Transcript-2').

	aMSKLogManager log: 'Test Log'.
	aMSKLogManager log: 'Test Log2'.
	aMSKLogManager warning: 'Test Warning'.
	aMSKLogManager error: 'Test Error'.
	aMSKLogManager log: 'Test Log3'.

	aMSKLogManager terminateAll

which produces the follwoing output:

10.06.2008 14:59:46 E: Test Error
10.06.2008 14:59:46 L: Test Log
10.06.2008 14:59:46 L: Test Log2
10.06.2008 14:59:46 L: Test Log3

The communication between the LogManager and connectors are done only through SharedQueues.

If a connector is added to the LogManager via #addConnector: the connector is automatically started and is ready for working.

A connector should always be given a name to make it easier to find it again. If you know the name of the connector you may ask the instance of LogManager via #connectorNamed: to get the special connector.

By sending #terminalAll to the instance of MSKLogManager all connectors are told to shut down. This may take some time, but after that call the MSKLogManager instance should not be used any more.

A single connector can be shut down, by removing it from the instance of MSKLogManager via #removeConnector:.

The default instance of MSKLogManager can be handled by various methods on the class side of MSKLogManager.

To make the message more interesting you can set some attributes at the level of MSKLogManager. This is “computerName”, “userName” and “applicationName”.

The format of the log message can be configured by a template string, where %? is a placeholder for $1 … $9. The message can be

  • 1: contents of the original error message
  • 2: shortcut character “L”, “E”, “W”
  • 3: sourceLevel1
  • 4: sourceLevel2
  • 5: computerName
  • 6: userName
  • 7: applicationName
  • 8: date
  • 9: time

The original template string is '%8 %9 %2: %1'. The format can be different for each connector – you may set it via #messageTemplate:.

This entry was posted in Smalltalk and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s