Stop tracing and start logging

    Debug information with the good old “trace” is around since ActionScript 1 and has been heavily used as this was a key for debugging a Flash application. In AS3 trace has lost its importance as Flash Player 9 supports Runtime Exceptions and the Flex Builder debugger supports breakpoints so you can have a look at all variables where and when you want.

    Instead of “trace” I recommend to use the logging API of the Flex framework. To have the output also on the Console window add the TraceTarget to your main application MXML:

    <mx:TraceTarget fieldSeparator="->" includeCategory="true" includeLevel="true" includeTime="true" />

    The nice thing you get when you add the TraceTarget is that the internal logs of the Flex framework also show up. If you use for instance HTTPService and load something this shows up in your console:

    “[SWF] Users:soenkerohde:Documents:workspace:Example:bin-debug:Example.swf – 191,465 bytes after decompression
    19:23:22.843->[INFO]->mx.messaging.Producer->’54A8E65E-90C9-3D70-66F6-BA1BEE5842F9′ producer set destination to ‘DefaultHTTP’.
    19:23:22.956->[INFO]->mx.messaging.Channel->’direct_http_channel’ channel endpoint set to http:
    19:23:22.960->[INFO]->mx.messaging.Producer->’54A8E65E-90C9-3D70-66F6-BA1BEE5842F9′ producer sending message ‘33FDE4CB-764A-C757-4FC6-BA1BEECFF0A6′
    19:23:22.973->[DEBUG]->mx.messaging.Channel->’direct_http_channel’ channel sending message:
    (mx.messaging.messages::HTTPRequestMessage)#0
    body = (Object)#1
    clientId = (null)
    contentType = “application/x-www-form-urlencoded”
    destination = “DefaultHTTP”
    headers = (Object)#2
    httpHeaders = (Object)#3
    messageId = “33FDE4CB-764A-C757-4FC6-BA1BEECFF0A6″
    method = “GET”
    recordHeaders = false
    timestamp = 0
    timeToLive = 0
    url = “http://feeds.feedburner.com/soenkerohde?format=xml”
    19:23:22.978->[INFO]->mx.messaging.Producer->’54A8E65E-90C9-3D70-66F6-BA1BEE5842F9′ producer connected.
    19:23:23.002->[INFO]->mx.messaging.Producer->’54A8E65E-90C9-3D70-66F6-BA1BEE5842F9′ producer acknowledge of ‘33FDE4CB-764A-C757-4FC6-BA1BEECFF0A6′.
    19:23:23.006->[INFO]->mx.rpc.http.HTTPService->Decoding HTTPService response
    19:23:23.011->[DEBUG]->mx.rpc.http.HTTPService->Processing HTTPService response message:
    (mx.messaging.messages::AcknowledgeMessage)#0
    body = “… REMOVED THE CONTENT HERE”
    clientId = “DirectHTTPChannel0″
    correlationId = “33FDE4CB-764A-C757-4FC6-BA1BEECFF0A6″
    destination = “”
    headers = (Object)#1
    messageId = “6AF8650F-3634-FD0D-FF23-BA1BEEFFCDDB”
    timestamp = 0
    timeToLive = 0″

    As you see this a bunch of information and the brackets in front indicate the level of the logging message which are DEBUG and INFO here. If you are not interested in DEBUG logs at all simply change the level in the TraceTarget to LogEventLevel.INFO.

    What you need to log on your own is pretty simple. Just add this to every class (MXML/AS) to define your logger constant:

    import mx.logging.Log;
    import mx.logging.ILogger;
    private static const logger:ILogger = Log.getLogger("MyCategory");

    Now you can use the constant like:

    logger.info("this is an info message");

    which shows up as: 19:51:47.917->[INFO]->MyCategory->this is an info message
    Replace “MyCategory” with a category name. I always use the name of the class. In this case an info message is used but there is also debug, warn, error and fatal to represent the kind of your log message.

5 comments to Stop tracing and start logging

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">