• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..23-Jun-2021-

CHANGELOG.mdH A D23-Jun-2021683 2014

CONTRIBUTORSH A D23-Jun-2021199 65

LICENSEH A D23-Jun-20211.4 KiB2824

README.mdH A D23-Jun-20212.9 KiB9464

backend.goH A D23-Jun-20211.2 KiB4727

format.goH A D23-Jun-202110.2 KiB409287

level.goH A D23-Jun-20213 KiB13497

log.goH A D23-Jun-20211.1 KiB5846

log_nix.goH A D23-Jun-20211.1 KiB4728

log_windows.goH A D23-Jun-20212.8 KiB10572

logger.goH A D23-Jun-20217.3 KiB262163

memory.goH A D23-Jun-20215.4 KiB238165

multi.goH A D23-Jun-20211.8 KiB6644

syslog.goH A D23-Jun-20211.5 KiB5434

syslog_fallback.goH A D23-Jun-2021696 2916

README.md

1## Golang logging library
2
3[![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/op/go-logging) [![build](https://img.shields.io/travis/op/go-logging.svg?style=flat)](https://travis-ci.org/op/go-logging)
4
5Package logging implements a logging infrastructure for Go. Its output format
6is customizable and supports different logging backends like syslog, file and
7memory. Multiple backends can be utilized with different log levels per backend
8and logger.
9
10**_NOTE:_** backwards compatibility promise have been dropped for master. Please
11vendor this package or use `gopkg.in/op/go-logging.v1` for previous version. See
12[changelog](CHANGELOG.md) for details.
13
14## Example
15
16Let's have a look at an [example](examples/example.go) which demonstrates most
17of the features found in this library.
18
19[![Example Output](examples/example.png)](examples/example.go)
20
21```go
22package main
23
24import (
25	"os"
26
27	"github.com/op/go-logging"
28)
29
30var log = logging.MustGetLogger("example")
31
32// Example format string. Everything except the message has a custom color
33// which is dependent on the log level. Many fields have a custom output
34// formatting too, eg. the time returns the hour down to the milli second.
35var format = logging.MustStringFormatter(
36	`%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}`,
37)
38
39// Password is just an example type implementing the Redactor interface. Any
40// time this is logged, the Redacted() function will be called.
41type Password string
42
43func (p Password) Redacted() interface{} {
44	return logging.Redact(string(p))
45}
46
47func main() {
48	// For demo purposes, create two backend for os.Stderr.
49	backend1 := logging.NewLogBackend(os.Stderr, "", 0)
50	backend2 := logging.NewLogBackend(os.Stderr, "", 0)
51
52	// For messages written to backend2 we want to add some additional
53	// information to the output, including the used log level and the name of
54	// the function.
55	backend2Formatter := logging.NewBackendFormatter(backend2, format)
56
57	// Only errors and more severe messages should be sent to backend1
58	backend1Leveled := logging.AddModuleLevel(backend1)
59	backend1Leveled.SetLevel(logging.ERROR, "")
60
61	// Set the backends to be used.
62	logging.SetBackend(backend1Leveled, backend2Formatter)
63
64	log.Debugf("debug %s", Password("secret"))
65	log.Info("info")
66	log.Notice("notice")
67	log.Warning("warning")
68	log.Error("err")
69	log.Critical("crit")
70}
71```
72
73## Installing
74
75### Using *go get*
76
77    $ go get github.com/op/go-logging
78
79After this command *go-logging* is ready to use. Its source will be in:
80
81    $GOPATH/src/pkg/github.com/op/go-logging
82
83You can use `go get -u` to update the package.
84
85## Documentation
86
87For docs, see http://godoc.org/github.com/op/go-logging or run:
88
89    $ godoc github.com/op/go-logging
90
91## Additional resources
92
93* [wslog](https://godoc.org/github.com/cryptix/go/logging/wslog) -- exposes log messages through a WebSocket.
94