My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Logging  

Featured
Updated Mar 28, 2011 by maxkugland@gmail.com

To get logging up and running we firstly need to create and configure a LoggerFactory. The LoggerFactory takes two constructor parameters, the id of the factory and an optional IOutputFormatter which formats each log message before it is sent to the outputs. By default a DefaultOutputFormatter is used which formats the log messages in the following way:

factory-id timestamp - classname.methodname [linenumber] message-severity  message 

Note that if the application doesn't run in a debug player the line number of the log message can't be determined and therefore always stays 0.

Via set range it is possible to restrict the output of log messages by their severity. In this example each log message with a severity level between LogLevel.TRACE and LogLevel.FATAL is output. The set basePackage method helps to further restrict which log messages are output. Only logs which originate within or beneath the specified base package are sent to the outputs.

Next we need to add some ILogOutput implementations in order to send the log messages to various destinations. splinklibrary provides four implementations:

  • TraceOutput which traces the log messages
  • QLogOutput which sends the log messages via socket connection to the Qlog output console qlog download
  • FirebugOutput sends the log messages to the firebug output console firebug download
  • SosOutput sends the log messages to the sos output console sos download

Finally we add the configured LoggerFactory to the static LoggerProvider. Now we can obtain an ILogger for the current class:

LoggerProvider.getLogger(factory-id, Class); 

We provide the id of the ILoggerFactory we wish to use as first argument and the class in which we are using the ILogger as second argument. Eventually we can send log messages:

_logger.log(severity, message);

or use the shortcut methods:

_logger.trace(message);
_logger.info(message);
_logger.debug(message);
_logger.warn(message);
_logger.error(message);
_logger.fatal(message);

Here is the complete code:

public class App extends Sprite {
	private static var _logger : ILogger;

	private function configureLogger() : void {
		var factory : ILoggerFactory = new LoggerFactory("splink");
		factory.range = new LogRange(LogLevel.TRACE, LogLevel.FATAL);
                factory.basePackage = "org.splink";
				
		factory.addLogOutput(new TraceOutput()); 
		factory.addLogOutput(new QLogOutput());
		factory.addLogOutput(new FirebugOutput());
		factory.addLogOutput(new SosOutput());
		
		LoggerProvider.addLoggerFactory(factory);
		_logger = LoggerProvider.getLogger("splink", Test);
	}
	
	public function App() {
		configureLogger();
		
		_logger.trace("trace");
		_logger.info("info");
		_logger.debug("debug");
		_logger.warn("warn");
		_logger.error("error");
                _logger.fatal("fatal");
                // alternatively:
		_logger.log(LogLevel.TRACE, "trace");
	}	
}

Now we can obtain an ILogger in any of our application's classes through LoggerProvider. Note that the logger instance we obtain should be static. This avoids the creation of unneccessary ILogger instances per class instance. So each object factored from a class only uses the one ILogger which is stored in a private static field.

public class Somewhere {
	private static const _logger:ILogger = LoggerProvider.getLogger("splink", Somewhere);
	
	public function Somewhere() {
		_logger.trace("hi!");
	}
}

Sign in to add a comment
Powered by Google Project Hosting