Logging

You can log messages at several different points in a Flex application's life cycle. You can log messages when you compile the application, when you deploy it to a web application server, or when a client runs it. You can log messages on the server or on the client. These messages are useful for informational, diagnostic, and debugging activities.

About logging

When you encounter a problem with your application, whether during compilation or at run time, the first step is to gather diagnostic information to locate the cause of the problem. The source of a problem typically is in one of two places: the server web application, or the client application.

Flex includes several different logging and error reporting mechanisms that you can use to track down failures:

Client-side logging and debugging

With the debugger version of Adobe® Flash® Player, or with an AIR application that you debug using ADL, you can use the global trace() method to write out messages or configure a TraceTarget to customize log levels of applications for data services-based applications. For more information, see Client-side logging and debugging.

Compiler logging

When compiling your Flex applications , you can view deprecation and warning messages, and sources of fatal errors. For more information, see Compiler logging.

Policy file logging

You can log messages for all policy file interactions between a running SWF and Flash Player. For more information, see Policy file logging.

The following example shows the types of logging you can do in the appropriate environment:

Types of logging.

To use client-side debugging utilities such as the trace() global method and client-side data services logging, you must install and configure the debugger version of Flash Player. This is described in Using the debugger version of Flash Player. The debugger version of Flash Player is not required to log compiler messages.

Using the debugger version of Flash Player

The debugger version of Flash Player is a tool for development and testing. Like the standard version of Adobe Flash Player, it runs SWF files in a browser or on the desktop in a stand-alone player. Unlike Flash Player, the debugger version of Flash Player enables you to do the following:

  • Output statements and application errors to the debugger version of the Flash Player local log file by using the trace() method.

  • Write data services log messages to the local log file of the debugger version of Flash Player.

  • View run-time errors (RTEs).

  • Use the fdb command-line debugger.

Note: Any client running the debugger version of Flash Player can view your application's trace() statements and other log messages unless you disable them. For more information, see Suppressing debug output.

The debugger version of Flash Player lets you take advantage of the client-side logging utilities such as the trace() method and the logging API. You are not required to run the debugger version of Flash Player to log compiler messages because compiling does not require a player.

Note: ADL logs trace() output from AIR applications, based on the setting in mm.cfg (the same setting used by the debug version of Flash Player).

In nearly all respects, the debugger version of Flash Player appears to be the same as the standard version of Flash Player. To determine whether or not you are running the debugger version of Flash Player, use the instructions in Determining Flash Player version in Flex.

The debugger version of Flash Player comes in ActiveX, Plug-in, and stand-alone versions for Microsoft Internet Explorer, Netscape-based browsers, and desktop applications, respectively. You can find the debugger version of Flash Player installers in the following locations:

  • Flex SDK: install_dir/runtimes/player/os_version/

Uninstall your current Flash Player before you install the debugger version of Flash Player. For information on installing the debugger version of Flash Player, see the Flex installation instructions.

You can enable or disable trace logging and perform other configuration tasks for the debugger version of Flash Player. For more information, see Editing the mm.cfg file.

Editing the mm.cfg file

You use the settings in the mm.cfg text file to configure the debugger version of Flash Player. These settings also affect logging of trace() output in AIR applications running in the ADL debugger. If this file does not exist, you can create it when you first configure the debugger version of Flash Player. The location of this file depends on your operating system.

The following table shows where to create the mm.cfg file for Flash Player 10.1 and later on several operating systems:

Operating system

Create file in …

Macintosh OS X

Flash Player first checks the user's home directory (~). If none is found, then Flash Player looks in /Library/Application Support/Macromedia

Windows 2000/XP

%HOMEDRIVE%\%HOMEPATH%

The default value is "c:\Documents and settings\username".

Your system administrator might map the home directory to a shared network drive. In that case, check with your system administrator to determine how to configure your debugger Player.

Windows Vista Windows 7

%HOMEDRIVE%\%HOMEPATH%

The default value is "c:\Users\username".

Your system administrator might map the home directory to a shared network drive. In that case, check with your system administrator to determine how to configure your debugger Player.

Linux

/home/username
Tip: On Microsoft Windows 2000, the default location of the mm.cfg file for earlier versions of Flash Player was \.

The following table lists the properties that you can set in the mm.cfg file:

Property

Description

ErrorReportingEnable

Enables the logging of error messages.

