Flex: Log to the Firebug Console

Posted by admin at 17 February 2010

Category: programming

Logging to the firebug console from a Flex App is easy. A quick google search turns up loads of goodies… but you don’t really need a full fledged logging API for simple Flex Apps. Additionally, the example in the Adobe Flex Cookbook pisses off Internet Explorer as there is no check to see if window.console is even available.

At any rate, I wrote a small static class for logging to firebug from a Flex app. Use it however you wish:

package ctz.util
{
	import flash.external.ExternalInterface;

	public final class ConsoleLog
	{
		public function ConsoleLog()
		{
			throw new Error("static methods only");
		}

		public static function log(o:Object):void
		{
			if (o is Array)
				ConsoleLog.logArray(o as Array);
			else if (o is String)
				ConsoleLog.logString(o as String);
			else
				ConsoleLog.logObject(o);
		}

		public static function logString(msg:String):void
		{
			if (ExternalInterface.available)
				ExternalInterface.call("function log(msg){ if (window.console) { console.log(msg); } }", msg);
		}

		public static function logObject(obj:Object):void
		{
			var s:String = "";
			for (var k:String in obj)
				s += k + ": " + obj[k];
			ConsoleLog.logString(s);
		}

		public static function logArray(arr:Array):void
		{
			var s:String = "Item Count: " + arr.length + " || ";
			for (var i:int = 0; i < arr.length; i++)
				s += i + ": " + arr[i] + " ";
			ConsoleLog.logString(s);
		}
	}
}

File can be downloaded here.

Leave a Reply

Leave a Reply
  • (required)
  • (required) (will not be published)