Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/op/go-logging
Golang logging library
https://github.com/op/go-logging
Last synced: 29 days ago
JSON representation
Golang logging library
- Host: GitHub
- URL: https://github.com/op/go-logging
- Owner: op
- License: bsd-3-clause
- Created: 2013-04-03T03:45:10.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2023-12-13T20:47:18.000Z (11 months ago)
- Last Synced: 2024-10-10T23:08:06.473Z (about 1 month ago)
- Language: Go
- Size: 146 KB
- Stars: 1,793
- Watchers: 61
- Forks: 272
- Open Issues: 60
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
## Golang logging library
[![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)
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](CHANGELOG.md) for details.## Example
Let's have a look at an [example](examples/example.go) which demonstrates most
of the features found in this library.[![Example Output](examples/example.png)](examples/example.go)
```go
package mainimport (
"os""github.com/op/go-logging"
)var log = logging.MustGetLogger("example")
// Example format string. Everything except the message has a custom color
// which is dependent on the log level. Many fields have a custom output
// formatting too, eg. the time returns the hour down to the milli second.
var format = logging.MustStringFormatter(
`%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}`,
)// Password is just an example type implementing the Redactor interface. Any
// time this is logged, the Redacted() function will be called.
type Password stringfunc (p Password) Redacted() interface{} {
return logging.Redact(string(p))
}func main() {
// For demo purposes, create two backend for os.Stderr.
backend1 := logging.NewLogBackend(os.Stderr, "", 0)
backend2 := logging.NewLogBackend(os.Stderr, "", 0)// For messages written to backend2 we want to add some additional
// information to the output, including the used log level and the name of
// the function.
backend2Formatter := logging.NewBackendFormatter(backend2, format)// Only errors and more severe messages should be sent to backend1
backend1Leveled := logging.AddModuleLevel(backend1)
backend1Leveled.SetLevel(logging.ERROR, "")// Set the backends to be used.
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](https://godoc.org/github.com/cryptix/exp/wslog) -- exposes log messages through a WebSocket.