Clog
Clog is a channel-based logging package for Go.
This package supports multiple logger adapters across different levels of logging. It uses Go's native channel feature to provide goroutine-safe mechanism on large concurrency.
Installation
To use a tagged revision:
go get gopkg.in/clog.v1
To use with latest changes:
go get github.com/go-clog/clog
Please apply -u
flag to update in the future.
Testing
If you want to test on your machine, please apply -t
flag:
go get -t gopkg.in/clog.v1
Please apply -u
flag to update in the future.
Getting Started
Clog currently has three builtin logger adapters: console
, file
, slack
and discord
.
It is extremely easy to create one with all default settings. Generally, you would want to create new logger inside init
or main
function.
...
import (
"fmt"
"os"
log "gopkg.in/clog.v1"
)
func init() {
err := log.New(log.CONSOLE, log.ConsoleConfig{})
if err != nil {
fmt.Printf("Fail to create new logger: %v\n", err)
os.Exit(1)
}
log.Trace("Hello %s!", "Clog")
log.Info("Hello %s!", "Clog")
log.Warn("Hello %s!", "Clog")
...
}
...
The above code is equivalent to the follow settings:
...
err := log.New(log.CONSOLE, log.ConsoleConfig{
Level: log.TRACE,
BufferSize: 0,
})
...
In production, you may want to make log less verbose and asynchronous:
...
err := log.New(log.CONSOLE, log.ConsoleConfig{
Level: log.INFO,
BufferSize: 100,
})
...
Console logger comes with color output, but for non-colorable destination, the color output will be disabled automatically.
Error Location
When using log.Error
and log.Fatal
functions, the first argument allows you to indicate whether to print the code location or not.
...
log.Error(0, "So bad... %v", err)
log.Error(2, "So bad... %v", err)
log.Fatal(2, "Boom! %v", err)
...
Calling log.Fatal
will exit the program.
File
File logger is more complex than console, and it has ability to rotate:
...
err := log.New(log.FILE, log.FileConfig{
Level: log.INFO,
BufferSize: 100,
Filename: "clog.log",
FileRotationConfig: log.FileRotationConfig {
Rotate: true,
Daily: true,
},
})
...
Slack
Slack logger is also supported in a simple way:
...
err := log.New(log.SLACK, log.SlackConfig{
Level: log.INFO,
BufferSize: 100,
URL: "https://url-to-slack-webhook",
})
...
This logger also works for Discord Slack endpoint.
Discord
Discord logger is supported in rich format via Embed Object:
...
err := log.New(log.DISCORD, log.DiscordConfig{
Level: log.INFO,
BufferSize: 100,
URL: "https://url-to-discord-webhook",
})
...
This logger also retries automatically if hits rate limit after retry_after
.
Credits
License
This project is under Apache v2 License. See the LICENSE file for the full license text.