Quick start

goodsign edited this page Aug 8, 2014 · 43 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.

Basic config

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.

Loading config

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

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 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

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. 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.

Demo config with all features

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.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.