Skip to content
Sunjammer edited this page Nov 20, 2011 · 1 revision

SLF4HX is a haXe port of SLF4AS, in turn an adaption of the Java SLF4J project, in turn a spiritual successor to Log5j. Phew.

The intent is to offer a minimalist, lightweight yet functional logging API for haXe applications.

General use:

1. Set up the log binding.

Logging.logBinding = new FlashTraceBinding();

A log binding is a receptacle for your logging messages. In short, it's what ends up having to deal with each message and display it or otherwise handle it in the logging solution of your choice. By default, the log binding is an instance of HaxeTraceBinding, which simply passes the message to the standard haXe trace.

In this example, we used the FlashLogBinding, which passes messages to the flash.Lib trace() method. To create a log binding of your own, implement ILogBinding.

2. Create and use a logger for your class

private static var L:Logger = Logging.getLogger(MyClass);
The Logger object is what you'll use to log messages, through one of its logging methods.

L.info("Regularly interesting stuff");
L.debug("Not so important stuff");
L.warn("Something's off...");
L.error("Shit has hit the fan (recoverable)");
L.fatal("A baby has hit the fan");

3. Log stuff!

The logging methods accept a variable number of arguments;

L.info("Hello world");
L.info("Hello","world");
will both result in the same output, "Hello world".

In addition, slf4hx supports some simple formatting to let you reuse log message formats:

L.info("My name is {}","Andreas"); Will result in "My name is Andreas";

A variable number of arguments can also be used in the formatting:

L.info("My name is {} and I'm from {}","Andreas","Norway")

When more arguments are passed than the format allows, it will loop back over itself:

L.info("My name is {}","Andreas","Richard");
Results in "My name is Andreas, My name is Richard"