May 26

Log Package for Flex and Air Applications

Filed under: Development | Taged as: , , , | 2 comments

The ASLib de.flamelab.log package provides classes and functionalities for general purpose logging. It can be regarded as an alternative to the Flex build in mx.loggging package as it offers a different approach, similar to the Zend_Log component in PHP. It can be used in any ActionScript based project and is therefore not limited to Flex applications.

Overview

The package supports multiple log backends and formatting and filtering messages sent to the log. These functionality is devided into to the following objects:

  • A Log (de.flamelab.log.Log) represents the basic object. An application can have as many Log objects as you like. Log objects can be sotred in the LogRegistry
  • A Writer (de.flamelab.log.AbstractLogWriter) implements a concrete logging mechanism and is therfore responsible for saving the message to a storage. New Writers can be added by inheriting and implementing the abstract base class.
  • A Filter (de.flamelab.log.ILogFilter) blocks data from being logged. A filter is applied to a Writer object and can be chained with other filters.
  • A Formatter(de.flamelab.log.ILogFormatter) is used to format the log messages before they are written by a Writer. Each Writer has excatly one Formatter.
  • A LogRegistry (de.flamelab.log.LogRegistry) can be used to gain convenient access to different Log instances among different classes.
  • A LogEvent (de.flamelab.log.LogEvent) is used for encapsulating the necessary log information an is dispatched to all assigned Writers of a Log object.

Logging messages

To start logging, a Writer intance must be passed to a Log instance and the log() Method can be called on the Log. Additionally, the Log instance can be stored in the LogRegistry for further usage. Writer instances can be fetched through a static method of a LogFactory. A Log instance must at least have one Writer assigned with the addWriter() method to it to work properly. If multiple Writers are assigned, each Writer recieves the log data.

logger = new Log();
LogRegistry.setLog('testlogger', logger);
var write:AbstractLogWriter = LogFactory.getWriter(LogFactory.FIREBUG);
LogRegistry.getLog('testlogger').addWriter(writer);
logger.log("emergency log message", Log.EMERG);

The log method accepts two arguments, the log message and a priority. The Log class defines the following priorites, based on the BSD syslog protocol, described in RFC-3164:

  • EMERG – Emergency: system is unusable
  • ALERT – Alert: action must be taken immediately
  • CRIT – Critical: critical conditions
  • ERR – Error: error conditions
  • WARN – Warning: warning conditions
  • NOTICE – Notice: normal but significant condition
  • INFO – Informational: informational messages
  • DEBUG – Debug: debug messages

By calling the log() method, a LogEvent instance is created an populated with all necessary data describing the log action. It is passed to all Writer objects and its creation is completely transparent.

Writers

A Writer is an object which responsibility is to record the log data to a defined storage. All Writer inherit and implement the abstract AbstractLogWriter class. Each Writer can be instantiated directly or it can be fetched through the LogFactory. The package already has the following Writer objects implemented:

  • LogWriterFirebug: Writes the data to the Firebug Console.
  • LogWriterAirFile: Uses the FileStream object to write log messages to a defined logfile on the clients’ filesystem.
  • LogWriterAirSqlite: Uses Airs’ SQLite ability and stores messages in a automatically created, local database table.

All Writers are meant for logging certain application activities or errors during application runtime, not only while developing or debugging.

Formatters

A Formatter takes an LogEvent and formates its data in a predefined manner for storage. Some Writers are not line-oriented and cannot use a Formatter. The LogWriterAirSqlite for example takes the data from the event and inserts its values into the database directly.

Each Formatter must implement the ILogFormatter interface. The package includes a basic LogFormatterSimple that uses a user-defined pattern for arranging the data provided by the LogEvent in a message. It is also used for default formatting when no Formatter is specified. A Formatter is set on an individual Writer by using the setFormatter() method.

logger = new Log();
var writer:AbstractLogWriter = LogFactory.getWriter(
LogFactory.AIRFILE,
{uri:"logs/log.txt"});
var format:LogFormatterSimple = new LogFormatterSimple("%timestamp%:%message%");
writer.setFormatter(format);
logger.addWriter(writer);
logger.log("error log message", Log.ERR);

Filters

A Filter objects decides if a message should be stored or blocked from being written to the log. The addFilter() method is used to add a Filter to a Writer. They can be chained, as multiple Filters can be applied.
Each Filter mus implement the ILofFilter interface. The package comes with two predefined classes:

  • LogFilterPriority: Only messages with a priority smaller or equal the given value are logged.
  • LogFilterRegex: Uses a regular expression to decide whether a message should be blocked.
logger = new Log();
var writer:AbstractLogWriter = LogFactory.getWriter(
LogFactory.AIRFILE,
{uri:"logs/log.txt"});
var prioFilter:LogFilterPriority = new LogFilterPriority(Log.WARN);
var regex:RegExp = /.*?(critical).*?/;
var regexFilter:LogFilterRegex = new LogFilterRegex(regex);
writerDB.addFilter(prioFilter);
writerDB.addFilter(regexFilter);
logger.addWriter(writer);
logger.log("error log message", Log.ERR);

The package is part of the ASLib. Get the ASLib

Comments are closed.