Skip to content
pkorotkov edited this page Mar 7, 2012 · 37 revisions

Seelog is designed to be exceptionally handy. Its default config and package-level logger are ready-to-use, so to start you need just 2 lines of code:

package main

import log "github.com/cihub/seelog"

func main() {
    defer log.Flush()
    log.Info("Hello from Seelog!")
}

'Info' is just one of the log levels supported by Seelog. You can also use Trace, Debug, Info, Warn, Error, Critical levels.

Loading config

Most wiki sections cover Seelog tuning using a config. There are several funcs in the 'seelog' package that help you create and load the config.

logger, err := log.LoggerFromConfigAsFile("seelog.xml")
	
if err != nil {
	return err
}
	
log.ReplaceLogger(logger)

There are also 'LoggerFromConfigAsBytes' and 'LoggerFromWriterWithMinLevel' funcs. The former is obvious, the latter is used to write log to an io.Writer and avoid creating Seelog xml configs.

NOTE: You can run 'ReplaceLogger' at any time. Configurations switching is described here: Changing config on the fly

Defer block and flushing

In many scenarios log information generated cannot be processed in the main goroutine. In these cases we suggest asynchronous loggers which work in a non-blocking mode sequentially gulping down the buffered messages from the queue. In such situations it is crucial to be sure enough that no log data losses if an application suffers a panic crash. We resolved it with log.Flush() in the defer block of the main function, what guarantees that all the messages left in a log message queue will be normally processed independently of whether the application panics or not.

NOTE: The only place where the defer block must be put is the main function of an executable, before any usages of the seelog constructs. Don't bother about deferred flushing when writing packages: Writing libraries with Seelog

ReplaceLogger and UseLogger

Both funcs change the package level variable responsible for the current logger. This variable is used in package level funcs 'Trace', 'Debug', etc.

The former correctly closes the previous logger (with flushing log data) and then replaces the old one with the new one. This is the most recommended func when you change one configuration to another.

The latter only flushes the previous logger (without closing it) and then replaces it with the new one. This func should be used when you 'switch' between loggers and you don't need to close any of them when changing.