Set the ErrorReportingEnable property to 1 to enable the debugger version of Flash Player to write error messages to the log file. To disable logging of error messages, set the ErrorReportingEnable property to 0.

The default value is 0.

MaxWarnings

Sets the number of warnings to log before stopping.

The default value of the MaxWarnings property is 100. After 100 messages, the debugger version of Flash Player writes a message to the file stating that further error messages will be suppressed.

Set the MaxWarnings property to override the default message limit. For example, you can set it to 500 to capture 500 error messages.

Set the MaxWarnings property to 0 to remove the limit so that all error messages are recorded.

PolicyFileLog

Enables the logging of policy file messages.

Set PolicyFileLog to 1 to log policy file messages. The default value is 0.

For more information on using policy file logs, see Log file location.

PolicyFileLogAppend

Lets you save the contents of the policy file log file.

Set the PolicyFileLogAppend property to 1 to save previous policy file log entries. The default value is 0.

If PolicyFileLogAppend is not enabled, each new root-level SWF clears the log file. If PolicyFileLogAppend is enabled, the previous contents of the log file will always be kept, and the log file will grow at the end.

If many different root-level SWF files are loaded during your testing, you will probably want to enable PolicyFileLogAppend. However, if you enable PolicyFileLogAppend, you will probably need to manually rename or delete the log file from time to time, since otherwise it will grow to a large size, and it will be difficult to determine where previous output ends and new output begins.

For more information on using policy file logs, see Policy file logging.

TraceOutputFileEnable

Enables trace logging.

Set TraceOutputFileEnable to 1 to enable the debugger version of Flash Player to write trace messages to the log file. Disable trace logging by setting the TraceOutputFileEnable property to 0.

The default value is 0.

TraceOutputFileName

Note: Beginning with the Flash Player 9 Update, Flash Player ignores the TraceOutputFileName property and stores the flashlog.txt file in a hard-coded location based on operating system. For more information, see Log file location.

Sets the location of the log file. By default, the debugger version of Flash Player writes error messages to a file named flashlog.txt, located in the same directory in which the mm.cfg file is located.

Set TraceOutputFileName to override the default name and location of the log file by specifying a new location and name in the following form: On Macintosh OS X, you should use colons to separate directories in the TraceOutputFileName path rather than slashes.

TraceOutputFileName=<fully qualified path/filename> 

The following sample mm.cfg file enables error reporting and trace logging:

 ErrorReportingEnable=1

Log file location

Beginning with the Flash Player 9 Update, you cannot modify the log file location or name. The filename is flashlog.txt, and its location is hard-coded, depending on your operating system. The following table shows the flashlog.txt file location:

Operating System

Log file location

Windows 95/98/ME/2000/XP

C:\Documents and Settings\username\Application Data\Macromedia\Flash Player\Logs

Windows Vista/Windows 7

C:\Users\username\AppData\Roaming\Macromedia\Flash Player\Logs

Macintosh OS X

/Users/username/Library/Preferences/Macromedia/Flash Player/Logs/

Linux

/home/username/.macromedia/Flash_Player/Logs/

The default log file location changed between the initial Flash Player 9 release and the Flash Player 9 Update. In the initial Flash Player 9 release, the default location was the same directory as the mm.cfg file and you could update the log file location and name through the TraceOutputFileName property.

Determining Flash Player version in Flex

To determine which version of Flash Player you are currently using—the standard version or the debugger version—you can use the Capabilities class. This class contains information about Flash Player and the system that it is currently operating on. To determine if you are using the debugger version of Flash Player, you can use the isDebugger property of that class. This property returns a Boolean value: the value is true if the current player is the debugger version of Flash Player and false if it is not.

The following example uses the playerType, version, and isDebugger properties of the Capabilities class to display information about the Player:

<?xml version="1.0"?> 
<!-- logging/CheckDebugger.mxml --> 
<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx"> 
 
    <s:layout> 
        <s:VerticalLayout/> 
    </s:layout> 
 
    <fx:Script> 
        <![CDATA[ 
        import flash.system.Capabilities; 
 
        private function reportVersion():String { 
            if (Capabilities.isDebugger) { 
                return "Debugger version of Flash Player"; 
            } else { 
                return "Flash Player"; 
            } 
        } 
        private function reportType():String { 
            return Capabilities.playerType + " (" + Capabilities.version + ")"; 
        } 
        ]]> 
    </fx:Script> 
    
    <s:Label text="{reportVersion()}"/> 
    <s:Label text="{reportType()}"/> 
 
