`

Flex日志管理---Logging API

    博客分类:
  • FLEX
阅读更多
用过java的日志框架log4j之后,你就会被它方便而又强大的功能所吸引。我们不仅可以控制日志输出的目的地,还可以控制日至输出级别,便于调试和发布。
    其实在Flex 中也提供了这样的一个框架,Logging API就是最基本的日志控制框架,只不过大部分的人都在用最简单的trace()函数罢了。
    Logging API不仅提供了最基本的trace功能,还提供了log target,也就是输出的方式。还提供了destination目的地的配置功能。通过我们对log的级别控制我们可以输 出一些普通信息而过滤掉debug的信息。除此之外还可以进行自定义log target,对框架进行扩展。

重要概念和类介绍:

Logger : 提供了接口发送log到一个特定的target,它实现了ILogger接口。
Log target :定义了日志将会被写道哪里。Flex 提供了两类target,TraceTarget和MiniDebugTarget。TraceTarget就是将log输出到trace()函数输出的文件中,也就是flashlog.txt文件中。当然你也可以自定义一个log target。
Logging Level :定义了当前系统可输出的日志级别。

如下表所示,按照高到低排列。如果现在的level是ALL,那么系统中所有的日志都会被输出。如果是INFO,那么高于INFO的DEBUG信息就不会被输出。这个很容易理解。

Logging level constant (int) Description
ALL (0)   Designates that messages of all logging levels should be logged.
DEBUG (2) Logs internal Flex activities. This is most useful when debugging anapplication.Select the DEBUG logging level to include DEBUG, INFO, WARN, ERROR, and FATAL messages in your log files.
INFO (4)  Logs general information.Select the INFO logging level to include INFO, WARN, ERROR, and FATAL messages in your log files.
WARN (6)   Logs a message when the application encounters a problem. These problems do not cause the application to stop running, but could lead to further errors.Select the WARN logging level to include WARN, ERROR, and FATAL messages in your log files.
ERROR(8)    Logs a message when a critical service is not available or a situation has occurred that restricts the use of the application. Select the ERROR logging level to include ERROR and FATAL messages in your log files.
FATAL (1000)  Logs a message when an event occurs that results in the failure of the application. Select the FATAL logging level to include only FATAL messages in your log files.

log target 过滤filters
logTarget.filters=["mx.rpc.*","mx.messaging.*"];
这个例子中就是指定了我们要输出日志的类和包。只有在mx.rpc和mx.messaging包下的类才能输出log,忽略其他的。也可以是mxml形式:

<mx:TraceTarget id="logTarget" includeDate="true" includeTime="true"
                  includeCategory="true" includeLevel="true">
            <mx:filters>
                  <mx:Array>
                        <mx:String>mx.rpc.*</mx:String>
                        <mx:String>mx.messaging.*</mx:String>
                  </mx:Array>
            </mx:filters>
           <!-- 0 is represents the LogEventLevel.ALL constant. -->
           <mx:level>0</mx:level>
</mx:TraceTarget>

参数介绍
includeDate="true",输出的log带日期
includeTime="true", 输出的log带时间
includeCategory="true",输出的log带分类信息,也就是哪个类或者控件输出的log
includeLevel="true",输出的log是否带level信息,如[INFO],[DEBUG]等。

来个实用的小例子

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml " layout="absolute" initialize="initLog()">
<mx:Script>
  <![CDATA[
   import mx.logging.*;
   import mx.logging.targets.*;
   private var myLogger : ILogger;
   public function printLog(level:Number):void
   {
    if(level ==2)
     myLogger.debug("This is debug click");
    if(level == 4)
     myLogger.info("This is info click");
    if(level == 6)
     myLogger.warn("This is warn click");
    if(level ==
     myLogger.error("This is error click");
    if(level ==1000)
     myLogger.fatal("This is fatal click");
   }
   private function initLog():void{
    /*
    // Create a target.
    var logTarget:TraceTarget = new TraceTarget();
    // Log only messages for the classes in the mx.rpc.* and
    // mx.messaging packages.
    logTarget.filters=["*"];
    // Log all log levels.
    logTarget.level = LogEventLevel.ALL;
    // Add date, time, category, and log level to the output.
    logTarget.includeDate = true;
    logTarget.includeTime = true;
    logTarget.includeCategory = true;
    logTarget.includeLevel = true;
    // Begin logging.
    Log.addTarget(logTarget);
    */
    myLogger = Log.getLogger("myCustomClass");
   }
  ]]>
</mx:Script>
<mx:TraceTarget level="4" includeDate="true" includeTime="true"
  includeCategory="true" includeLevel="true">
  <mx:filters>
   <mx:Array>
    <mx:String>*</mx:String>
   </mx:Array>
  </mx:filters>
</mx:TraceTarget>
<mx:VBox width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
  <mx:Button label="DEBUG(2)" click="printLog(2)"/>
  <mx:Button label="INFO(4)" click="printLog(4)"/>
  <mx:Button label="WARN(6)" click="printLog(6)"/>
  <mx:Button label="ERROR(8)" click="printLog(8)"/>
  <mx:Button label="FATAL(1000)" click="printLog(1000)"/>

</mx:VBox>

</mx:Application>

本文转自:http://www.iteye.com/topic/392808




本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/meteorlWJ/archive/2009/06/23/4287807.aspx
分享到:
评论

相关推荐

    commons-logging.jar

    commons-logging-api-99.0-does-not-exist.jar, commons-logging-api.jar, commons-logging-java1.1.jar, commons-logging-optional.jar, commons-logging-osgi-1.0.jar, commons-logging-osgi.jar, commons-logging...

    commons-logging-1.2-API文档-中英对照版.zip

    包含翻译后的API文档:commons-logging-1.2-javadoc-API文档-中文(简体)-英语-对照版.zip 对应Maven信息:groupId:commons-logging,artifactId:commons-logging,version:1.2 使用方法:解压翻译后的API文档,...

    commons-logging-1.1.1-API文档-中文版.zip

    包含翻译后的API文档:commons-logging-1.1.1-javadoc-API文档-中文(简体)版.zip 对应Maven信息:groupId:commons-logging,artifactId:commons-logging,version:1.1.1 使用方法:解压翻译后的API文档,用...

    pax-logging-api-1.7.3.jar,pax-logging-service-1.7.3.jar

    pax-logging-api-1.7.3.jar,pax-logging-service-1.7.3.jar,pax-confman-propsloader-0.2.3.jar 在system.properties中加入bundles.configuration.location=conf conf\services\org.ops4j.pax.logging.properties ...

    jboss-logging-3.3.2.Final-API文档-中文版.zip

    包含翻译后的API文档:jboss-logging-3.3.2.Final-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.jboss.logging:jboss-logging:3.3.2.Final; 标签:jboss、logging、jar包、java、API文档、中文版; 使用方法...

    jboss-logging-3.4.1.Final-API文档-中文版.zip

    包含翻译后的API文档:jboss-logging-3.4.1.Final-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.jboss.logging:jboss-logging:3.4.1.Final; 标签:jboss、logging、中文文档、jar包、java; 使用方法:解压...

    commons-logging-1.2-API文档-中文版.zip

    包含翻译后的API文档:commons-logging-1.2-javadoc-API文档-中文(简体)版.zip 对应Maven信息:groupId:commons-logging,artifactId:commons-logging,version:1.2 使用方法:解压翻译后的API文档,用浏览器...

    commons-logging-1.1.3-API文档-中英对照版 (2).zip

    包含翻译后的API文档:commons-logging-1.1.3-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:commons-logging:commons-logging:1.1.3; 标签:commons、logging、中英对照文档、jar包、java; 使用方法...

    jboss-logging-3.4.3.Final-API文档-中文版.zip

    包含翻译后的API文档:jboss-logging-3.4.3.Final-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.jboss.logging:jboss-logging:3.4.3.Final; 标签:jboss、logging、jar包、java、中文文档; 使用方法:解压...

    pax-logging-api-1.7.3.jar

    pax-logging-api-1.7.3

    commons-logging-1.1.3-API文档-中文版.zip

    包含翻译后的API文档:commons-logging-1.1.3-javadoc-API文档-中文(简体)版.zip; Maven坐标:commons-logging:commons-logging:1.1.3; 标签:commons、logging、中文文档、jar包、java; 使用方法:解压翻译后的...

    jboss-logging-3.4.3.Final-API文档-中英对照版.zip

    包含翻译后的API文档:jboss-logging-3.4.3.Final-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:org.jboss.logging:jboss-logging:3.4.3.Final; 标签:jboss、logging、jar包、java、中英对照文档; ...

    APRESS--Logging-in-Java-with-the-JDK-1_4-Logging-API-and-Apache-log4j

    APRESS--Logging-in-Java-with-the-JDK-1_4-Logging-API-and-Apache-log4j

    commons-logging-api.jar

    commons-logging-api.jar

    jboss-logging-3.3.2.Final-API文档-中英对照版.zip

    包含翻译后的API文档:jboss-logging-3.3.2.Final-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:org.jboss.logging:jboss-logging:3.3.2.Final; 标签:jboss、logging、jar包、java、API文档、中英...

    jboss-logging-3.4.2.Final-API文档-中文版.zip

    包含翻译后的API文档:jboss-logging-3.4.2.Final-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.jboss.logging:jboss-logging:3.4.2.Final; 标签:jboss、logging、中文文档、jar包、java; 使用方法:解压...

    commons-logging-1.1.3.jar

    common-logging是apache提供的一个通用的日志接口。用户可以自由选择第三方的日志组件作为具体实现,像log4j,或者jdk自带的logging, common-logging会通过动态查找的机制,在程序运行时自动找出真正使用的日志库。...

    commons-logging-1.1.1-API文档-中英对照版.zip

    包含翻译后的API文档:commons-logging-1.1.1-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:commons-logging:commons-logging:1.1.1; 标签:logging、commons、jar包、java、中英对照文档; 使用方法...

    开发工具 commons-logging-1.2

    开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发...

Global site tag (gtag.js) - Google Analytics