The Logging Framework

The trace() statement can be a powerful method of logging, but if you have been exposed to logging in the past, you likely used some sort of logging framework or built your own. Flex includes a logging framework that offers several benefits over using the trace() statement alone.

The logging framework consists of two main components: the logger and the target. The logger is used by an application to configure the logging framework and to send messages that are output via a target.

A target is used to specify where log messages are output. They can be output to any mechanism that Flash Player supports. The logging framework includes a TraceTarget, which inherits from LineFormattedTarget and AbstractTarget and implements the ILoggingTarget interface.

TraceTarget internally sends messages via the global trace() function. This will often be the target you use. Here’s an example using the logging framework with TraceTarget:

<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="initializeHandler()"> <mx:Script> <![CDATA[ import mx.logging.Log; import mx.logging.targets.TraceTarget; private var _target:TraceTarget; private function initializeHandler():void { _target = new TraceTarget(); _target.includeTime = true; _target.includeLevel = true; _target.includeCategory = true; Log.addTarget(_target); } private function sendToLog():void { Log.getLogger("com.oreilly.programmingflex.MainClass").info("Log Message"); } ]]> ...

Get Programming Flex 3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.