</s:Application>

Other properties of the Capabilities class include hasPrinting, os, and language.

Client-side logging and debugging

Often, you use the trace() method when you debug applications to write a checkpoint message on the client, which signals that your application reached a specific line of code, or to output the value of a variable.

The debugger version of Flash Player has two primary methods of writing messages that use trace():

  • The global trace() method. The global trace() method prints Strings to a specified output log file. For more information, see Using the global trace() method.

  • Logging API. The logging API provides a layer of functionality on top of the trace() method that you can use with your custom classes or with the data service APIs. For more information, see Using the logging API.

Configuring the debugger version of Flash Player to record trace() output

To record messages on the client, you must use the debugger version of Flash Player. You must also set TraceOutputFileEnable to 1 in your mm.cfg file. For more information on editing the mm.cfg file, see Editing the mm.cfg file.

The debugger version of Flash Player sends output from the trace() method to the flashlog.txt file. The location of this file is determined by the operating system. For more information, see Log file location.

You can suppress trace output by setting the omit-trace-statements compiler argument to true.

Using the global trace() method

You can use the debugger version of Flash Player to capture output from the global trace() method and write that output to the client log file. You can use trace() statements in any ActionScript or MXML file in your application. Because it is a global function, you are not required to import any ActionScript classes packages to use the trace() method.

Only debug SWF files will write out trace() statements. To create a debug SWF file, set the debug compiler argument to true.

The following example defines a function that logs the various stages of the Button control's startup life cycle:

<?xml version="1.0"?> 
<!-- logging/ButtonLifeCycle.mxml --> 
<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx"> 
 
    <fx:Script> 
        <![CDATA[ 
        private function traceEvent(event:Event):void { 
            trace(event.currentTarget + ":" + event.type); 
        } 
        ]]> 
    </fx:Script> 
 
    <s:Button id="b1" label="Click Me" 
        preinitialize="traceEvent(event)" 
        initialize="traceEvent(event)" 
        creationComplete="traceEvent(event)" 
        updateComplete="traceEvent(event)" 
    /> 
 
</s:Application>

The following example shows the output of this simple application:

 TraceLifecycle_3.b1:Button:preinitialize 
 TraceLifecycle_3.b1:Button:initialize 
 TraceLifecycle_3.b1:Button:creationComplete 
 TraceLifecycle_3.b1:Button:updateComplete 
 TraceLifecycle_3.b1:Button:updateComplete 
 TraceLifecycle_3.b1:Button:updateComplete

Messages that you log by using the trace() method should be Strings. If the output is not a String, use the String(...) conversion function, or use the object's toString() method, if one is available, before you call the trace() method.

To enable tracing, you must configure the debugger version of Flash Player as described in Configuring the debugger version of Flash Player to record trace() output.

Using the logging API

The logging API lets an application capture and write messages to a target's configured output. Typically the output is equivalent to the global trace() method, but it can be anything that an active target supports.

The logging API consists of the following parts:

Logger

The logger provides an interface for sending a message to an active target. Loggers implement the ILogger interface and call methods on the Log class. The two classes of information used to filter a message are category and level. Each logger operates under a category. A category is a string used to filter all messages sent from that logger. For example, a logger can be acquired with the category "orange". Any message sent using the "orange" logger only reaches those targets that are listening for the "orange" category. In contrast to the category that is applied to all messages sent with a logger, the level provides additional filtering on a per-message basis. For example, to indicate that an error occurred within the "orange" subsystem, you can use the error level when logging the message. The supported levels are defined by the LogEventLevel class. The Flex framework classes that use the logging API set the category to the fully qualified class name as a convention.

Log target

The log target defines where log messages are written. Flex predefines a single log target: TraceTarget, which is the most commonly used log target. This log target connects the logging API to the trace system so that log messages are sent to the same location as the output of the trace() method. For more information on the trace() method, see Using the global trace() method.

You can also write your own custom log target. For more information, see Implementing a custom logger with the logging API.

Destination

The destination is where the log message is written. Typically, this is a file, but it can also be a console or something else, such as an in-memory object. The default destination for TraceTarget is the flashlog.txt file. You configure this destination on the client.

The following example shows a sample relationship between a logger, a log target, and a destination:

Relationship between a logger, log target, and destination.

