Kris
Simplified, pedestrian usage:
1
2
3
4
|
import tango.util.log.Config;
Log ("hello world");
Log ("temperature is {} degrees", 75);
|
Generic usage:
Loggers are named entities, sometimes shared, sometimes specific to
a particular portion of code. The names are generally hierarchical in
nature, using dot notation (with '.') to separate each named section.
For example, a typical name might be something like "mail.send.writer"
1
2
3
4
5
6
7
8
|
import tango.util.log.Log;
auto log = Log.lookup ("mail.send.writer");
log.info ("an informational message");
log.error ("an exception message: {}", exception);
etc ...
|
It is considered good form to pass a logger instance as a function or
class-ctor argument, or to assign a new logger instance during static
class construction. For example: if it were considered appropriate to
have one logger instance per class, each might be constructed like so:
1
2
3
4
5
6
|
private Logger log;
static this()
{
log = Log.lookup (nameOfThisClassOrStructOrModule);
}
|
Messages passed to a Logger are assumed to be either self-contained
or configured with "{}" notation a la Layout & Stdout:
1
|
log.warn ("temperature is {} degrees!", 101);
|
Note that an internal workspace is used to format the message, which
is limited to 2000 bytes. Use "{.256}" truncation notation to limit
the size of individual message components, or use explicit formatting:
1
2
3
|
char[4096] buf = void;
log.warn (log.format (buf, "a very long message: {}", someLongMessage));
|
To avoid overhead when constructing arguments passed to formatted
messages, you should check to see whether a logger is active or not:
1
2
|
if (log.warn)
log.warn ("temperature is {} degrees!", complexFunction());
|
tango.log closely follows both the API and the behaviour as documented
at the official Log4J site, where you'll find a good tutorial. Those
pages are hosted over
here.