Flex桌面AIR软件日志添加

日志包装类

package log
{
    import com.adobe.air.logging.FileTarget;

    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;

    import mx.logging.ILogger;
    import mx.logging.Log;
    import mx.logging.LogEventLevel;

    /**
     *  AIR程序中记录日志的工具类 (对as3corelib官方类库中类com.adobe.air.logging.FileTarget的封装)
     *
     *  @author Sakura
     *  博客地址:http://www.cnblogs.com/god_bless_you
     */
    public final class LogUtil
    {
        private static var _fileTarget:FileTarget;

        private static var _level:int = LogEventLevel.ALL;

        /**
         *  Provides access to the level this target is currently set at.
         *  Value values are:
         *    <ul>
         *      <li><code>LogEventLevel.FATAL (1000)</code> designates events that are very
         *      harmful and will eventually lead to application failure</li>
         *
         *      <li><code>LogEventLevel.ERROR (8)</code> designates error events that might
         *      still allow the application to continue running.</li>
         *
         *      <li><code>LogEventLevel.WARN (6)</code> designates events that could be
         *      harmful to the application operation</li>
         *
         *      <li><code>LogEventLevel.INFO (4)</code> designates informational messages
         *      that highlight the progress of the application at
         *      coarse-grained level.</li>
         *
         *      <li><code>LogEventLevel.DEBUG (2)</code> designates informational
         *      level messages that are fine grained and most helpful when
         *      debugging an application.</li>
         *
         *      <li><code>LogEventLevel.ALL (0)</code> intended to force a target to
         *      process all messages.</li>
         *    </ul>
         *
         */
        public static function get level():int
        {
             return _level;
        }

        public static function set level(value:int):void
        {
            // A change of level may impact the target level for Log.
            if (checkFileTarget())
            {
                Log.removeTarget(_fileTarget);
                _fileTarget = null;
                _level = value;
            }
            initLogging();
        }

        private static var _logFilePath:String = File.applicationDirectory.resolvePath( "Logs\\main.log" ).nativePath;
//        File.applicationDirectory
        public static function get logFilePath():String
        {
            return _logFilePath;
        }

        public static function set logFilePath(value:String):void
        {
            if (checkFileTarget())
            {
                Log.removeTarget(_fileTarget);
                _fileTarget = null;
                _logFilePath = value;
            }
            initLogging();
        }

        private static function getLogger(category:String):ILogger
        {
            return Log.getLogger(category);
        }

        private static function initLogging():void
        {
            if (checkFileTarget())
                return;
                trace("logpath:"+_logFilePath);
            var file:File = new File(_logFilePath);
            var filestrm:FileStream = new FileStream;
            filestrm.open(file,FileMode.WRITE);
            filestrm.writeInt(0);
            filestrm.close();
            trace("清除日志"+file.exists+""+file.nativePath);

            _fileTarget = new FileTarget(file);
            _fileTarget.level = _level;
            _fileTarget.includeDate = true;
            _fileTarget.includeTime = true;
            _fileTarget.includeCategory = true;
            _fileTarget.includeLevel = true;                

            Log.addTarget(_fileTarget);
        }

        private static function checkFileTarget():Boolean
        {
            return (_fileTarget) ? true : false;
        }

        private static function generateMessage(message:String,rest:Array):String
        {
            for (var i:int = 0; i < rest.length; i++)
            {
                message = message.replace(new RegExp("\\{"+i+"\\}", "g"), rest[i]);
            }
            return message;
        }

        /**
          *  Logs the specified data using the <code>LogEventLevel.DEBUG</code>
          *  level.
         *  <code>LogEventLevel.DEBUG</code> designates informational level
         *  messages that are fine grained and most helpful when debugging
         *  an application.
         *
         *  <p>The string specified for logging can contain braces with an index
         *  indicating which additional parameter should be inserted
         *  into the string before it is logged.
         *  For example "the first additional parameter was {0} the second was {1}"
         *  will be translated into "the first additional parameter was 10 the
         *  second was 15" when called with 10 and 15 as parameters.</p>
         *
         *  @param category The category for which this log sends messages.
         *
         *  @param message The information to log.
         *  This string can contain special marker characters of the form {x},
         *  where x is a zero based index that will be replaced with
         *  the additional parameters found at that index if specified.
         *
         *  @param rest Additional parameters that can be subsituted in the str
         *  parameter at each "{<code>x</code>}" location, where <code>x</code>
         *  is an integer (zero based) index value into the Array of values
         *  specified.
         *
         */
        public static function debug(category:String, message:String, ... rest):void
        {
            initLogging();

            if (Log.isDebug())
            {
                var logger:ILogger = getLogger(category);
                logger.debug(generateMessage(message,rest));
            }
        }

        /**
          *  Logs the specified data using the <code>LogEvent.INFO</code> level.
          *  <code>LogEventLevel.INFO</code> designates informational messages that
          *  highlight the progress of the application at coarse-grained level.
          *
          *  <p>The string specified for logging can contain braces with an index
          *  indicating which additional parameter should be inserted
          *  into the string before it is logged.
         *  For example "the first additional parameter was {0} the second was {1}"
         *  will be translated into "the first additional parameter was 10 the
         *  second was 15" when called with 10 and 15 as parameters.</p>
         *
         *  @param category The category for which this log sends messages.
         *
         *  @param message The information to log.
         *  This String can contain special marker characters of the form {x},
         *  where x is a zero based index that will be replaced with
         *  the additional parameters found at that index if specified.
         *
         *  @param rest Additional parameters that can be subsituted in the str
         *  parameter at each "{<code>x</code>}" location, where <code>x</code>
         *  is an integer (zero based) index value into the Array of values
         *  specified.
         *
         */
        public static function info(category:String, message:String, ... rest):void
        {
            initLogging();

            if (Log.isInfo())
            {
                var logger:ILogger = getLogger(category);
                logger.info(generateMessage(message,rest));
            }
        }

        /**
         *  Logs the specified data using the <code>LogEventLevel.WARN</code> level.
         *  <code>LogEventLevel.WARN</code> designates events that could be harmful
         *  to the application operation.
         *
         *  <p>The string specified for logging can contain braces with an index
         *  indicating which additional parameter should be inserted
         *  into the string before it is logged.
         *  For example "the first additional parameter was {0} the second was {1}"
         *  will be translated into "the first additional parameter was 10 the
         *  second was 15" when called with 10 and 15 as parameters.</p>
         *
         *  @param category The category for which this log sends messages.
         *
         *  @param message The information to log.
         *  This String can contain special marker characters of the form {x},
         *  where x is a zero based index that will be replaced with
         *  the additional parameters found at that index if specified.
         *
         *  @param rest Aadditional parameters that can be subsituted in the str
         *  parameter at each "{<code>x</code>}" location, where <code>x</code>
         *  is an integer (zero based) index value into the Array of values
         *  specified.
         *
         */
        public static function warn(category:String, message:String, ... rest):void
        {
            initLogging();

            if (Log.isWarn())
            {
                var logger:ILogger = getLogger(category);
                logger.warn(generateMessage(message,rest));
            }
        }

        /**
         *  Logs the specified data using the <code>LogEventLevel.ERROR</code>
         *  level.
         *  <code>LogEventLevel.ERROR</code> designates error events
         *  that might still allow the application to continue running.
         *
         *  <p>The string specified for logging can contain braces with an index
         *  indicating which additional parameter should be inserted
         *  into the string before it is logged.
         *  For example "the first additional parameter was {0} the second was {1}"
         *  will be translated into "the first additional parameter was 10 the
         *  second was 15" when called with 10 and 15 as parameters.</p>
         *
         *  @param category The category for which this log sends messages.
         *
         *  @param message The information to log.
         *  This String can contain special marker characters of the form {x},
         *  where x is a zero based index that will be replaced with
         *  the additional parameters found at that index if specified.
         *
         *  @param rest Additional parameters that can be subsituted in the str
         *  parameter at each "{<code>x</code>}" location, where <code>x</code>
         *  is an integer (zero based) index value into the Array of values
         *  specified.
         *
         */
        public static function error(category:String, message:String, ... rest):void
        {
            initLogging();

            if (Log.isError())
            {
                var logger:ILogger = getLogger(category);
                logger.error(generateMessage(message,rest));
            }
        }

        /**
         *  Logs the specified data using the <code>LogEventLevel.FATAL</code>
         *  level.
         *  <code>LogEventLevel.FATAL</code> designates events that are very
         *  harmful and will eventually lead to application failure
         *
         *  <p>The string specified for logging can contain braces with an index
         *  indicating which additional parameter should be inserted
         *  into the string before it is logged.
         *  For example "the first additional parameter was {0} the second was {1}"
         *  will be translated into "the first additional parameter was 10 the
         *  second was 15" when called with 10 and 15 as parameters.</p>
         *
         *  @param category The category for which this log sends messages.
         *
         *  @param message The information to log.
         *  This String can contain special marker characters of the form {x},
         *  where x is a zero based index that will be replaced with
         *  the additional parameters found at that index if specified.
         *
         *  @param rest Additional parameters that can be subsituted in the str
         *  parameter at each "{<code>x</code>}" location, where <code>x</code>
         *  is an integer (zero based) index value into the Array of values
         *  specified.
         *
         */
        public static function fatal(category:String, message:String, ... rest):void
        {
            initLogging();

            if (Log.isFatal())
            {
                var logger:ILogger = getLogger(category);
                logger.fatal(generateMessage(message,rest));
            }
        }

        /**
         *  Clear all logs.
         */
        public static function clear():void
        {
            initLogging();
            _fileTarget.clear();
        }
    }
}

