# README
log
A wrapper library based on go.uber.org/zap
.
Installation
go get -u github.com/magicedy/log
Quick Start
package main
import (
"github.com/magicedy/log"
"go.uber.org/zap"
"sync"
)
var once sync.Once
func setupLogger() {
once.Do(func() {
fileConf := log.FileLogConfig{RootPath: "logs", Filename: "log.jsonl"}
conf := &log.Config{Level: "debug", Stdout: true, Format: "json", File: fileConf}
l, p, err := log.InitLogger(conf)
if err == nil {
log.ReplaceGlobals(l, p)
} else {
log.Fatal("initialize logger error", zap.Error(err))
}
})
}
func main() {
setupLogger()
log.Print("this is info message")
log.Println("Hello", "World")
log.Debug("this is debug message")
log.Info("this is info message")
log.Infow("this is info message with fileds", "age", 37, "agender", "man")
log.Warn("this is warn message")
log.Error("this is error message")
}
{"level":"INFO","msg":"this is info message"}
{"level":"INFO","msg":"Hello World"}
{"level":"DEBUG","msg":"this is debug message"}
{"level":"INFO","msg":"this is info message"}
{"level":"INFO","msg":"this is info message with fileds","age":37,"agender":"man"}
{"level":"WARN","msg":"this is warn message"}
{"level":"ERROR","msg":"this is error message"}
# Functions
Ctx returns a logger which will log contextual messages attached in ctx.
No description provided by the author
No description provided by the author
No description provided by the author
DefaultTimeEncoder serializes time.Time to a human-readable formatted string.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
GetLevel gets the logging level.
No description provided by the author
No description provided by the author
No description provided by the author
InitLogger initializes a zap logger.
InitLoggerWithWriteSyncer initializes a zap logger with specified write syncer.
InitTestLogger initializes a logger for unit tests.
L returns the global Logger, which can be reconfigured with ReplaceGlobals.
No description provided by the author
NewTextCore creates a Core that writes logs to a WriteSyncer.
No description provided by the author
NewTextEncoderByConfig creates a fast, low-allocation Text encoder with config.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
R returns utils.ReconfigurableRateLimiter.
RatedDebug print logs at debug level it limit log print to avoid too many logs return true if log successfully.
RatedInfo print logs at info level it limit log print to avoid too many logs return true if log successfully.
RatedWarn print logs at warn level it limit log print to avoid too many logs return true if log successfully.
No description provided by the author
ReplaceGlobals replaces the global Logger and SugaredLogger.
S returns the global SugaredLogger, which can be reconfigured with ReplaceGlobals.
SetLevel alters the logging level.
ShortCallerEncoder serializes a caller in file:line format.
Sync flushes any buffered log entries.
No description provided by the author
No description provided by the author
No description provided by the author
With creates a child logger and adds structured context to it.
WithDebugLevel returns context with a debug level enabled logger.
WithErrorLevel returns context with a error level enabled logger.
WithFatalLevel returns context with a fatal level enabled logger.
WithFields returns a context with fields attached.
WithInfoLevel returns context with a info level enabled logger.
WithModule adds given module field to the logger in ctx.
WithReqID adds given reqID field to the logger in ctx.
WithTraceID returns a context with trace_id attached.
WithWarnLevel returns context with a warning level enabled logger.
# Structs
Config serializes log related config in toml/json.
No description provided by the author
FileLogConfig serializes file log related config in toml/json.
MLogger is a wrapper type of zap.Logger.
ZapProperties records some information about zap.