Using the Flex 3 Logging API
Using the Flex 3 logging API is not really cut and dry. It is made to be rather extensible and flexible but at the same time it manages to be completely painful and hard to understand. The Log, ILogger, LogLogger, TraceTarget, LogEvent and LogEvent level classes and be rather cumbersome.
An example of a straight up ‘log write’ would be:
var mylog:ILogger = Log.getLogger("my.package.MyClassName"); if(Log.isDebug()){ mylog.debug("Hello from the logger"); }
This would get you [DEBUG] level output with the message “Hello from the logger”. The problem with this approach is that I wanted to be able to log from any class, without having to create ILogger instances in all classes. I also wanted the logging to be dynamic so I could hide the class package if I wanted, I also wanted the LogEvents to carry all the information that the actual ‘log write’ contained.
So I created CustomLogger and CustomLogEvent classes that are built on the pre-existing logging API. This simple goal of these classes is to make logging easier. Now you can directly call:
CustomLogger.debug("Hello from the custom_logger");
or you can access it with a Singleton instance:
var log:CustomLogger = CustomLogger.getInstance(); log.debug("Hello from the custom_logger");
So thats all good and well but how does this make things easier? By using the CustomLogger you now have FULL control over the log messages as opposed to handing them off to a Flex ILogger/LogLogger and letting it do what it will with the log messages. Also, you do not need to instantiate a new ILogger for every class that wants to write log output. Additionally the expanded CustomLogEvent class gives whatever is listening the full rundown on what is happening in terms of logging as opposed to just a glimpse.
Here is an example that shows how useful the new CustomLogger/CustomLogEvent classes are especially when it comes to directing log output to a different target (in this case, a List component).