You can also use the logging API to send messages from custom code you write. You can do this when you create a set of custom APIs or components or when you extend the Flex framework classes and you want users to be able to customize their logging. For more information, see Implementing a custom logger with the logging API.

The following packages within the Flex framework are the only ones that use the logging API:

  • mx.rpc.*

  • mx.messaging.*

  • mx.data.*

To configure client-side logging in MXML or ActionScript, create a TraceTarget object to log messages. The TraceTarget object logs messages to the same location as the output of the trace() statements. You can also use the TraceTarget to specify which classes to log messages for, and what level of messages to log.

The levels of logging messages are defined as constants of the LogEventLevel class. The following table lists the log level constants and their numeric equivalents, and describes each message level:

Logging level constant (int)

Description

ALL (0)

Designates that messages of all logging levels should be logged.

DEBUG (2)

Logs internal Flex activities. This is most useful when debugging an application.

Select the DEBUG logging level to include DEBUG, INFO, WARN, ERROR, and FATAL messages in your log files.

INFO (4)

Logs general information.

Select the INFO logging level to include INFO, WARN, ERROR, and FATAL messages in your log files.

WARN (6)

Logs a message when the application encounters a problem. These problems do not cause the application to stop running, but could lead to further errors.

Select the WARN logging level to include WARN, ERROR, and FATAL messages in your log files.

ERROR (8)

Logs a message when a critical service is not available or a situation has occurred that restricts the use of the application.

Select the ERROR logging level to include ERROR and FATAL messages in your log files.

FATAL (1000)

Logs a message when an event occurs that results in the failure of the application.

Select the FATAL logging level to include only FATAL messages in your log files.

The log level lets you restrict the amount of messages sent to any running targets. Whatever log level you specify, all "lower" levels of messages are written to the log. For example, if you set the log level to DEBUG, all log levels are included. If you set the log level to WARNING, only WARNING, ERROR, and FATAL messages are logged. If you set the log level to the lowest level of message, FATAL, only FATAL messages are logged.

Using the logging API with data services

The data services classes are designed to use the logging API to log client-side and server-side messages.

Enable the logging API with data services
  1. Create a TraceTarget logging target and set the value of one or more filter properties to include the classes whose messages you want to log. You can filter the log messages to a specific class or package. You can use wildcards (*) when defining a filter.

  2. Set the log level by using the level property of the log target. You can also add detail to the log file output, such as the date and time that the event occurred, by using properties of the log target.

  3. When you create a target within ActionScript, call the Log class's addTarget() method to add the new target to the logging system. Calling the addTarget() method is not required when you create a target in MXML. As long as the client is using the debugger version of Flash Player and meets the requirements described in Configuring the debugger version of Flash Player to record trace() output, the messages are logged.

The following example configures a TraceTarget logging target in ActionScript:

<?xml version="1.0"?> 
<!-- logging/ActionScriptTraceTarget.mxml --> 
<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    creationComplete="initLogging();" 
    height="600"> 
 
    <s:layout> 
        <s:VerticalLayout/> 
    </s:layout> 
    
    <fx:Script> 
        <![CDATA[ 
        import mx.collections.ArrayCollection; 
        import mx.logging.targets.*; 
        import mx.logging.*; 
 
        [Bindable] 
        public var myData:ArrayCollection; 
 
        private function initLogging():void { 
            /* Create a target. */ 
            var logTarget:TraceTarget = new TraceTarget(); 
 
            /* Log only messages for the classes in the mx.rpc.* and 
               mx.messaging packages. */ 
            logTarget.filters=["mx.rpc.*","mx.messaging.*"]; 
 
            /* Log all log levels. */ 
            logTarget.level = LogEventLevel.ALL; 
 
            /* Add date, time, category, and log level to the output. */ 
            logTarget.includeDate = true; 
            logTarget.includeTime = true; 
            logTarget.includeCategory = true; 
            logTarget.includeLevel = true; 
 
            /* Begin logging. */ 
            Log.addTarget(logTarget); 
        } 
        ]]> 
    </fx:Script> 
 
    <fx:Declarations> 
        <!-- HTTPService is in the mx.rpc.http.* package --> 
        <mx:HTTPService id="srv" 
            url="../assets/trace_example_data.xml" 
            useProxy="false" 
            result="myData=ArrayCollection(srv.lastResult.data.result)"/> 
    </fx:Declarations> 
 
    <mx:LineChart id="chart" dataProvider="{myData}" showDataTips="true"> 
        <mx:horizontalAxis> 
            <mx:CategoryAxis categoryField="month"/> 
        </mx:horizontalAxis> 
        <mx:series> 
            <mx:LineSeries yField="apple" name="Apple"/> 
            <mx:LineSeries yField="orange" name="Orange"/> 
            <mx:LineSeries yField="banana" name="Banana"/> 
        </mx:series> 
    </mx:LineChart> 
 
    <s:Button id="b1" click="srv.send();" label="Load Data"/> 
 
