LogManager
is the core access point of the As2lib Logging API.
You use it to set the underlying repository that stores and releases loggers and to obtain a logger according to a logger's name of the repository.
The repository must be set before anything else when you are using this class as access point to obtain loggers. There is no default repository. This means that all messages sent to loggers obtained from the getLogger method, before the repository has been set will not be logged.
This class could be used as follows with a non-singleton repository. Note
that you can of course also use any other kind of logger repository.
// configuration: when setting everything up
var loggerHierarchy:LoggerHierarchy = new LoggerHierarchy();
var traceLogger:SimpleHierarchicalLogger = new SimpleHierarchicalLogger("org.mydomain");
traceLogger.addHandler(new TraceHandler());
loggerHierarchy.addLogger(traceLogger);
LogManager.setLoggerRepository(loggerHierarchy);
// usage: in the class org.mydomain.MyClass
var myLogger:Logger = LogManager.getLogger("org.mydomain.MyClass");
if (myLogger.isInfoEnabled()) {
myLogger.info("This is an informative log message.");
}
If you have one logger that shall always be returned you can use the
convenience method setLogger that does all the work with the repository
for you.
// configuration: when setting everything up
var traceLogger:SimpleLogger = new SimpleLogger();
traceLogger.addHandler(new TraceHandler());
LogManager.setLogger(traceLogger);
// usage: in the class org.mydomain.MyClass
var myLogger:Logger = LogManager.getLogger("org.mydomain.MyClass");
if (myLogger.isInfoEnabled()) {
myLogger.info("This is an informative log message.");
}
It is common practice to obtain loggers per class. You may thus consider the
following strategy of obtaining loggers:
private static var logger:Logger = LogManager.getLogger("org.as2lib.MyClass");
Applying this strategy you have a logger per class that can be used within per class and per instance methods of the logging class.
static public function getLoggerByObject(object):Logger
Returns the logger according to the passed-in object
.
If object
is of type 'function' it is supposed that this is the type
to get the name of otherwise it is supposed to be the instance of the type to get
the name of.
The name of the type is used as logger name.
Note that evaluating the name is rather slow. It is thus recommended to hardcode the name and use the getLoggerByName method.
object | the object to return the type name of |
the logger for the given object
static public function getLoggerByName(loggerName:String):Logger
Returns the logger according the passed-in loggerName
.
Uses the set logger repository to receive the logger that is returned.
null
is only returned if the logger repository is initialized and
returns null
or undefined
.
If the logger repository has not been initialized yet a proxy gets returned
that is replaced by the actual logger of the repository, as soon as the
repository gets initialized. This means that the following access in classes is
possible:
private static var logger:Logger = LogManager.getLogger("org.as2lib.MyClass");
But note that you shall not log messages before the actual initialization of the repository; these messages will never be logged. Proxies are just returne to enable the convenient logger access above.
loggerName | the name of the logger to return |
the logger according to the passed-in name
static public function setLogger(logger:Logger):Void
Sets the logger
that is returned on calls to the getLogger
method.
This method actually sets a singleton repository via the static
setLoggerRepository that always returns the passed-in logger
and ignores the name.
You could also set the repository by hand, this is just an easier way of doing it if you always want the same logger to be returned.
logger | the logger to return on calls to the method
|
static public function getLoggerRepository(Void):LoggerRepository
Reutrns the logger repository set via setLoggerRepository.
There is no default logger repository, so you must set it before anything else.
the set logger repository
static public function setLoggerRepository(loggerRepository:LoggerRepository):Void
Sets a new repositroy returned by getLoggerRepository.
The getLogger method uses this repository to obtain the logger for the given logger name.
loggerRepository | the new logger repository |