我们在编写Flex应用程序的时候经常会忽略logging,发现问题需要debug的时候用的最多的就是Alert,其实使用Flex的logging功能相当简单。
1. Client Side Logging
Flex 的 Logging 功能有两种类型。Client side的了logging 和 Server side 的logging。Client side 的logging 比较适合程序员在开发的过程中进行代码追踪,调试。可能有的朋友会问,不是有强大的Flex Builder用来开发调试吗,干嘛还要不厌其烦的一行行打log?Flex Builder 是很强大,但在某些开发场景下不一定用的上FlexBuilder,譬如我们使用IBM RAD来开发Web应用,后台使用java,前台使用flex,中间的数据服务使用BlazeDS,在这种情况下,如何对前端代码Debug?所以 Client side logging 作为调试利器还是少不了的。下面简单介绍一下Flex的Client Side Logging。
1) 配置logging文件
Flex的Logging不象Java的Logging,我们不能自定义logging文件。Flex的logging文件位置都比较固定,具体位置视操作系统的不同而有所区别。Windows2000/XP系统一般在 C:\Documents and Settings\username\Application Data\Macromedia\Flash Player\Logs \FlashLog.txt 。除了FlashLog.txt文件外,我们还要设置另外一个文件mm.cfg在目录 C:\Documents and Settings\username 下,配置内容参考如下:
ErrorReportingEnable=1
TraceOutputFileEnable=1
MaxWarnings=1
2) Flex Trace
我们可以直接在代码中使用Trace方法向FlashLog.txt文件输出log消息。Trace方法只能在Flash Debug Player中执行。
3) FlashTracer Firefox Plugin
这是一个相当好用的Firefox插件,可以在Firefox浏览器中实时跟踪log输出,非常利于调试,下载地址是https://addons.mozilla.org/en-US/firefox/search?q=FlashTracer&cat=all 。
4) Flex Logging API
Flex 提供了一套非常好用的Logging API, 分别在SDK包mx.logging, mx.logging.target和mx.logging.error下。有兴趣的朋友可以去Adobe LiveDoc了解一下其详细的用法http://livedocs.adobe.com/flex/3/html/help.html?content=logging_09.html
什么都不说了,下面提供一个简单的使用LoggingAPI的例子
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()"
- horizontalAlign="center" >
- <mx:Script>
- <![CDATA[
- import mx.logging.LogLogger;
- import mx.logging.ILogger;
- import mx.logging.Log;
- import mx.logging.LogEventLevel;
- import mx.logging.targets.TraceTarget;
- private var logger:ILogger;
- private function init():void{
- var logTarget:TraceTarget = new TraceTarget();
- logger = new LogLogger("xx");
- logTarget.filters = ["main"];
- logTarget.level = LogEventLevel.ALL;
- logTarget.includeCategory = true;
- logTarget.includeDate = true;
- logTarget.includeLevel = true;
- logTarget.includeTime = true;
- logTarget.addLogger(logger);
- Log.addTarget(logTarget);
- //trace("create complete");
- }
- private function clickBtn():void{
- //var logger:LogLogger = new LogLogger("*");
- logger.debug("It's the debug");
- logger.info("It's the info");
- logger.warn("It's the warn");
- logger.error("It's the error");
- logger.fatal("It's the fatal");
- //trace("It's the first click on btn");
- }
- ]]>
- </mx:Script>
- <mx:Button id="btn" label="OK" click="clickBtn()"/>
- </mx:Application>
2. Server Side Logging
Flex的Server Side Logging其实是Flex Logging API的一个配置版本,一般我们讨论Server Side Logging指的都是Flex的Data Services Logging,以BlazeDS为例,我们可以如下配置Server Side Logging (在BlazeDS的services-config.xml中)
- <logging>
- <target class="flex.messaging.log.ConsoleTarget" level="Debug">
- <properties>
- <prefix>[BlazeDS]</prefix>
- <includeDate>false</includeDate>
- <includeTime>false</includeTime>
- <includeLevel>false</includeLevel>
- <includeCategory>false</includeCategory>
- </properties>
- <filters>
- <pattern>Endpoint.*</pattern>
- <pattern>Service.*</pattern>
- <pattern>Configuration</pattern>
- </filters>
- </target>
- </logging>
1) Level
在以上的配置中,我们可以发现level属性,Flex中一共有6种level:
level | Description |
All | Logs all messages |
Debug | Logs debug message. Debug messages indicate internal Flex activities |
Error | Logs error messages. Error messages indicate when a critical service is not available or a situation restricts use of the application. |
Info | Logs information messages. Information messages indicate general information to the developer or administrator. |
None | No messages are logged. |
Warn | Logs warning messages. Warning messages indicate that Flex encountered a problem with the application, but the application does not stop running. |
Level的级别顺序为All-Debug-Info-Warn-Error-None,一般在开发过程中开启Info级别来追踪Flex数据传递中的问题就已经足够了,而在Production环境下开启Error级别更为合理。
2) Target Class
上面的配置中中比较重要的是Target Class,BlazeDS默认是flex.messaging.log.ConsoleTarget, Flex中Target的概念和Log4j中Appender的概念类似,我们不仅可以将log打印到console(ConsoleTarget),而且可以输出至文件中(TraceTarget),特别地,还可以使用flex.messaging.log.ServletLogTarget来将Log记录到默认的servlets中。Target是一个全局设置,所以如果我们在ActionScipts中创建Target就必须使用Log类的全局方法addTarget来添加Target,从而使得Flex的log event能被该Target接收到。
3) Properties
- prefix: log消息的前缀,会打印在log消息的最前面
- includeDate: log消息是否输出日期
- includeTime: log消息是否输出时间
- includeLevel: 是否输出级别信息
- includeCategory: 是否输出分类信息
4) Filter
再看一下filter配置,这主要是用来对Log作category过滤的。只有符合配置设置的log才会被打印出来。Flex Logging中category概念同样类似于Log4j中的category,Flex编程规范推荐我们使用类的全限定名来作为logger的category名,我们一般在新建logger的时候制定logger所属的分类。
- private var logger:ILogger = new LogLogger("com.storm.MyPlane")
如上所示,logger的category即是“com.storm.MyPlane”,如果我们在Fliter中只配置了<pattern>com.storm.*</pattern>,那么只有位于包com.storm下的类的logging输出才会被打印出来。
总之,Flex的logging在概念方面和java非常相似,但是flex logging的灵活性相比java要低,但这也恰恰却更利于flex开发人员学习与理解。