</s:Application>

In the preceding example, the filters property is set to log messages for all classes in the mx.rpc and mx.messaging packages. In this case, it logs messages for the HTTPService class, which is in the mx.rpc.http.* package.

You can also configure a log target in MXML. When you do this, though, you must be sure to use an appropriate number (such as 2) rather than a constant (such as DEBUG) for the log level.

The following example sets the values of the filters for a TraceTarget logging target by using MXML syntax:

<?xml version="1.0"?> 
<!-- charts/MXMLTraceTarget.mxml --> 
<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    creationComplete="initApp();" 
    height="600"> 
 
    <s:layout> 
        <s:VerticalLayout/> 
    </s:layout> 
 
    <fx:Declarations> 
        <!-- HTTPService is in the mx.rpc.http.* package --> 
        <mx:HTTPService id="srv" 
            url="../assets/trace_example_data.xml" 
            useProxy="false" 
            result="myData=ArrayCollection(srv.lastResult.data.result)"/> 
 
        <mx:TraceTarget id="logTarget" 
            includeDate="true" 
            includeTime="true" 
            includeCategory="true" 
            includeLevel="true"> 
            <mx:filters> 
                <fx:Array> 
                    <fx:String>mx.rpc.*</fx:String> 
                    <fx:String>mx.messaging.*</fx:String> 
                </fx:Array> 
            </mx:filters> 
            <!-- 0 is represents the LogEventLevel.ALL constant. --> 
            <mx:level>0</mx:level> 
        </mx:TraceTarget> 
    </fx:Declarations> 
    
    <fx:Script> 
        <![CDATA[ 
        import mx.collections.ArrayCollection; 
        import mx.logging.Log; 
 
        [Bindable] 
        public var myData:ArrayCollection; 
        
        private function initApp():void { 
            Log.addTarget(logTarget); 
        }        
        ]]> 
    </fx:Script> 
 
    <mx:LineChart id="chart" dataProvider="{myData}" showDataTips="true"> 
        <mx:horizontalAxis> 
            <mx:CategoryAxis categoryField="month"/> 
        </mx:horizontalAxis> 
        <mx:series> 
            <mx:LineSeries yField="apple" name="Apple"/> 
            <mx:LineSeries yField="orange" name="Orange"/> 
            <mx:LineSeries yField="banana" name="Banana"/> 
        </mx:series> 
    </mx:LineChart> 
 
    <s:Button id="b1" click="srv.send();" label="Load Data"/> 
 
</s:Application>

Implementing a custom logger with the logging API

If you write custom components or an ActionScript API, you can use the logging API to access the trace system in the debugger version of Flash Player. You do this by defining your log target as a TraceTarget, and then calling methods on your logger when you log messages.

The following example extends a Button control. It writes log messages for the startup life cycle events, such as initialize and creationComplete, and the common UI events, such as click and mouseOver.

// logging/MyCustomLogger.as 
package { // The empty package. 
    import mx.controls.Button; 
    import flash.events.*; 
    import mx.logging.*; 
    import mx.logging.targets.*; 
        
    public class MyCustomLogger extends Button { 
 
        private var myLogger:ILogger; 
 
        public function MyCustomLogger() { 
            super(); 
            initListeners(); 
            initLogger(); 
        } 
        private function initListeners():void { 
            // Add event listeners life cycle events. 
            addEventListener("preinitialize", logLifeCycleEvent); 
            addEventListener("initialize", logLifeCycleEvent); 
            addEventListener("creationComplete", logLifeCycleEvent); 
            addEventListener("updateComplete", logLifeCycleEvent); 
            
            // Add event listeners for other common events. 
            addEventListener("click", logUIEvent);      
            addEventListener("mouseUp", logUIEvent);        
            addEventListener("mouseDown", logUIEvent);      
            addEventListener("mouseOver", logUIEvent);      
            addEventListener("mouseOut", logUIEvent);       
        } 
        private function initLogger():void { 
            myLogger = Log.getLogger("MyCustomClass"); 
        } 
 
        private function logLifeCycleEvent(e:Event):void { 
            if (Log.isInfo()) { 
                myLogger.info(" STARTUP: " + e.target + ":" + e.type); 
            } 
        } 
 
        private function logUIEvent(e:MouseEvent):void { 
            if (Log.isDebug()) { 
                myLogger.debug(" EVENT:   " + e.target + ":" + e.type); 
            } 
        } 
    } 
}

