Today I have updated the ThunderBolt AS 3 package for logging Flex 2 applications with Firebug using the Flex 2 Logging Framework. For this reason I have added a common way for using log levels as well – you’ll get more information about it on my next blog entry next week ;-) .

However, ThunderBolt includes now a custom target class called ThunderBoltTarget extending the Flex 2 AbstractTarget class for using default behaviors and properties of the Flex 2 Logging Framework such as filters or common log levels. For more information about custom targets check out the Flex 2 LiveDocs: “Using the Logging API

Example

Get Adobe Flash player

 

Using ThunderBoltTarget

  1. Create an instance of ThunderBoltTarget

    1.  
    2. _target = new ThunderBoltTarget();
    3.  
  2. Optionally you can hide the output of time, log levels and category…

    1. _target.includeTime = false;
    2. _target.includeLevel = false;
    3. _target.includeCategory = false;
  3. …or filter the message using wildcards, e.g. only messages for the classes in the de.websector.* package.

    1.  
    2. _target.filters = ["de.websector.*"];
    3.  
  4. Add the ThunderBoltTarget as a custom target for the Flex Log instance

    1.  
    2. Log.addTarget(_target);
    3.  
  5. Call the Flex Log instance to send an info message to ThunderBolt. The getLogger() method defines a specified category, which describes an ID for mapping log messages as described above via _target.filters

    1.  
    2. Log.getLogger("de.websector.playground.ThunderBoltTargetExample").info("Just an info message.");
    3.  
  6. Flex Logging Framework supports logging of multiple objects using flags such as {0}, {1}, etc. Note: The outputs of these objects are only Strings, not a complex hierarchy of all properties and methods.

    1.  
    2. Log.getLogger("de.websector.playground.ThunderBoltTargetExample").error("Calling two objects, a number named myNumber and a array called myArray: {0}, {1}", myNumber, myArray);
    3.  
  7. All code of the example above.

    ThunderBoltTargetExample.mxml ( Download code )

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    3.     initialize="initializeHandler( )">
    4.  
    5.     <mx:Script>
    6.         <![CDATA[
    7.             import org.osflash.thunderbolt.Logger;
    8.             import flash.events.Event;
    9.            
    10.             private function traceToFirebug(event:Event):void 
    11.             {
    12.                 var myNumber: int = 5;
    13.                 var myString: String = "Lorem ipsum";
    14.                 var myArray: Array = ["firstValue",{x: 100, y: 200}, "secondValue"];
    15.                
    16.                 var label: String = event.target.label;
    17.            
    18.                 switch (label)
    19.                 {
    20.                     case "info":
    21.                         Log.getLogger("de.websector.playground.ThunderBoltTargetExample").info("Just an info message.");
    22.                     break;
    23.                     case "warn":
    24.                         Log.getLogger("de.websector.playground.ThunderBoltTargetExample").warn("Here is the value of the String named myString: {0}", myString);
    25.                     break;
    26.                     case "error":
    27.                         Log.getLogger("de.websector.playground.ThunderBoltTargetExample").error("Calling two objects, a number myNumber and a nested array called myArray: {0}, {1}", myNumber, myArray);
    28.                     break;
    29.                     case "debug":
    30.                         Log.getLogger("de.websector.playground.ThunderBoltTargetExample").debug("Just a info message.");
    31.                     break;
    32.                     default:
    33.  
    34.                 }
    35.             }
    36.  
    37.             import mx.logging.Log;
    38.             import org.osflash.thunderbolt.ThunderBoltTarget;
    39.            
    40.             private var _target: ThunderBoltTarget;
    41.            
    42.             private function initializeHandler( ):void
    43.             {
    44.                 _target = new ThunderBoltTarget();
    45.                 /*
    46.                 You can disable the time, level or category as follow            
    47.                 _target.includeTime = false;
    48.                 _target.includeLevel = false;
    49.                 _target.includeCategory = false;                
    50.                 */              
    51.                 _target.filters = ["de.websector.playground.ThunderBoltTargetExample"];
    52.                 Log.addTarget(_target);
    53.             }
    54.            
    55.            
    56.             private function getFireBug( ):void
    57.             {
    58.                 var url:URLRequest = new URLRequest( "http://www.getfirebug.com/" );
    59.                 navigateToURL( url );
    60.             }
    61.        
    62.       ]]>
    63.     </mx:Script>
    64.     <mx:Style source="css/logger.css"/>
    65.     <mx:Text htmlText="Using ThunderBoltTarget based on the Flex 2.0 Logging Framework"  paddingBottom="15" />
    66.     <mx:HBox horizontalGap="10">
    67.          <mx:Button label="info"
    68.                     click="traceToFirebug(event);" id="infoButton" width="100" height="50"/>
    69.          <mx:Button label="warn"
    70.                     click="traceToFirebug(event);" id="warnButton" width="100" height="50"/>
    71.          <mx:Button label="error"
    72.                     click="traceToFirebug(event);" id="errorButton" width="100" height="50"/>      
    73.          <mx:Button label="debug"
    74.                     click="traceToFirebug(event);" id="logButton" width="100" height="50"/>
    75.     </mx:HBox>    
    76.     <mx:Text htmlText="Press F12 to open Firebug"
    77.             paddingTop="15" paddingBottom="5"/>
    78.     <mx:LinkButton label="(Who the fuck is Firebug?)" id="getFirebug" click="getFireBug();" />
    79. </mx:Application>

Source

You’ll find the latest source of ThunderBolt, its custom target called ThunderBoldTarget.as and all examples on Google Code or check it out via SVN.

Related Articles

4 Responses to “[Update - Part 1] Logging Flex 2 applications with Firebug and ThunderBolt using the Flex 2 Logging Framework”

  1. Martin Kleppe Says:

    Hey, thats cool! I like the settings you’ve provided such as logging level, time, category (I didn’t get what this is all about). The loglevel and framecounter settings are already working for Thunderbolt AS2 but filers are definitely work to put the hands on.

  2. WS-Blog » [Update - Part 2] Logging Flex 2 and AS3 applications with Firebug and ThunderBolt Says:

    [...] part 1 I described a way for using ThunderBolt AS3 with the Flex 2 Logging Framework using an own log [...]

  3. Andreas Says:

    Hi,

    first of all the firebug logging target is great. But I have a long running flex app with lots of tracing information. So what about memory consumption? Will the logging console be cleaned up somehow? How can I achieve that?

    Thanks in advance,
    Andreas.

  4. sectore Says:

    Andreas,

    to stop logging to Firebug use the static property called “hide”:
    Logger.hide = true;
    For more information check out the ThunderBoltAS3 wiki page.

    To clean the logging console use the tab button named “clean” within Firebug. Currently its not possible to clean up the console outside from Firebug. For more information check out the Firebug Command Line API.

    I hope it helps ;-)

    -sectore

Leave a Reply

Follow sectore on Twitter