Flex AIR程序日志工具类LogUtil

Flex默认集成了一个功能齐全的Logging框架,它位于mx.logging包下。as3corelib类库中的类com.adobe.air.logging.FileTarget对Flex logging进行了一定的封装,可以在AIR程序中自由设置日志文件的存储路径以及添加了个清除所有日志的功能。由于实际项目的需求,在FileTarget类的基础上,我进行了再封装。新封装的LogUtils工具类不仅可以控制日志输出的目的地,还可以控制日至输出级别,便于调试和发布。

鉴于对工具类使用的讲解,简要说明下官方Logging框架。详细信息请参阅官方文档:http://livedocs.adobe.com/flex/3/html/help.html?content=logging_09.html

The logging API 由下面几部分组成:

  1. Logger:在日志记录框架中使用的记录程序, 提供了接口发送log到一个特定的target,它实现了ILogger接口。
  2. Log target:定义了日志将会被写到哪里。Flex提供了最常用的日志目标TraceTarget。 This log target connects the logging API to the trace system so that log messages are sent to the same location as the output of the trace() method.
  3. Destination: The destination is where the log message is written. Typically, this is a file, but it can also be a console or something else, such as an in-memory object. The default destination for TraceTarget is the flashlog.txt file. You configure this destination on the client.

LogUtils工具类使用示例:

  LogDemo

日志输出:

7/29/2011 10:43:54.292 [INFO] LogDemo preinitializeHandler

7/29/2011 10:43:54.292 [DEBUG] LogDemo preinitializeHandler

7/29/2011 10:43:54.292 [INFO] LogDemo initializeHandler

7/29/2011 10:43:54.292 [DEBUG] LogDemo initializeHandler

7/29/2011 10:43:54.370 [INFO] LogDemo creationCompleteHandler

7/29/2011 10:43:54.370 [DEBUG] LogDemo creationCompleteHandler

7/29/2011 10:43:54.385 [INFO] LogDemo addedToStageHandler

7/29/2011 10:43:54.385 [DEBUG] LogDemo addedToStageHandler

7/29/2011 10:43:54.385 [INFO] LogDemo 请跟我说:我是一个优秀的MT

注: Log target默认的filters为"*",故日志文件可能会打出下面包的类中的日志记录

mx.rpc.*
mx.messaging.*
mx.data.*

将日志级别 设置为LogEventLevel.ERROR以上,上述包中的日志就基本看不到了

LogUtil下载地址:https://files.cnblogs.com/god_bless_you/LogUtil.rar

转载请表明出处:http://www.cnblogs.com/god_bless_you

posted on 2011-07-29 11:41  God bless you  阅读(2028)  评论(0编辑  收藏  举报

导航