Journal Wrapper class
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; /** * Tool class for logging in Air Program (Encapsulation of Class Com.adobe.air.logging.FileTarget in As3corelib official class library) * * @author Sakura * Blog Address: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 of this target was currently set at. * Value values are: * <ul> * <li><code>logeventlevel.fatal (+) </code> Designates events that is very * harmful and would eventually leads 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>logeventlev El. WARN (6) </code> designates events that could being * 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 is fine grained and most helpful when * Deb Ugging an application.</li> * * <li><code>logeventlevel.all (0) </code> intend Ed to force a target to * process all messages.</li> * </ul> **/Public StaticfunctionGet Level ():int{return _level; } public staticfunctionSet 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.applicationdirectorypublic staticfunctionget LogFilePath (): String {return _logfilepath; } public staticfunctionset LogFilePath (value:string): void {if(Checkfiletarget ()) {log.removetarget (_filetarget); _filetarget=NULL; _logfilepath=value; } initlogging (); } private staticfunctionGetLogger (category:string): ILogger {return Log.getlogger (category); } private staticfunctioninitlogging (): void {if(Checkfiletarget ()) return; Trace ("LogPath:"+_logfilepath); varfile: File =new File (_logfilepath); var filestrm:filestream=new FileStream; Filestrm.open (file, Filemode.write); Filestrm.writeint (0); Filestrm.close (); Trace ("Clear Log"+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 staticfunctioncheckfiletarget (): Boolean {return (_filetarget)?true:false; } private staticfunctiongeneratemessage (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 is fine grained a nd most helpful when debugging * an application. * * <p>the string specified for logging can contain braces with an index * indicating which ADDI tional parameter should be inserted * to the string before it is logged. * For example "The first additional parameter is {0} The second was {1}" * would be translated into "the first A Dditional parameter was ten, the * second was, when called with Ten and 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 would be replaced with * The additional parameters found at that index if specified. * * @param rest Additional parameters that can being subsituted in the str * parameter @ each "{<cod E>x</code>} "location, where <code>x</code> * was an integer (zero based) index value into th e Array of values * specified. **/Public StaticfunctionDebug (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 AD Ditional parameter should be inserted * to the string before it is logged. * For example "The first additional parameter is {0} The second was {1}" * would be translated into "the first A Dditional parameter was ten, the * second was, when called with Ten and 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 was a zero based index that would be replaced with * The additional parameters found on that index if specified. * * @param rest Additional parameters that can being subsituted in the str * parameter @ each "{<CODE&G T;x</code>} "location, where <code>x</code> * was an integer (zero based) index value into the A Rray of values * specified. **/Public Staticfunction 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 being harmful * to the application oper ation. * * <p>the string specified for logging can contain braces with an index * indicating which Additional parameter should be inserted * to the string before it is logged. * For example "The first additional parameter is {0} The second was {1}" * would be translated into "the first A Dditional parameter was ten, the * second was, when called with Ten and 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 was a zero based index that would b e Replaced with * the Additional parameters found at that index if specified. * * @param rest aadditional parameters that can being subsituted in the str * parameter @ each "{<code& Gt;x</code>} "location, where <code>x</code> * was an integer (zero based) index value into the Array of values * specified. **/Public Staticfunctionwarn (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 ADDI tional parameter should be inserted * to the string before it is logged. * For example "The first additional parameter is {0} The second was {1}" * would be translated into "the first A Dditional parameter was ten, the * second was, when called with Ten and 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 was a zero based index that would b E replaced with * The additional parameters found on that index if specified. * * @param rest Additional parameters that can being subsituted in the str * parameter @ each "{<CODE&G T;x</code>} "location, where <code>x</code> * was an integer (zero based) index value into the A Rray of values * specified. **/Public Staticfunctionerror (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 is very * harmful and would eventually le AD to Application Failure * * <p>the string specified for logging can contain braces with an index * indicating which additional parameter should be inserted * to the string before it is logged. * For example "The first additional parameter is {0} The second was {1}" * would be translated into "the Firs T additional parameter was ten the * second was "when called with Ten and 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 was a zero based index that would b E replaced with * The additional parameters found on that index if specified. * * @param rest Additional parameters that can being subsituted in the str * parameter @ each "{<CODE&G T;x</code>} "location, where <code>x</code> * was an integer (zero based) index value into the A Rray of values * specified. **/Public StaticfunctionFatal (category:string, message:string, ... rest): void {initlogging (); if(Log.isfatal ()) {var Logger:ilogger=GetLogger (category); Logger.fatal (Generatemessage (message,rest)); } } /** * Clear all logs. */Public Staticfunction Clear(): void {initlogging (); _filetarget.Clear(); } }}
Dependencies Required: As3corelib Library
Flex Desktop Air Software log add