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 executing SWF file for the previous example is shown below:

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.