[Update - Part 1] Logging Flex 2 applications with Firebug and ThunderBolt using the Flex 2 Logging Framework
Posted on June 17, 2007
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
To see this content latest Flash Player Plugin is required.
Using ThunderBoltTarget
1) Create an instance of ThunderBoltTarget
_target = new ThunderBoltTarget();
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.
_target.filters = ["de.websector.*"];
4) Add the ThunderBoltTarget as a custom target for the Flex Log instance
Log.addTarget(_target);
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
Log.getLogger("de.websector.playground.ThunderBoltTargetExample").info("Just an info message.");
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.
Log.getLogger("de.websector.playground.ThunderBoltTargetExample").error("Calling two objects, a number named myNumber and a array called myArray: {0}, {1}", myNumber, myArray);
7) All code of the example above.
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.