Having your app keep a detailed log can be indispensable for figuring out tricky user-reported bugs. This is easy on the server-side, where you can use any logging framework or just write to a file. Flash includes the trace function, which is useful while developing, but nearly impossible to get from an end-user. Flex, however, has a good logging api that lets you put log statements anywhere you want. I’ve always wanted the interface to be as easy as trace, so I write a couple of scripts to make it easier.

 

 

There are two files in there now. The first is log.as. I didn’t realize before that it was possible to define your own package-level functions. Some might disagree with me, but I think this syntax is a lot nicer than the default. It looks just like trace. All it does is create an easy way to access the flex logging api.

import net.seanhess.flog.log;

function stuff():void
{
    log("Executing stuff");
    // do stuff 
    log("Finished!");
}

You can replace all your trace statements with these. It’s easy enough to add a trace target to the logging api, so all log messages are traced. See the Flex Logging API for more details, but here’s how you would do it.

import mx.logging.Log;
import mx.logging.targets.TraceTarget;

// Only once in your app
function addLoggingTargets():void
{
    Log.addTarget(new TraceTarget());
}

The second little tool is a new logging target. I wanted a way to log to a local file in flex, so the support guys could get an end-user to email it. The only way to do this without hacks is to write to a shared object. It turns out if you write an array of strings to a shared object, it’s easily human-readable. You attach SharedObjectTarget like any other target and away you go.

import mx.logging.Log;
import net.seanhess.flog.SharedObjectTarget;

// Only once in your app
function addLoggingTargets():void
{
    Log.addTarget(new TraceTarget());
    Log.addTarget(new SharedObjectTarget("MyCustomAppLogging"));        
}

The file is located in the application data folder, which is in~/Library/Preferences/Macromedia/Flash Player/#SharedObjects on the mac. (Google it for windows). The target does some fancy stuff like automatically saving if it hasn’t received a log message for one second. It also automatically rotates messages once the size reaches about 50k (The default allowed size for shared objects is 100k).

There’s more that can be done here. I might add a debug and an error function. You could easily write an app to send the contents to a server too.

For alternatives and more information look at the approaches of Tom BarkerVirgil Cristea, and John Wilker.

Hi!

Your library seems to be just what I've been looking for. The only question remaining is the license under which it has been published. Could you please add some licensing information together with the sources?