Logging in AIR application
We are developing the AIR application and we needed to have a powerful logging tools that have various level of logging such as INFO, DEBUG, ERROR, FATAL that tracks the business activities at client side. Fortunately, Flex provide the such wonderful tool that meet all our requirements about the logging tool with two exceptions:
- All logging information only be recorded in flashlog.txt file (its location is different in each OS, you can see here) - we can understand that because the logging tool is not only written for AIR desktop application but only for web application tool. However, I hope in the next version of AIR, developer can point out the specific log file for some AIR application, not all things are recorded in one hard code file like the current version.
- All the configuration settings of log are hard-coded, not support configurable settings in external config file also the usage is little bit different with normal logging practices.
We try to fix the second version. We would like to use the Action Script logging tool like we use the similar tool in Java. It is nice that only little effort of writing code, we can have the complete logging tool as plan. Here are our solution:
- First, we create the LoggerFactory that is the factory class that create the ILogger for logging category. One more responsibility of LoggerFactory class is set up logging configurations whenever it is called at the first time.
- Second, class mx.logging.Log plays the factory role like our LoggerFactory. However, Log class only allow creating log instance by string category. It is not the good practice because the category name is not change when developer re-factor the class name. We add the method allow support create log instance by class name.
package com.engroup.log
...
/**
* The custom logger factory of eSoftHead company
*/
public class LoggerFactory
{
private static var instance:LoggerFactory = new LoggerFactory();
function LoggerFactory()
{
if (instance != null) {
throw new Error("The instance Logger already be exist");
}
// hard-code or can read the configuration file in application path
var logTarget:TraceTarget = new TraceTarget();
logTarget.filters = ["com.engroup.*"];
logTarget.level = LogEventLevel.ALL;
logTarget.includeCategory = true;
logTarget.includeDate = true;
logTarget.includeLevel = true;
logTarget.includeTime = true;
Log.addTarget(logTarget);
}
private function getInternalLog(classReference:Class): ILogger {
var type:XML = describeType(classReference);
var className:String = type.@name;
var category:String = className.replace("::", ".");
return Log.getLogger(category);
}
public static function getLogger(classReference:Class): ILogger {
return instance.getInternalLog(classReference);
}
}
Note that the static variable instance is used also force the logging settings are configured at the first time class LoggerFactory is invoked. The method getInternalLog simply get the full class name of a class instance and create the ILogger correspond with category equals the class name. If you want to log, simply call:
private static var log:ILogger = LoggerFactory.getLogger(ABC);
log.debug("The example log message");
Finally, we have a complete logging solution for an AIR application base on AS logging tool.