需要依赖:as3corelib库

时间: 2024-10-11 06:07:06

Flex桌面AIR软件日志添加的相关文章

Dell 桌面虚拟化软件vWorkspace8.6一次简单过程

可能很多人不知道目前的通用的桌面虚拟化软件除了VMware view,Xendesktop,Mircsoft外还有dell的桌面虚拟化产品vWorkspace,目前的版本已经到8.6,在了解这款产品长啥样子之前,我们先了解下如何将dell的桌面虚拟化产品给安装起来. 和其他的桌面虚拟化产品一样,也都需要宿主机,也都需要底层虚拟化软件,也同样需要win7的摸板,同样需要域,dhch,dns,这里假设域,dhcp.dns都已经有了.看下面的步骤: 一.Windowsserver 2012 R2主机需

给日志添加&ldquo;复制&rdquo;效果

给日志添加如上效果的实现方法: 在日志编辑页面,源代码中,添加如下代码,包裹住 目标内容style1: <div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"></span></div>                     要复制的内容here-</d

用户使用双屏扩展桌面类软件时,所遇到的疑难问题目录汇编

在使用行易软件双屏类软件时,在软件启动之前,往往需要进行一下前期设置工作.这些设置工作做正确了,软件启动后,其相应的展示功能才能真正的运行实施. 双屏软件包括:活动现场双屏管理系统-多线程抽奖版,婚庆现场管理系统-双屏版,PPT图片双屏抽奖系统,评委计分系统-双屏版,比赛打分展示双屏管理系统,知识竞赛现场管理系统-双屏PPT版等. 软件使用过程中,有时也会遇到一些问题.我们把这些常见的疑难问题,汇总成目录,供大家学习: 1. 双屏前期设置方法[又名:系统扩展桌面设置方法-双屏双显扩展桌面技术前期

Remote Desktop Organizer远程桌面管理软件的基本使用和介绍

<Remote Desktop Organizer>是一款用于远程桌面管理的软件.软件支持windows平台运行. Remote Desktop Organizer 是一款 Windows 远程桌面管理软件,让你在同一个窗口内浏览到多个远程桌面的信息,方便 Windows 远程管理. 软件信息 名称:Remote Desktop Organizer 版本:1.4.7 下载:http://www.softpedia.com/get/Internet/Remote-Utils/Remote-Des

自定义flex组件使用标签方式添加子组件

一般情况下,当我们写了一个继承自flex组件并往里面添加了子组件,然后想用标签等方式添加子组件时候报错了,那如何解决这一的问题,自定义组件代码如下: <?xml version="1.0" encoding="utf-8"?> <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300">

飞雪桌面日历软件 V8.6 免费绿色版

软件名称: 飞雪桌面日历软件软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP / Win2008软件大小: 4MB图片预览: 软件简介:集合超多功能:日历.月历.世界时钟.倒计时时钟.定时关机.备忘录. 便签.节日生日定制.系统热键.光驱控制.网络校时.壁纸切换.邮件检查.语音报时.网络收音机 .天气预报.MD5计算等. 内含从公元1580-5000年的阴阳历,可查寻含阳历.阴历.天干地支(含2种算法).二十四节气.九九三伏.月相变

杨泽业:linux远程桌面连接软件xshell和ftp软件xftp安装图文教程

在linux系统中,Xshell和Xftp配合使用,是一套非常方便的linux服务器管理软件. xshell是通过输入命令来对服务器进行操作,如进入目录,压缩文件,解压文件,启动服务等等. Xftp是可视化工具,功能和FlashFXP差不多,可以上传,下载,移动文件等操作. 第一,下载Xshell和Xftp.关注[杨泽业建站]微信公众号回复Xshell或者Xftp即可下载全套软件. 第二,开始先安装Xshell.安装xshell,很简单,基本上点击next直至完成即可: 1.右键以管理员身份运行

deb包+软件图标+添加到系统菜单+举例安装卸载

本文介绍的内容和实验一下: 1. 制造deb包.2. 为了使软件图标.3. 开始菜单中添加到系统中的软件:4. 安装和卸载制作的deb包. 1. 制作deb包 制作deb包的方法可能有多种,本文使用的是dh_make工具. 能够參考文章:http://www.linuxidc.com/Linux/2011-02/32714.htm . 文章给出了制作的过程,可是没有详细的细节. 本文制作了一个deb规范的源代码包,这里不再赘述制作过程,能够下载该源代码包查看(http://download.cs

c#桌面应用程序如何添加弹出式广告

c#写的软件很多,如何添加诸如像搜狗输入法软件与灵格斯翻译软件的屏幕右下角弹出式广告呢. c#代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.