关于Log等级,有5种,他们之间的关系可以理解为:
调试(DEBUG):包含调试(DEBUG)、信息(INFO)、警告(WARN)、错误(ERROR)、致命错误(FATAL)
信息(INFO):包含信息(INFO)、警告(WARN)、错误(ERROR)、致命错误(FATAL)
警告(WARN):包含警告(WARN)、错误(ERROR)、致命错误(FATAL)
错误(ERROR):包含错误(ERROR)、致命错误(FATAL)
致命错误(FATAL):只有他自己
这样,如果log4j.rootLogger = INFO,那么 INFO,WARN,ERROR,FATAL就全部会被输出
如果log4j.rootLogger = ERROR,那么ERROR,FATAL就全部会被输出
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
initialize="initializeHandler( )">
<mx:Script>
<![CDATA[
import mx.logging.Log;
import mx.logging.targets.TraceTarget;
import mx.logging.LogEventLevel
// private var _target:TraceTarget;
private var _target:LogTarget;
private function initializeHandler( ):void
{
// _target = new TraceTarget(t1);
_target = new LogTarget(t1);
_target.includeDate = true;
_target.includeTime = true;
_target.includeLevel = true;
_target.includeCategory = true;
//Only shows message in this package.
_target.filters = ["net.nshen.logging.*" ];
_target.level = LogEventLevel.INFO;
Log.addTarget(_target);
}
private function sendToLog( ):void
{
Log.getLogger("net.nshen.logging.MainClass").
info("This info will be shown.");
Log.getLogger("com.adobe.MainClass").
info("This will not be shown due to the unmatched package.");
Log.getLogger("net.nshen.logging.MainClass").
debug("This is not an info, so it will not be shown.");
}
]]>
</mx:Script>
<mx:Button click="sendToLog( )" label="Log Message"/>
<mx:TextArea id="t1" width="306" height="154"/>
</mx:Application>
package
{
import mx.controls.TextArea;
import mx.core.mx_internal;
import mx.logging.targets.LineFormattedTarget;
import mx.logging.LogEventLevel;
use namespace mx_internal;
public class LogTarget extends LineFormattedTarget
{
public function LogTarget(tarea:TextArea)
{
//TODO: implement function
super();
this.level = LogEventLevel.INFO;
this.includeDate = true;
this.includeLevel = true;
this.includeTime = true;
_textArea = tarea;
}
private var _textArea:TextArea;
/*
Descendants of this class should override this method to direct the
specified message to the desired output.
@param message String containing preprocessed log message which may
include time, date, category, etc. based on property settings,
such as <code>includeDate</code>, <code>includeCategory</code>,
etc.
mx_internal function internalLog(message:String):void
{
// override this method to perform the redirection to the desired output
}
*/
mx_internal override function internalLog(message:String):void
{
_textArea.text += message + "\n";
}
}
}