用过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 == 8)
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://wmcai.blog.163.com/blog/static/48024200711613340390/
在as文件中用如下方法来调用和定义。
1.在mvc框架中定一个全局的_target
- package com.alcor.web.model
- {
- import com.adobe.cairngorm.model.ModelLocator;
- import mx.logging.Log;
- import mx.logging.targets.TraceTarget;
- import mx.logging.LogEventLevel
- public class LoggerLocator implements ModelLocator
- {
- private static var loggerLocator:LoggerLocator;
- public static var _target:TraceTarget;
- //获得类的实例
- public static function getInstance() : LoggerLocator
- {
- if ( loggerLocator == null )
- {
- loggerLocator = new LoggerLocator();
- _target = new TraceTarget( ); //创建一个trace target
- _target.includeDate = true; //显示日期
- _target.includeTime = true; //显示trace的时间
- _target.includeLevel = true; // 显示级别(error, debug, info .....)
- _target.includeCategory = true; //显示种类
- _target.filters = ["com.alcor.*" ]; //只显示com.alcor.*包里类trace出的信息
- _target.level = LogEventLevel.ALL //显示info()信息,debug()啊error()啊等等的所有信息...
- }
- return loggerLocator;
- }
- public function LoggerLocator()//构造函数
- {
- if ( loggerLocator != null )
- {
- throw new Error( "Only one instance should be instantiated" );
- }
- }
- }
- }
- //利用logging 来显示logger
- Log.addTarget(LoggerLocator._target); //配置好target了把它填到Log里
- Log.getLogger("com.alcor.web.commands.user.UserLoginCommand").debug("登录成功");