Flex 如何输出日志

2019-03-02 23:40|来源: 网路

我们在编写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的例子

 

Flex代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()"  
  3.       horizontalAlign="center" >   
  4.        
  5.      <mx:Script>   
  6.          <![CDATA[   
  7.              import mx.logging.LogLogger;   
  8.              import mx.logging.ILogger;   
  9.              import mx.logging.Log;   
  10.              import mx.logging.LogEventLevel;   
  11.              import mx.logging.targets.TraceTarget;   
  12.                
  13.              private var logger:ILogger;   
  14.            
  15.              private function init():void{   
  16.                  var logTarget:TraceTarget = new TraceTarget();   
  17.                  logger = new LogLogger("xx");   
  18.                  logTarget.filters = ["main"];   
  19.                  logTarget.level = LogEventLevel.ALL;   
  20.                  logTarget.includeCategory = true;   
  21.                  logTarget.includeDate = true;   
  22.                  logTarget.includeLevel = true;   
  23.                  logTarget.includeTime = true;   
  24.                  logTarget.addLogger(logger);   
  25.                  Log.addTarget(logTarget);   
  26.                  //trace("create complete");   
  27.              }   
  28.              private function clickBtn():void{   
  29.                  //var logger:LogLogger = new LogLogger("*");   
  30.                    
  31.                  logger.debug("It's the debug");   
  32.                  logger.info("It's the info");   
  33.                  logger.warn("It's the warn");   
  34.                  logger.error("It's the error");   
  35.                  logger.fatal("It's the fatal");   
  36.                  //trace("It's the first click on btn");   
  37.              }   
  38.          ]]>   
  39.      </mx:Script>   
  40.        
  41.      <mx:Button id="btn" label="OK" click="clickBtn()"/>   
  42.        
  43. </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中)

Xml代码
  1. <logging>  
  2.       <target class="flex.messaging.log.ConsoleTarget" level="Debug">  
  3.            <properties>  
  4.                 <prefix>[BlazeDS]</prefix>  
  5.                 <includeDate>false</includeDate>  
  6.                 <includeTime>false</includeTime>  
  7.                 <includeLevel>false</includeLevel>  
  8.                 <includeCategory>false</includeCategory>  
  9.            </properties>  
  10.           <filters>  
  11.                 <pattern>Endpoint.*</pattern>  
  12.                 <pattern>Service.*</pattern>  
  13.                 <pattern>Configuration</pattern>  
  14.          </filters>  
  15.       </target>  
  16. </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所属的分类。

Flex代码
  1. 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开发人员学习与理解。


转自:http://www.cnblogs.com/yunshangqingfeng/archive/2010/02/26/1674136

相关问答

更多
  • 什么是flex?[2022-03-10]

    flex是一种技术,也可以说是一种框架,就是做网页特效的
  • 什么是AS和flex[2023-07-12]

    AS(ActionScript) 动作脚本是遵循 ECMAscript第四版 的 Adobe Flash Player 运行时环境的编程语言。它在 Flash 内容和应用程序中实现交互性、数据处理、以及其他功能。 flex通常是指Adobe Flex,是最初由Macromedia公司在2004年3月发布的,基于其专有的Macromedia Flash平台,它是涵盖了支持RIA(Rich Internet Applications)的开发和部署的一系列技术组合。Flex 是一个高效、免费的开放源框架,可用于构 ...
  • 一种可能的解决方案是,使用mx.logging.Log来调试跟踪,使用自定义LogTarget来捕获它们。 它会像这样。 声明必要的对象: private var logTarget:MyLogTarget= new MyLogTarget(); private var myLog:ILogger; 设置初始化应用程序的日志记录: myLog=Log.getLogger("MyApp"); Log.addTarget(logTarget); 记录东西: myLog.info("Something s ...
  • 我没有时间来解决这个问题,但在调试时有一些建议: 首先,我会跟踪HTTPRequest的开始标记中的所有内容。
  • 我不确定“Firefox和IE的Flash调试器”是什么意思; 你是指这里提供的Flash Player的调试版吗? http://www.adobe.com/support/flashplayer/downloads.html 如果没有,您肯定需要安装那些,以便能够将trace()输出写入IDE控制台。 This is actually a combination of issues. The minor issue was that one application was interfering wit ...
  • (F)Lex不会在输入中搜索匹配项。 它会尝试当前输入位置的所有模式,并选择与大多数文本匹配的模式,如果多于一个匹配相同数量的文本,则选择最早的模式。 下一个lex匹配将从前一个匹配开始。 .*匹配线的其余部分。 \bstate\b只匹配七个字符。 所以.*会赢。 但是\bstate\b实际上并不匹配,因为这是lex,而不是<在这里插入你最喜欢的正则表达式语法>而\b表示退格,就像在C程序中一样。 字母L被引号覆盖的原因可能是您的输入文件是在Windows上创建的,并且在行尾有\ r \ n。 .*将匹配包 ...
  • 词法分析器无法识别空格,因此它使用带有指定字符串的规则。 插入此规则: [ \t\n\r] printf("%s", yytext); 但是,根据文档 ,它将解决最长匹配的模糊性,这仍然是“再见”规则。 Your lexer doesn't recognize whitespaces so it uses the rule with the specified string. Insert this rule: [ \t\n\r] printf("%s", yytext); However, accor ...
  • 我怀疑在识别的注释或符号后面的换行符序列的一部分被捕获到yytext中,因此在调试跟踪中回显。 尝试将\ r添加到字符类,如下所示: SYMBOLS [a-zA-Z][^\,\.\"\(\) \n\t\r]* COMMENTS ";"[^\n\r]* 无论如何,您可能希望将调试输出传递到文件中,以便可以使用od之类的工具以字符方式检查它。 I suspect that part of the the newline sequence that follows the reco ...
  • 我从来没用过它,但它似乎是你想要的: log4as3 I never used it, but it seems to be what you want: log4as3