flex自定义Log的输出,重写隐藏方法

来源:互联网 发布:阿里云app有域名交易吗 编辑:程序博客网 时间:2024/05/18 02:00

flex自带了mx.logging包,跟log4j很像。基本用法挺简单不讲了。

flex同时还实现了两种log的输出方式,在mx.logging.targets包,分别是MiniDebugTarget和TraceTarget(输出到LocalConnection和trace),可是有时候不想用这两种,比如想输出到TextArea显示。注意到这两个都是继承自LineFormattedTarget,看看这类忍不住有点激动了,因为它做了大部分的字符串格式化操作,直接拿来继承就行了。从文档上看它只有logEvent可以重写,而且又不大方便(因为LogEvent里的message不是格式化好的,要了有啥意思)。翻看源码才知道它有一个方法是mx_internal命名空间的,即隐藏的。(不过可以重写)

    /**     *  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    }

重写它就显得很简单,看看TraceTarget的源码也是重写它的。下面是自定义输出类

package myvocal.log{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 functionsuper();this.level = LogEventLevel.INFO;this.includeDate = true;this.includeLevel = true;this.includeTime = true; _textArea = tarea;}  private var _textArea:TextArea; mx_internal override function internalLog(message:String):void{_textArea.text += message + "/n";} }}
原创粉丝点击