gol
gol copied to clipboard
gol is a high performance async log kit for golang
gol: a high performance async log kit for golang
gol is a high performance async log infrastructure for golang, which include several useful log backend adapters, include file/file rotate/stmp/slack/elasticsearch etc...
Introduce
Level
gol support various log levels, you can set the logger's level to disable some lower level output
const (
ALL LogLevel = iota
DEBUG
INFO
WARN
ERROR
CRITICAL
)
Built in adapters
gol has several built in adapters
- Console adapter support write log to stderr, and this is the default adapter
- File adapter support write log to file
- File rotate adapter support write log to rotate files
- Smtp adapter support write log to email
- Slack adapter support write log to given slack channel
- ES adapter support write log to elastic search (under development)
Customize backend adapters
You can create any backend adapter which implement the Adapter interface.
Actually Adapter is a alias of io.Writer
type Adapter interface {
io.WriteCloser
}
Color
gol also include a colorful output
Usage
Log to console
import (
"github.com/philchia/gol"
"runtime"
)
defer gol.Flush()
gol.Debug("Hello, gol!!!")
gol.Criticalf("Hello from %s", runtime.GOOS)
Not log to console
import (
"github.com/philchia/gol"
"runtime"
)
gol.RemoveAdapter(gol.CONSOLELOGGER)
Log to file
import (
"github.com/philchia/gol"
"runtime"
)
defer gol.Flush()
gol.AddLogAdapter("file", file.NewAdapter("/var/log/tmp.log"))
gol.Debug("Hello, gol!!!")
gol.Criticalf("Hello from %s", runtime.GOOS)
Rotate log to file
import (
"github.com/philchia/gol"
"runtime"
)
defer gol.Flush()
gol.AddLogAdapter("rotate file", rotatefile.NewAdapter("./temp.log", 6, rotatefile.KB*1))
gol.Debug("Hello, gol!!!")
gol.Criticalf("Hello from %s", runtime.GOOS)
Set level
import (
"github.com/philchia/gol"
"runtime"
)
defer gol.Flush()
gol.SetLevel(gol.ERROR)
gol.Debug("Hello, gol!!!") // this will not print
gol.Criticalf("Hello from %s", runtime.GOOS)
Set options
import (
"github.com/philchia/gol"
"runtime"
)
defer gol.Flush()
gol.SetOption(gol.Llongfile | gol.Ldate | gol.Ltime | gol.Lmicroseconds)
gol.Debug("Hello, gol!!!")
gol.Criticalf("Hello from %s", runtime.GOOS)
Add adapters
You can implement you own custom adapters which implement the Adapter interface.
import (
"github.com/philchia/gol"
"runtime"
)
defer gol.Flush()
gol.SetOption(gol.Llongfile | gol.Ldate | gol.Ltime | gol.Lmicroseconds)
gol.AddLogAdapter("anonymous", a)
gol.Debug("Hello, gol!!!")
gol.Criticalf("Hello from %s", runtime.GOOS)
Installation
$go get github.com/philchia/gol
or you can use go get -u
to update the package
Documentation
For docs, see Documentation or run:
$godoc github.com/philchia/gol
Benchmark
gol include a benchmark against the builtin log package, run $go test ./... -bench . -benchmem
in your terminal to run the bench
Features
- [X] Log level support
- [X] Customizable log option support
- [X] Async write
- [X] Colorful output
- [X] Flush buffered log
- [X] Toggle console adapter
- [X] Logrotate
- [X] Mail adapter
- [X] Slack adapter
- [X] Level support for single adapter
- [ ] Elastic Search adapter for ELK stack
- [ ] 100% coverage
- [ ] Customizable msg buffer size
License
gol code is published under the MIT license