COMMUNITY

Shared haxe libraries: how to log beyond trace statements?


(Dion) #1

I create, and attempt to maintain, several haxelib libraries.

I’d like to add logging statements beyond “trace”, ideally, with different levels of logging: debug, info, etc.

But how to do this in way that can be easily used when someone else is consuming the library, and may have their own logging solution? How to do this in a way that’s easy for the library consumer to understand, and allow them to use my libraries log statements with their own solution, with a minimal amount of configuration?

I’m ok if the solution is not cross-platform, if the solution is simpler.

I’d really like to hear both quick-and-dirty, and more complex solutions, or ideas, if anyone has any.

Dion


(Ben Morris) #2

Here is HaxePunk’s minimal logging utility. With Import.hx (awesome underappreciated feature) you can import these helpers into your namespace so they’re as easy to use as trace.

For extensibility I would just give consumers a method to register a log callback (LogLevel -> String -> Void) and use the callback if one is defined.

A nice thing about Haxe is, with conditional compilation, you can have log statements while debugging, but in production the method calls will disappear without a trace :wink:


(Allan Dowdeswell) #3

I’ve been using MConsole.


(Kyliathy) #4

I’m resurrecting this because I’m curious about the topic.

I found this, by the makers of hexMachina:

It’s inspired by log4j. Opinions?


(Philippe) #5

I’d suggest having something dead simple, like the haxe.Log.trace function that one can redefine and route to a preferred logging infra. Maybe your lib would use an internal function which could be filtered by setting a log level value, static as well, and you may possibly also want to namespace the logs to differentiate your different libs.


#6

I’d say that this very much depends on what you’re expecting from your logging.
If you’re used to log4j or log4net and you want the same possibilities of configuration or you’re expecting sending logs to multiple endpoints based on their verbosity, hierarchy etc. with the tradeoff of minor performance hit then go with hexLog.
If you need something to just print out statement into browser console then hexLog is likely an overkill.


(Kyliathy) #7

It’s a pretty complex project I feel, specifically on the logic side of things (it’s a story & game logic engine). So I think I’m going to need quite a lot of logging when I’m hunting for bugs. But @elsassph’s suggestion can work too if implemented correctly. I’m not favoring any log4 variant, I’m simply not confident enough in my Haxing skills yet :smiley: . And since I’m investing as much as I can in the prototype itself, I have little to spare when it comes to making an efficient yet future-proof logging system.


(Mark) #8

Not sure if you seen this, but there is a section on debugging in Haxe Manual: https://haxe.org/manual/debugging.html