Within the application that uses the MyCustomLogger class, define a TraceTarget, as the following example shows:

<?xml version="1.0"?> 
<!-- logging/LoadCustomLogger.mxml --> 
<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    xmlns="*" > 
 
    <s:layout> 
        <s:VerticalLayout/> 
    </s:layout> 
 
    <fx:Declarations> 
        <mx:TraceTarget level="0" 
            includeDate="true" 
            includeTime="true" 
            includeCategory="true" 
            includeLevel="true"> 
            <mx:filters> 
                <fx:Array> 
                    <fx:String>*</fx:String> 
                </fx:Array> 
            </mx:filters> 
        </mx:TraceTarget> 
    </fx:Declarations> 
 
    <MyCustomLogger label="Click Me"/> 
 
</s:Application>

After running this application, the flashlog.txt file looks similar to the following:

 3/9/2009 18:58:05.042 [INFO] MyCustomLogger STARTUP: Main_3.mcc:MyCustomLogger:preinitialize 
 3/9/2009 18:58:05.487 [INFO] MyCustomLogger STARTUP:  
 Main_3.mcc:MyCustomLogger:initialize 
 3/9/2009 18:58:05.557 [INFO] MyCustomLogger STARTUP: Main_3.mcc:MyCustomLogger:creationComplete 
 3/9/2009 18:58:05.567 [INFO] MyCustomLogger STARTUP: Main_3.mcc:MyCustomLogger:updateComplete 
 3/9/2009 18:58:05.577 [INFO] MyCustomLogger STARTUP: Main_3.mcc:MyCustomLogger:updateComplete 
 3/9/2009 18:58:05.577 [INFO] MyCustomLogger STARTUP: Main_3.mcc:MyCustomLogger:updateComplete 
 3/9/2009 18:58:06.849 [DEBUG] MyCustomLogger EVENT: Main_3.mcc:MyCustomLogger:mouseOver 
 3/9/2009 18:58:07.109 [DEBUG] MyCustomLogger EVENT: Main_3.mcc:MyCustomLogger:mouseDown 
 3/9/2009 18:58:07.340 [DEBUG] MyCustomLogger EVENT: Main_3.mcc:MyCustomLogger:mouseUp 
 3/9/2009 18:58:07.360 [DEBUG] MyCustomLogger EVENT: Main_3.mcc:MyCustomLogger:click 
 3/9/2009 18:58:07.610 [DEBUG] MyCustomLogger EVENT: Main_3.mcc:MyCustomLogger:mouseOut

To log a message, you call the appropriate method of the ILogger interface. The ILogger interface defines a method for each log level: debug(), info(), warn(), error(), and fatal(). The logger logs messages from these calls if their levels are at or under the log target's logging level. If the target's logging level is set to all, the logger records messages when any of these methods are called.

To improve performance, a static method corresponding to each level exists on the Log class, which indicates if any targets are listening for a specific level. Before you log a message, you can use one of these methods in an if statement to avoid running the code. The previous example uses the Log.isDebug() and Log.isInfo() static methods to ensure that the messages are of level INFO or DEBUG before logging them.

The previous example logs messages dispatched from any category because the TraceTarget's filters property is set to the wildcard character (*). The framework code sets the category of the logger to the fully qualified class name of the class in which logging is being performed. This is by convention only; any String specified when calling Log.getLogger(x) is the category required in a filters property to receive the message.

When you set the filters property for logging within the Flex framework, you can restrict this to a certain package or packages, or to other classes. To restrict the logging to your custom class only, add the category specified when the logger was acquired ("MyCustomLogger") to the filters Array, as the following example shows:

 <mx:filters> 
 	<mx:Array> 
 		<mx:String>MyCustomLogger</mx:String> 
 	</mx:Array> 
 </mx:filters>

In ActionScript, you can set the filters property by using the following syntax:

 traceTarget.filters = ["p1.*", "p2.*", "otherPackage*"];

