as3corelib Tutorial:An Adobe AIR class that provides a log target for the Flex logging framework

log-128x128 Part of the newest corelib 0.92.1, an Adobe AIR only class that provides a log target for the Flex logging framework, that logs files to a file on the user’s system.I think it’s useful when you want to write the logs into a specified file instead of the default one.

Search-256x256 DownloadDownload Full Project

Methods

FileTarget()

  1. public function FileTarget(logFile:File = null)
  2. Parameters
  3. logFile:File (default = null)

clear()

  1. public function clear():void
  2. clear the log messages in the log file.

Properties

  1. logURI:String[read-only]
  2. public function get logURI():String

Return

  1. return the URI of the log file.

Screenshot:

FileTargetDemo

The following is full source code of FileTargetDemo.as:

  1. package
  2. {
  3.     import com.adobe.air.logging.FileTarget;
  4.  
  5.     import flash.events.Event;
  6.     import flash.filesystem.File;
  7.  
  8.     import mx.collections.ArrayCollection;
  9.     import mx.core.WindowedApplication;
  10.     import mx.logging.Log;
  11.     import mx.logging.LogEventLevel;
  12.  
  13.     public class FileTargetDemo extends WindowedApplication
  14.     {
  15.         public function FileTargetDemo()
  16.         {
  17.             super();
  18.         }       
  19.         [Bindable]
  20.         protected var myData:ArrayCollection;
  21.  
  22.         [Bindable]
  23.         protected var fileTarget:FileTarget;
  24.         protected var logFilePath:String;
  25.  
  26.         [Bindable]
  27.         protected var logContent:String;
  28.  
  29.         protected function initLogging():void
  30.         {
  31.             logFilePath = File.applicationDirectory.nativePath + "\\"+ new Date().toLocaleDateString()+".txt";
  32.  
  33.             var file:File = new File(logFilePath);
  34.  
  35.             fileTarget = new FileTarget(file);               
  36.             fileTarget.filters=["mx.rpc.*","mx.messaging.*"];                               
  37.             fileTarget.level = LogEventLevel.ALL;               
  38.             fileTarget.includeDate = true;
  39.             fileTarget.includeTime = true;
  40.             fileTarget.includeCategory = true;
  41.             fileTarget.includeLevel = true;               
  42.  
  43.             Log.addTarget(fileTarget);
  44.         }
  45.  
  46.         protected function showLog():void           
  47.         {
  48.             var file:File = new File(logFilePath);
  49.             if(    file.exists);
  50.             {   
  51.                 file.load();
  52.                 file.addEventListener(Event.COMPLETE,fileLoadCompleteHandler);
  53.             }
  54.         }
  55.  
  56.         private function fileLoadCompleteHandler(event:Event):void
  57.         {
  58.             logContent = String(event.currentTarget.data);
  59.         }
  60.  
  61.         protected function clearLog():void
  62.         {
  63.             fileTarget.clear();
  64.             showLog();
  65.         }
  66.  
  67.         protected function srv_ResultHandler(event:Event):void
  68.         {
  69.             myData = event.currentTarget.lastResult.data.result;
  70.         }
  71.  
  72.     }
  73. }

And FileTargetDemoView.mxml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FileTargetDemo
  3.     xmlns="*" 
  4.     xmlns:mx="http://www.adobe.com/2006/mxml"
  5.     layout="absolute"
  6.     creationComplete="initLogging()">
  7.     <mx:HTTPService 
  8.         id="srv" 
  9.         url="trace_example_data.xml" 
  10.         useProxy="false" 
  11.         result=" srv_ResultHandler(event);"
  12.         />
  13.  
  14.     <mx:Panel 
  15.         title="FileTarget Demo"
  16.         width="100%"
  17.         height="100%">
  18.         <mx:VDividedBox
  19.             width="100%"
  20.             height="100%">
  21.             <mx:LineChart id="chart" 
  22.                 dataProvider="{myData}" 
  23.                 showDataTips="true"
  24.                 width="100%"
  25.                 height="100%">
  26.                 <mx:horizontalAxis>
  27.                     <mx:CategoryAxis 
  28.                         categoryField="month"/>
  29.                 </mx:horizontalAxis>
  30.  
  31.                 <mx:series>
  32.                     <mx:LineSeries 
  33.                         yField="apple" 
  34.                         name="Apple"/>
  35.  
  36.                     <mx:LineSeries 
  37.                         yField="orange" 
  38.                         name="Orange"/>
  39.  
  40.                     <mx:LineSeries 
  41.                         yField="banana" 
  42.                         name="Banana"/>
  43.                 </mx:series>
  44.             </mx:LineChart>
  45.  
  46.             <mx:TextArea
  47.                 editable="false" 
  48.                 text="{logContent}"
  49.                 width="100%"
  50.                 height="100%"/>
  51.         </mx:VDividedBox>
  52.  
  53.         <mx:ControlBar>
  54.              <mx:Button id="b1" 
  55.                  label="Load Data"
  56.                  click="srv.send();" 
  57.                  />
  58.  
  59.              <mx:Button 
  60.                  label="show log"
  61.                  click="showLog()"/>
  62.  
  63.              <mx:Button 
  64.                  label="clear log"
  65.                  click="clearLog()"/>
  66.  
  67.              <mx:Label id="logFileURI" 
  68.                  text="log file uri:{fileTarget.logURI}"/>
  69.         </mx:ControlBar>
  70.     </mx:Panel> 
  71. </FileTargetDemo>

Enjoy!

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed
You can leave a response, or trackback from your own site.

Leave a Reply