-
Notifications
You must be signed in to change notification settings - Fork 244
Quick start
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.
This is an example of seelog config that redirects its output to console using default formatting options, constraints, etc.
<seelog>
<outputs>
<console />
</outputs>
</seelog>
Most wiki sections cover Seelog tuning using configs.
There are several funcs in the Seelog package that help you load configs.
logger, err := log.LoggerFromConfigAsFile("seelog.xml")
if err != nil {
return err
}
log.ReplaceLogger(logger)
There are also 'LoggerFromConfigAsBytes', 'LoggerFromConfigAsString'.
NOTE: You can run 'ReplaceLogger' at any time. Configurations switching is described here: Changing config on the fly
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 is lost 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 uses of the Seelog constructs. Don't bother about deferred flushing when writing packages: Writing libraries with Seelog
Both funcs change the package level variable responsible for the current logger. This variable is used in package level funcs 'Trace', 'Debug', etc. However, mind the difference.
The former correctly closes the previous logger (with flushing log data) and then replaces it with a new one. This is the most recommended method when you change log configurations.
The latter only flushes the previous logger (without closing it) and then replaces it with a new one. This method should be used when you change loggers and are indifferent to closing the old logger.
There is a demo config which demonstrates most of the features in one place: example config. You can check it before diving into all of the features.