Clog is a channel-based logging package for Go (Evacuated from NSA/Microsoft Github)

anonymous 0ad05a7f49 cleanup 5 anos atrás
debian 0c76806157 close #2 5 anos atrás
.travis.yml 0ad05a7f49 cleanup 5 anos atrás
LICENSE 2d70043ee0 Initial commit 8 anos atrás
Makefile 0efdf4a8ed Add basic tests (#2) 7 anos atrás
README.md 0ad05a7f49 cleanup 5 anos atrás
clog.go 3bc2eaba5f discord: add native support (#6) 6 anos atrás
clog_test.go 14cd95b1ed Add more tests (#2) 7 anos atrás
console.go 3bc2eaba5f discord: add native support (#6) 6 anos atrás
console_test.go c325c1547f Add Delete function and rename NewLogger -> New 7 anos atrás
discord.go 3bc2eaba5f discord: add native support (#6) 6 anos atrás
discord_test.go 3bc2eaba5f discord: add native support (#6) 6 anos atrás
error.go 2d70043ee0 Initial commit 8 anos atrás
file.go 3bc2eaba5f discord: add native support (#6) 6 anos atrás
file_test.go 2fcf07b477 close #3 5 anos atrás
logger.go c325c1547f Add Delete function and rename NewLogger -> New 7 anos atrás
logger_test.go c291b816fa Add more tests (#2) 7 anos atrás
slack.go 3bc2eaba5f discord: add native support (#6) 6 anos atrás
slack_test.go 3bc2eaba5f discord: add native support (#6) 6 anos atrás

README.md

Clog Build Status GoDoc Sourcegraph

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.

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")
	// Output: Hello 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, // Record all logs
		BufferSize: 0,         // 0 means logging synchronously
	})
...

In production, you may want to make log less verbose and asynchronous:

...
	err := log.New(log.CONSOLE, log.ConsoleConfig{
		// Logs under INFO level (in this case TRACE) will be discarded
		Level:      log.INFO, 
		// Number mainly depends on how many logs will be produced by program, 100 is good enough
		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.

...
	// 0 means disable printing code location
	log.Error(0, "So bad... %v", err)

	// To print appropriate code location mainly depends on how deep your call stack is, 
	// you need to try and verify
	log.Error(2, "So bad... %v", err)
	// Output: 2017/02/09 01:06:16 [ERROR] [...uban-builder/main.go:64 main()] ...
	log.Fatal(2, "Boom! %v", err)
	// Output: 2017/02/09 01:06:16 [FATAL] [...uban-builder/main.go:64 main()] ...
...

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,
		},
	})
...

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.