Tour Weblog

File and TextArea Logging Classes for Adobe AIR and Flex

July 6th, 2007 by Mike Chambers

As part of the Adobe AIR applications we are building for the bus, I ran into a need to log information about the app state to the file system. Luckily, Flex has a pretty complete logging framework, so I was able to quickly create a FileTarget class for logging to the file system when running in Adobe AIR.

Here is a quick example:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
	creationComplete="onCreationComplete()"
	>

	<mx:Script>
		<![CDATA[
			import flash.filesystem.File;
			import com.adobe.onair.logging.TextAreaTarget;
			import mx.logging.LogEventLevel;
			import mx.logging.Log;
			import com.adobe.onair.logging.FileTarget;

			private var fileTarget:FileTarget;
			private var textAreaTarget:TextAreaTarget;

			private static const LOG_NAME:String = "logTargetExamples";

			private function onCreationComplete():void
			{
				fileTarget = new FileTarget();
				fileTarget.includeDate = true;
				fileTarget.includeTime = true;
				fileTarget.includeLevel = true;
				fileTarget.level = LogEventLevel.ALL;

				textAreaTarget = new TextAreaTarget(logField);
				textAreaTarget.includeDate = true;
				textAreaTarget.includeTime = true;
				textAreaTarget.includeLevel = true;
				textAreaTarget.level = LogEventLevel.ALL;

				Log.addTarget(fileTarget);
				Log.addTarget(textAreaTarget);

				Log.getLogger(LOG_NAME).info("File Logging Active : " + new File(fileTarget.logURI).nativePath);
			}

			private function onLogClick():void
			{
				Log.getLogger(LOG_NAME).info("This is an info log");
			}

		]]>
	</mx:Script>
	<mx:TextArea right="10" left="10" top="10" bottom="40" id="logField" />
	<mx:Button label="Log" right="10" bottom="10" click="onLogClick()"/>

</mx:WindowedApplication>

This will log all output to a log file on the file system (the exact path is logged to the text area). I also created a TextAreaTarget, so you can also direct log messages to any TextArea.

The FileTarget only works in Adobe AIR, but the TextAreaTarget works in any Flex project (desktop or browser).

You can download the classes from the bus tour’s code repository.

I will be showing this code as part of my “deconstructing the bus” session at the free one day AIR Camps. You can find more information, including a list of cities, agenda, and how to register here.

Trackback URI

Leave a Reply