I figured since I’m on the subject I would go ahead and create a custom logging target for sugarcookie and a companion logging app – LumberJack. This particular target sends log message objects over a LocalConnection to a basic logging console that will output them into a text field. Being able to see trace statements from a site running on any server, on any network can really help when debugging your application that is deployed somewhere besides your localhost. There are other tools out there that do this like RED|Bug, Xray or even FlashTracer, but this one is extremely simple and includes all the code so you can modify it to suit your needs.
First let’s create the new log target class by extending the existing mx.logging.targets.LineFormattedTarget Flex class. Then override the logEvent method with the functionality desired and you’re in business. Here’s how I did it:
package sugarcookie.logger { import flash.events.StatusEvent; import flash.events.AsyncErrorEvent; import flash.events.SecurityErrorEvent; import flash.net.LocalConnection; import mx.logging.LogEvent; import mx.logging.targets.LineFormattedTarget; import mx.utils.ObjectUtil; public class LogMessageDispatcherTarget extends LineFormattedTarget { private var _conn:LocalConnection; public function LogMessageDispatcherTarget() { super(); _conn = new LocalConnection(); _conn.addEventListener(StatusEvent.STATUS, onStatus); _conn.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError); _conn.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError); } public override function logEvent(event:LogEvent):void { _conn.send("_echo1","update",{level:event.level,category:event.currentTarget.category,message:event.message,time:this.includeTime}); } private function onStatus(evt:StatusEvent):void { trace("onStatus Event: "+ObjectUtil.toString(evt)); } private function onAsyncError(evt:AsyncErrorEvent):void { trace("onAsyncError Event: "+ObjectUtil.toString(evt)); } private function onSecurityError(evt:SecurityErrorEvent):void { trace("onSecurityError Event: "+ObjectUtil.toString(evt)); } } }
Building on the previous post, I have swapped out the TraceTarget for our new LogMessageDispatcherTarget.
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init();"> <mx:Script> <![CDATA[ import mx.logging.Log; import mx.logging.ILogger; import mx.logging.LogEventLevel; import sugarcookie.logger.LogMessageDispatcherTarget; private var _log:ILogger; private var _loggerTarget:LogMessageDispatcherTarget; private function init():void { _loggerTarget = new LogMessageDispatcherTarget(); _loggerTarget.filters=["*"]; _loggerTarget.level = LogEventLevel.ALL; _loggerTarget.includeDate = false; _loggerTarget.includeTime = false; _loggerTarget.includeCategory = true; _loggerTarget.includeLevel = true; Log.addTarget(_loggerTarget); _log = Log.getLogger("ExampleRemoteLogger"); _log.info("Logging rocks, Yo!"); } ]]> </mx:Script> </mx:Application>