Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.

MetadataProcessor: LogProcessor

ThomasBurleson edited this page Oct 1, 2010 · 4 revisions

Swiz has amazing support for metadata processors. Swiz has – for example – the InjectorProcessor which handles the well known [Inject] tag. With the code syntax shown below:

   [Inject(“model.currentEmployee”]
   public var who:Object = null;

The InjectorProcessor will inject an reference to currrentEmployee into the who target variable.

Logging is another best practice mostly ignored because of the painful process of setup and manual repitition involved. Developers use logging features to embed “this line was executed” reporting functionality in their code. So, let’s consider your goal of adding logging features to your Swiz application.

A history of logging messages can be invaluable to developers to monitor the “true” application workflow or event flow processes. Here is a sample of a log output:

    15:45:07.987 com.mindspace.crm.views::MyCustomClass getAvailableEmployees()
    15:45:09.014 com.mindspace.crm.views::MyCustomClass onResults_getAvailableEmployees(): count=3
    15:45:21.221 com.mindspace.crm.views::MyCustomClass selectEmployee(): EmployeeID=4F4C
    15:45:21.223 com.mindspace.crm.views::MyCustomClass loadEmployee(): EmployeeID=4F4C, autoEdit=true
    15:45:22.232 com.mindspace.crm.views::MyCustomClass onResults_SaveEmployee(): EmployeeID=4F4C
    15:45:22.242 com.mindspace.crm.views::MyCustomClass startEmployeeEdit()

With Swiz, you could easily instantiate a logger instance in your Swiz bean and then [Inject] a reference to the logger into your custom classes. But even this manual process in Swiz means that you must perform the following three (3) tasks:

  1. Create an instance of Logger and a TraceTarget (or ILoggingTarget implementation).
  2. Set logging filters to allow only desired classes in desired packages to output log messages.
  3. Manually include the name of the class [which is making a logger call] in EACH of your log calls.

With the enhanced LogProcessor (upgraded to support Swiz v1.x), logging is almost trivial AND incredibiy helpful. Consider the code samples below.

(1) Configure Swiz

In your Swiz setup do the following:

   <?xml version="1.0" encoding="utf-8"?>
   <swiz:Swiz xmlns:swiz="http://swiz.swizframework.org" xmlns:mx="http://www.adobe.com/2006/mxml">
     <swiz:customProcessors>
       <swiz:LogProcessor /> 
     </swiz:customProcessors>
   </swiz:Swiz>

(2) Use the [Log] tag:

In your custom classes (here is shown in MyCustomClass), use the ** [Log]** tag to auto inject a pre-configured an ILogger reference. The logger instance is already configured to prepend the className to the output messages.

	package com.mindspace.crm.views {
	  public class MyCustomClass {
	     [Log]
	    public var logger : ILogger;

	  public function confirmValid(uid:String):Boolean {
	       logger.debug ( “someMethod() uid=+ uid );
	      return uid != "";
	  }
	}

During application runtime

With the above Swiz settings, during runtime the LogProcessor will:

  1. Auto-creates an internal Logger and LogTarget for you,
  2. auto-adds the target class as a logging filter,
  3. auto-adds the className to the log output, and
  4. injects the customized Logger into each target class variable.

And best of all, only your classes with the [Log] metadata tag will have these auto-logging features. The [Log] features do not interfere with other existing logging mechanisms. Thus, invoking confirmValid(4) would yield a console output of:

    5/9/2010 15:45:05.987 [DEBUG] com.mindspace.crm.views::MyCustomClass someMethod() uid=4

It could not get easier!

Clone this wiki locally