Golang logging library
Package logging implements a logging infrastructure for Go. Its output format
is customizable and supports different logging backends like syslog, file and
memory. Multiple backends can be utilized with different log levels per backend
and logger.
NOTE: backwards compatibility promise have been dropped for master. Please
vendor this package or use gopkg.in/op/go-logging.v1
for previous version. See
changelog for details.
Example
Let's have a look at an example which demonstrates most
of the features found in this library.
package main
import (
"os"
"github.com/op/go-logging"
)
var log = logging.MustGetLogger("example")
var format = logging.MustStringFormatter(
`%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}`,
)
type Password string
func (p Password) Redacted() interface{} {
return logging.Redact(string(p))
}
func main() {
backend1 := logging.NewLogBackend(os.Stderr, "", 0)
backend2 := logging.NewLogBackend(os.Stderr, "", 0)
backend2Formatter := logging.NewBackendFormatter(backend2, format)
backend1Leveled := logging.AddModuleLevel(backend1)
backend1Leveled.SetLevel(logging.ERROR, "")
logging.SetBackend(backend1Leveled, backend2Formatter)
log.Debugf("debug %s", Password("secret"))
log.Info("info")
log.Notice("notice")
log.Warning("warning")
log.Error("err")
log.Critical("crit")
}
Installing
Using go get
$ go get github.com/op/go-logging
After this command go-logging is ready to use. Its source will be in:
$GOPATH/src/pkg/github.com/op/go-logging
You can use go get -u
to update the package.
Documentation
For docs, see http://godoc.org/github.com/op/go-logging or run:
$ godoc github.com/op/go-logging
Additional resources
- wslog -- exposes log messages through a WebSocket.