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()

public function FileTarget(logFile:File = null)
Parameters
logFile:File (default = null)

clear()

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

Properties

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

Return

return the URI of the log file.

Screenshot:

FileTargetDemo

The following is full source code of FileTargetDemo.as:

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

	import flash.events.Event;
	import flash.filesystem.File;

	import mx.collections.ArrayCollection;
	import mx.core.WindowedApplication;
	import mx.logging.Log;
	import mx.logging.LogEventLevel;

	public class FileTargetDemo extends WindowedApplication
	{
		public function FileTargetDemo()
		{
			super();
		}
		[Bindable]
		protected var myData:ArrayCollection;

		[Bindable]
		protected var fileTarget:FileTarget;
		protected var logFilePath:String;

		[Bindable]
		protected var logContent:String;

		protected function initLogging():void
		{
			logFilePath = File.applicationDirectory.nativePath + "\\"+ new Date().toLocaleDateString()+".txt";

			var file:File = new File(logFilePath);

			fileTarget = new FileTarget(file);
			fileTarget.filters=["mx.rpc.*","mx.messaging.*"];
			fileTarget.level = LogEventLevel.ALL;
			fileTarget.includeDate = true;
			fileTarget.includeTime = true;
			fileTarget.includeCategory = true;
			fileTarget.includeLevel = true;            	

			Log.addTarget(fileTarget);
		}

		protected function showLog():void
		{
			var file:File = new File(logFilePath);
			if(	file.exists);
			{
				file.load();
				file.addEventListener(Event.COMPLETE,fileLoadCompleteHandler);
			}
		}

		private function fileLoadCompleteHandler(event:Event):void
		{
			logContent = String(event.currentTarget.data);
		}

		protected function clearLog():void
		{
			fileTarget.clear();
			showLog();
		}

		protected function srv_ResultHandler(event:Event):void
		{
			myData = event.currentTarget.lastResult.data.result;
		}

	}
}

And FileTargetDemoView.mxml

<?xml version="1.0" encoding="utf-8"?>
<FileTargetDemo
	xmlns="*"
	xmlns:mx="http://www.adobe.com/2006/mxml"
	layout="absolute"
	creationComplete="initLogging()">
	<mx:HTTPService
        id="srv"
        url="trace_example_data.xml"
        useProxy="false"
        result=" srv_ResultHandler(event);"
    	/>

	<mx:Panel
		title="FileTarget Demo"
		width="100%"
		height="100%">
		<mx:VDividedBox
			width="100%"
			height="100%">
			<mx:LineChart id="chart"
				dataProvider="{myData}"
				showDataTips="true"
				width="100%"
				height="100%">
		        <mx:horizontalAxis>
		            <mx:CategoryAxis
		            	categoryField="month"/>
		        </mx:horizontalAxis>

		        <mx:series>
		            <mx:LineSeries
		            	yField="apple"
		            	name="Apple"/>

		            <mx:LineSeries
		            	yField="orange"
		            	name="Orange"/>

		            <mx:LineSeries
		            	yField="banana"
		            	name="Banana"/>
		        </mx:series>
		    </mx:LineChart>

		    <mx:TextArea
		    	editable="false"
		    	text="{logContent}"
		    	width="100%"
				height="100%"/>
		</mx:VDividedBox>

		<mx:ControlBar>
			 <mx:Button id="b1"
			 	label="Load Data"
			 	click="srv.send();"
			 	/>

			 <mx:Button
			 	label="show log"
			 	click="showLog()"/>

			 <mx:Button
			 	label="clear log"
			 	click="clearLog()"/>

			 <mx:Label id="logFileURI"
			 	text="log file uri:{fileTarget.logURI}"/>
		</mx:ControlBar>
	</mx:Panel>
</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 Follow us Follow us
You can leave a response, or trackback from your own site.

Leave a Reply