# README
Logger
This logger basically configure zerolog so that you can log via github.com/rs/zerolog/log
Usage
Initialization
Import shared/logger/auto
package. It will be self-initialized.
import "github.com/xmlking/toolkit/logger/auto"
Other option to initialize logger is to set DefaultLogger your self. this will give more control to developer.
logger.DefaultLogger =logger.NewLogger()
logger.DefaultLogger =logger.NewLogger(logger.WithLevel(zerolog.DebugLevel), logger.WithFormat(logger.PRETTY))
logger.DefaultLogger =logger.NewLogger(logger.WithLevel(zerolog.DebugLevel), logger.WithFormat(logger.PRETTY), logger.EnableGrpcLog(true))
// with rotating file writer
lw := logger.FileWriter(
"test-demo.log",
logger.FileConfig{
MaxSize: 5,
MaxBackups: 10,
MaxAge: 14,
Compress: true,
}
)
logger.NewLogger(logger.WithOutput(lw), logger.WithFormat(logger.JSON), logger.WithLevel(zerolog.WarnLevel))
Once logger is initialized, then you can use standard github.com/rs/zerolog/log
package's helper methods to log in your code.
Environment Variables
Your can set Logger config via Environment Variables
grpc logs are disabled by default. you can enable via CONFY_LOG_GRPC
grpc internal logs also adopt
CONFY_LOG_LEVEL
andCONFY_LOG_FORMAT
No need to set
GRPC_GO_LOG_SEVERITY_LEVEL
andGRPC_GO_LOG_VERBOSITY_LEVEL
CONFY_LOG_LEVEL=<trace,debug,info,warn,error,fatal,panic>
CONFY_LOG_FORMAT=<pretty/json/gcp>
CONFY_LOG_GRPC=true
CONFY_LOG_FILE=app1.log
Aside from logging in JSON, you can also configure Zerolog to output binary logs encoded in CBOR format. You can enable it by using the binary_log
build tag while compiling your application:
go build -tags binary_log -o build ./service/engine/...
You can decode this binary log entry to JSON with any CBOR decoder, such as csd
CONFY_LOG_FORMAT=json ./build/engine 2> >(csd)
Test
CONFY_LOG_LEVEL=info CONFY_LOG_FORMAT=json go test github.com/xmlking/toolkit/logger -count=1
Reference
- A Complete Guide to Logging in Go with Zerolog