The wildcard character can appear only at the end of a value in the Array.

The Log.getLogger() method sets the category of the logger. You pass this method a String that defines the category.

Note: The Flex packages that use the logging API set the category to the current class name by convention, but it can be any String that falls within the filters definitions.

The value of the category must fall within the definition of at least one of the filters for the log message to be logged. For example, if you set the filters property to something other than "*" and you use Log.getLogger("MyCustomLogger"), the filter Array must include an entry that matches MyCustomLogger, such as "MyCustomLogger" or "My*".

You can include the logger's category in your log message, if you set the logger's includeCategory property to true.

You can also use the ILogger interface's log() method to customize the log message, and you can specify the logging level in that method. The following example logs messages that use the log level that is passed into the method:

package { // The empty package. 
    // logging/MyCustomLogger2.as 
    import mx.controls.Button; 
    import flash.events.*; 
    import flash.events.MouseEvent; 
    import mx.logging.*; 
    import mx.logging.targets.*; 
        
    public class MyCustomLogger2 extends Button { 
 
        private var myLogger:ILogger; 
 
        public function MyCustomLogger2() { 
            super(); 
            initListeners(); 
            initLogger(); 
        } 
        private function initListeners():void { 
            // Add event listeners life cycle events. 
            addEventListener("preinitialize", logLifeCycleEvent); 
            addEventListener("initialize", logLifeCycleEvent); 
            addEventListener("creationComplete", logLifeCycleEvent); 
            addEventListener("updateComplete", logLifeCycleEvent); 
            
            // Add event listeners for other common events. 
            addEventListener("click", logUIEvent);      
            addEventListener("mouseUp", logUIEvent);        
            addEventListener("mouseDown", logUIEvent);      
            addEventListener("mouseOver", logUIEvent);      
            addEventListener("mouseOut", logUIEvent);       
        } 
        private function initLogger():void { 
            myLogger = Log.getLogger("MyCustomClass"); 
        } 
 
        private function logLifeCycleEvent(e:Event):void { 
            if (Log.isInfo()) { 
                dynamicLogger(LogEventLevel.INFO, e, "STARTUP"); 
            } 
        } 
 
        private function logUIEvent(e:MouseEvent):void { 
            if (Log.isDebug()) { 
                dynamicLogger(LogEventLevel.DEBUG, e, "EVENT"); 
            } 
        } 
        
        private function dynamicLogger( 
                level:int, 
                e:Event, prefix:String):void { 
            var s:String = "__" + prefix + "__" + e.currentTarget + 
                ":" + e.type; 
            myLogger.log(level, s); 
        } 
 
    } 
}

Compiler logging

Flex provides you with control over the output of warning and debug messages for the application and component compilers. When you compile, you can enable the message output to help you to locate and fix problems in your application.

For the command-line compiler, the settings that you use to control messages are defined in the flex-config.xml file or as command-line compiler options.

You have a high level of control over what compiler messages are displayed. For example, you can enable or disable messages such as binding-related warnings in the flex-config.xml file by using the show-binding-warnings option. The following example disables these messages in the flex-config.xml file:

 <show-binding-warnings>false</show-binding-warnings>

You can also set this option on the command line.

If you enable compiler messages, they are written to the console window (or System.out) by default.

For more information on the compiler logging settings, see Viewing warnings and errors.

Policy file logging

Flash Player 9 and later support policy file logging. The policy file log shows messages for every event relating to policy files like the crossdomain.xml file. This includes attempts to retrieve them, successes and failures in processing them, and the fate of the requests that depend on them.

The policy file log file is named policyfiles.txt. It is in the same location as the trace log (flashlog.txt). For information on where to find these log files, see Log file location.

To use policy file logging, you must use the debug version of Flash Player. In addition, you must add the following to your mm.cfg file:

 PolicyFileLog=1   # Enables policy file logging 
 PolicyFileLogAppend=1  # Optional; do not clear log at startup

For more information about editing the mm.cfg file, see Editing the mm.cfg file.

To test if you are logging policy file log messages, open any SWF file with your debug player. The application does not need to use a policy file, or even be running on the network. In the log file, you should see at least one message indicating that the root-level SWF was loaded.

Navigation

Using Flex » Developer tools

Adobe and Adobe Flash Player are either registered trademarks or trademarks of Adobe Systems Incorporated in the United States and/or other countries and are used by permission from Adobe. No other license to the Adobe trademarks are granted.