LogProcessor – custom metadata processor for Swiz 1.0.0
Just recently Swiz 1.0.0 alpha was released. Essentially it is rewrite of Swiz itself but it also brings new cool features. One of the features that I really like the most is the ability to define Custom Metadata Processors. It allows you to define your own metadata and have it wired into your code with the same mechanism that [Autowire] and [Mediate] are implemented by Swiz. I decided to give it a try by building a very simple LogProcessor that injects preconfigured ILogger from Flex API based on [Log] metadata.
The usage of this custom metadata looks like this:
[Log] public var log:ILogger; private function someFunction():void { log.debug("someFunction was called!"); }
Here is the code of my LogProcessor. The addMetadata function actually does the job:
package processors { import mx.logging.Log; import mx.logging.LogEventLevel; import mx.logging.targets.TraceTarget; import org.swizframework.core.Bean; import org.swizframework.processors.MetadataProcessor; import org.swizframework.reflection.IMetadataTag; public class LogProcessor extends MetadataProcessor { private static var traceTarget:TraceTarget; public function LogProcessor(level:int = LogEventLevel.ALL, filters:Array = null, includeDate:Boolean = true, includeTime:Boolean = true, includeCategory:Boolean = true, includeLevel:Boolean = true) { super("Log"); if (LogProcessor.traceTarget == null) { LogProcessor.traceTarget = new TraceTarget(); LogProcessor.traceTarget.level = level; LogProcessor.traceTarget.filters=filters; LogProcessor.traceTarget.includeDate = includeDate; LogProcessor.traceTarget.includeTime = includeTime; LogProcessor.traceTarget.includeCategory = includeCategory; LogProcessor.traceTarget.includeLevel = includeLevel; Log.addTarget(LogProcessor.traceTarget); } } /** * Assign ILogger instance */ override public function addMetadata(bean:Bean, metadata:IMetadataTag):void { // bean.typeDescriptor.className returns class name in packages::ClassName notation // that is not accepted by Log.getLogger function, returning only ClassName string var className:String = bean.typeDescriptor.className; className = className.substr(className.lastIndexOf(":") + 1); // Setting Logger bean.source[ metadata.host.name ] = Log.getLogger(className); } /** * Remove ILogger instance */ override public function removeMetadata(bean:Bean, metadata:IMetadataTag):void { bean.source[ metadata.host.name ] = null; } public function set level(value:int):void { LogProcessor.traceTarget.level = value; } public function set filters(value:Array):void { LogProcessor.traceTarget.filters = value; } public function set includeDate(value:Boolean):void { LogProcessor.traceTarget.includeDate = value; } public function set includeTime(value:Boolean):void { LogProcessor.traceTarget.includeTime = value; } public function set includeCategory(value:Boolean):void { LogProcessor.traceTarget.includeCategory = value; } public function set includeLevel(value:Boolean):void { LogProcessor.traceTarget.includeLevel = value; } } }


Thank you for the news Piotr, I’ve missed that.
Grzegorz Łobiński
16 Dec 09 at 1:53 pm
[...] Walczyszyn created a custom LogProcessor to address #1, but I didn't like how the TraceTarget was hard-coded into it. Instead of building [...]
SwizLogger Helpers: SwizLoggerConfig and LoggerProcessor at foomonger's blog
21 Mar 10 at 12:47 am
@Piotr
Thanks for a great idea. I upgraded your LogProcessor class to support Swiz 1.0 and enhanced with some usability functionality. This is now available on a SwizFramework fork on GitHub:
http://wiki.github.com/ThomasBurleson/swiz-framework/
I added some wiki docs with code snippets also at
http://wiki.github.com/ThomasBurleson/swiz-framework/using-logprocessoras
With your base code and the new functionality, I love LOGGING. And – best of all – I discovered several bugs that standard debugging and trace() calls did not reveal.
Thanks again.
- ThomasB
Thomas Burleson
12 May 10 at 2:49 pm
@Thomas Thank you for doing this, great job!!!
Piotr Walczyszyn
14 May 10 at 10:11 am
Wanted to also mention the online, live sample of Swiz Logging in the Swiz CafeTownsend demo:
http://www.gridlinked.info/swiz-localization-l10n-logging/#swizLogging
http://wiki.github.com/ThomasBurleson/l10nInjection_Samples/sample-applications#demo1
Thomas Burleson
3 Sep 10 at 11:47 am
I don’t see how your [Log] attribute would this be any different from this:
[Inject]
public var log:ILogger;
ash
3 Jun 11 at 11:25 pm
I feel that is one of the such a lot vital information for me. And i am glad studying your article. But want to remark on some general things, The site style is ideal, the articles is in reality nice
. Good job, cheers.
Evelin Madia
28 Oct 11 at 6:27 am