Usage
The use of as3commons-logging is simple and straightforward, when in
need of a logger use
the
LoggerFactory
class to get one, e.g.
var logger:ILogger = LoggerFactory.getLogger("myLogger");
logger.info("Some logging message");
...
If the logger is specific to a class it's a good practice to use the
specific method
getClassLogger(clazz:Class)
:
var logger:ILogger = LoggerFactory.getClassLogger(MyClass);
logger.info("Some logging message");
...
returning a logger whose name is the full class name of the passed
class.
The default ILogger returned by LoggerFactory
is simply using trace()
to log messages. In order to use a different logger a different
ILoggerFactory
should be set in the LoggerFactory class:
... // Some main class method, mx:Application init for example
LoggerFactory.loggerFactory = new FlexLoggerFactory(); // Setting flex logger factory as current logger factory
once done this every further call to get a logger, anywhere in the code,
it will return the logger created by this logger factory:
... // Some other class
var logger:ILogger = LoggerFactory.getClassLogger(MyClass);
logger.info("Some logging message"); // using the flex logger here!
The ILogger interface provides
the following logging levels:
- debug
- info
- warn
- error
- fatal
All
ILogger log methods accept variable arguments usually taken to
format message string, e.g.
var logger:ILogger = LoggerFactory.getClassLogger(MyClass);
logger.info("Some logging message from {0}", "myApp");
// Default logger will trace:
// my.package.MyClass 12:10:34 [INFO] Some logging message from myApp
Implementing a logger
Adapting a logging framework to be used by as3commons-logging is very easy: just
provide an implementation of the two main API interfaces:
ILoggerFactory
and
ILogger
using the targeted logging framework.
Implementing variable arguments replacement in log messages is not compulsory but
considered a good practice in order to provide more transparent logging framework
switching.