日志包装类

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();
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 = ; 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库

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

  1. Atitit 全屏模式的cs桌面客户端软件gui h5解决方案 Kiosk模式

    Atitit 全屏模式的cs桌面客户端软件gui h5解决方案 Kiosk模式 1.1. Kiosk Software广泛用于公共电脑或者嵌入系统,最常用的就是ATM机.自动服务机之类的系统了.,1 ...

  2. 给日志添加“复制”效果

    给日志添加如上效果的实现方法: 在日志编辑页面,源代码中,添加如下代码,包裹住 目标内容style1: <div class="cnblogs_code"><di ...

  3. c#桌面小软件

    这是以前练习时用c#做的桌面小软件,今天回顾下. 这是设计界面 可以看出该程序能够播放网络歌曲及浏览新闻. 实现:歌曲来源百度API,播放WindowsMediaPlayer api地址:string ...

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

    <Remote Desktop Organizer>是一款用于远程桌面管理的软件.软件支持windows平台运行. Remote Desktop Organizer 是一款 Windows ...

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

    软件名称: 飞雪桌面日历软件软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP / Win2008软件大小: 4MB图片预览: 软件简介: ...

  6. Win7系统桌面便签怎么添加?

    参考:http://jingyan.baidu.com/article/ab69b270c207432ca7189f99.html Win7系统桌面便签怎么添加?有时候工作.学习忙起来就会忘记要办的事 ...

  7. 火狐删除配置文件 会删除目录下所有文件 切记不要把配置文件建立在桌面 恢复软件:易我数据恢复向导 9.0 DiskGenius500

    火狐删除配置文件 会删除目录下所有文件 切记不要把配置文件建立在桌面 恢复软件:易我数据恢复向导 9.0  DiskGenius500 结果:由于时间比较常 恢复文件均失败了~

  8. Python3的日志添加功能

    python日志添加功能,主要记录程序运行中的日志,统一收集并分析 一.日志的级别 debug(调试信息) info() warning(警告信息)error(错误信息) critical(致命信息) ...

  9. 强烈推荐一个和朋友远程桌面的软件teamviewer

    强烈推荐一个和朋友远程桌面的软件teamviewer个人认为:贼溜

随机推荐

  1. http://my.oschina.net/u/1177710/blog/284608

    http://my.oschina.net/u/1177710/blog/284608 http://chuhanzhi.com/?p=45 http://www.2cto.com/kf/201501 ...

  2. 分析apache日志,统计ip访问频次命令

    统计访问频次最高的10个ip: cat /var/log/httpd/access_log |awk '{print $1}'|sort|uniq -c|sort -nr|head -10 统计恶意i ...

  3. 深入理解C#中的泛型(一)

    为什么要有泛型? 请大家思考一个问题:由你来实现一个最简单的冒泡排序算法.假设没有使用泛型的经验.可能会毫不犹豫的写出下面代码: public class SortHelper { //參数为int数 ...

  4. 赵雅智_service生命周期

    Android中的服务和windows中的服务是类似的东西,服务一般没实用户操作界面.它执行于系统中不easy被用户发觉,能够使用它开发如监控之类的程序. 服务的开发步骤 第一步:继承Service类 ...

  5. IntelliJ IDEA15,PhpStorm10,WebStorm11激活破解

    此方法可用于激活IntelliJ IDEA15,PhpStorm10,WebStorm11等系列JetBrains产品.仅供参考,请支持正版. 最新方法:http://15.idea.lanyus.c ...

  6. 使用xml-rpc调试openerp模块中的函数

    运行openerp模块中的函数 有很多方式, 可以在视图中加个按钮然后点击它, 也可以在集成开发环境中强制执行它. 不过, 用python写个小脚本,xml-rpc调用直接执行它, 无疑是最简便的方法 ...

  7. centos无法安装vmvare-tools的问题

    不要使用 vmware 自带的 tools, 版本太老了. 从这里下载:https://github.com/rasa/vmware-tools-patches.git 直接下载或者使用下面的命令: ...

  8. asp 支付宝 企业版 接口 支持网银接口 ,网银直接支付

    asp 支付宝 企业版 接口 支持网银接口 ,网银直接支付 仅仅是多了一个defalutbank的參数. 详细看 open.alipay.com <% ' 类名:AlipaySubmit ' 功 ...

  9. 让网页自动调用双核浏览器的极速模式(<meta name="renderer" content="webkit">)

    背景:最近做了一个网站,传单服务器之后,每次打开都是乱码,不对啊,我在本地测得明明是排版很整齐啊,然后发现,360用的是“兼容模式”打开的,这就尴尬了,用户一打开就是乱码,这用户体验得有多差!用户可不 ...

  10. Windows Visual Studio 下配置OpenGL

    很多同学不是很清楚OpenGL的配置,一个个帮忙配置也不是办法,干脆就写一篇教程好了. 本人使用的是Visual Studio 2015版本,对于Visual Studio 2012版本的配置会稍有不 ...