SwizLogger Helpers: SwizLoggerConfig and LoggerProcessor
I started using the SwizLogger recently and quickly realized I wanted to do the following:
- Create ILogger instances without importing Swiz classes in my application classes
- Easily add targets in MXML
#1 isn't a huge deal if you consider logging to be more of a system tool than an application dependency, but if I'm avoiding Singletons why not keep it up. #2 also isn't a big deal because it doesn't take much ActionScript to add logging targets, but I think it makes a lot of sense to do it declaratively.
Piotr Walczyszyn created a custom LogProcessor to address #1, but I didn't like how the TraceTarget was hard-coded into it. Instead of building more complex processor I decided to create a bare-bones LoggerProcessor and SwizLoggerConfig class.
Here's an example application's source (the SWF is just a button): Source
The LoggerProcessor is again bare-bones. All it does is inject an ILogger instance using SwizLogger:
[Logger] public var logger:ILogger;
The LoggerProcessor priority is set to be one above [Inject] so the injected ILoggers are available at [Inject] and [PostConstruct].
SwizLoggerConfig doesn't do too much either. It's just provides a default public Array property which is used to add targets to the SwizLogger.
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:utils="com.foomonger.swizframework.utils.*" xmlns:view="example.view.*" xmlns:local="*" layout="absolute"> <mx:Script> <![CDATA[ import mx.logging.LogEventLevel; ]]> </mx:Script> <utils:SwizLoggerConfig> <mx:TraceTarget includeCategory="true" includeDate="false" includeLevel="true" includeTime="true" level="{LogEventLevel.INFO}" filters="*" fieldSeparator=" | "/> </utils:SwizLoggerConfig> <local:SwizLoggerConfigExampleSwiz/> <view:MainView/> </mx:Application>
The value of these classes will depend on your personal style and preference. I wrote them so I like them. They're available on Google Code.
Happy coding.
* Update: Here's the MXML syntax for adding multiple packages to the filters Array:
<mx:TraceTarget includeCategory="true" includeDate="false" includeLevel="true" includeTime="true" level="{LogEventLevel.INFO}" fieldSeparator=" | "> <mx:filters> ["foo.bar.*", "com.foomonger.*"] </mx:filters> </mx:TraceTarget>
Hey Sam,
Great work as always. We actually have a loggingTargets property on Swiz itself, did you notice that? Maybe it makes more sense on SwizConfig?
Ben
Ben Clinkinbeard
24 Mar 10 at 1:32 PM
Wow, I totally missed that. And I’m the guy that thinks programmers need to read more code and docs. Thanks for pointing it out.
My first thought is that it makes more sense in SwizConfig. Though part of me thinks it shouldn’t be in there at all because logging targets don’t modify the Swiz instance. Then again it’s more for convenience and having it in Swiz/SwizConfig means one less object.
Sam Ahn
24 Mar 10 at 1:50 PM