Skip to content

Using multiple loggers

cihub edited this page Feb 12, 2012 · 11 revisions

Actually, you should avoid it in most cases. If you need to change some parameters on the fly, read this section: Changing config on the fly. Multiple loggers should be used in case you really need different logger configurations to exist simultaneously.

Usually, the most common use-case for this functionality is logging in programs with multiple modules, e.g. you import 2-3 libraries that also use Seelog, so you want to pass them special configs. See Writing libraries with Seelog.

Another practical case is logging in extremely performance critical situations: it is sometimes not acceptable to write many exceptions and filters in dispatching scheme for performance critical sections, because it could make things too complicated. In these cases it could be better to use a simple logger, which just holds 1 constraint and 1 output and just use it in all such critical files.

NOTE: In most situations, even in high-load apps, you don't need that, because you just should avoid logging too much. Info messages and warns for diagnostics (not frequent), errors and critical for errors (also not frequent). Debug and Trace should be disabled in production configs. So probably, this 'multi-config' feature should be used rarely in case it is absolutely needed.

Implementation

logger1, err1 := log.LoggerFromConfigAsFile("seelog1.xml")
logger2, err2 := log.LoggerFromConfigAsFile("seelog2.xml")
logger3, err3 := log.LoggerFromConfigAsFile("seelog3.xml")

...
// Switching between them without closing them
log.UseLogger(logger1)
...
log.UseLogger(logger2)
...
log.UseLogger(logger3)
...
log.UseLogger(logger1)
...