This content has been marked as final. Show 1 reply
I've implemented a logging framework for my company that allows log messages to be written to the trace file, back to the server, or both. I'm sorry that I can't share the code as it's not my intellectual property. But I can tell you my approach, which would be very easy to adapt to displaying log messages in a UI component.
First, read all the docs you can on the logging framework. I realize they don't tell you how to roll your own, but be familiar with them as a starting point.
Second, trace() is a low-level Flash Player function that is used by, but is really separate from, the logging framework. Specifically, trace() is used by mx.logging.targets.TraceTarget, which can be instantiated to receive various log messages that will then be output to the trace file. This is obviously close to what we want, but not quite there.
What I found I needed to do was move one step up to TraceTarget's parent, LineFormattedTarget, extend that class and override LineFormattedTarget.internalLog(), which is where (along with a couple other helper methods) I implemented the functionality I described above. If you go this route there are two things to be aware of:
1. internalLog() is in the mx_internal namespace; in order to override it you have to import and use mx_internal. Inspired by something else I read on that subject, I posted a blog about doing so here.
2. LineFormattedTarget.internalLog() is an empty method (it would probably be abstract were this Java); check out TraceTarget's implementation of it if you want to get a feel for a 'proper' implementation
There are other approaches--you could chuck the whole logging framework and just roll your own if you wanted. However, it has a good built-in support for Log4J-like features, including runtime management of log targets and log levels, that I find very valuable. There may be better approaches to implementing your own target as well, but I haven't found one yet. Once I understood how the log targets fit in to the overall framework, and got over my nervousness about using mx_internal (nothing exploded, I swear), it was actually really easy to implement, and I now use logging religiously.
Hope that helps,