picture of code

ZenLog

Sometimes, trace is not enough.

When I started working with Haxe, I wasn’t completely satisfied with any of the logging libraries, so of course I made my own.

Problem

The existing package that most resonated with me was LogShim, but it had one critical issue for me:

The selling point of Haxe is that you can export to any platform. I first started using it to write gameplay logic for a side-project I thought to eventually bring into Unity, or whatever engine would make the most sense years from now when I finally finish.  Haxe gave me the freedom to start coding now, without worrying about platforms or engines.

That means we want to easily route logs from a Haxe-package into a game engine’s native logging system. With LogShim, this would have to be done at compile time of the Haxe-package, before pulling it into Unity as C# for example.  That was the main use-case I was attempting to solve.

Solution

For this I created an ILogger typedef, and a static Logger singleton. This means that at runtime in your game engine, you can just pass in any ILogger  (example, a wrapper around calls to Unity’s logger) and it will take over.

class Log {
    public static var Logger:ILogger = new TraceLogger();
    
    public static inline function debug (?message :Dynamic, ?extra :Array<Dynamic>, ?pos :haxe.PosInfos): Void{
        Logger.debug(message, extra, pos);
    }

The default ILogger just routes everything to trace.

There’s also a FilteredLogger, which allows you to enable / disable each logging level, as well as a Hide and Show list to override the level-filtering based on the content of the log message.

As a bonus, the Zenlog satisfies the Log Shim typedef, so it can simply be plugged into anything already using that.

Room for Improvement

So, my questions to all the Haxe users out there:

Does this seem useful?  How could I improve it?  What do you look for in a logging library?

ScottPlusPlus

Working to upgrade our democracy by making voting more awesome (ex: STAR Voting). Reach out if you want to chat about saving the world.

Leave a Reply

Your email address will not be published. Required fields are marked *