# Packages
No description provided by the author
# README
Log
simple log wrapper based on kratos log.
Characteristic
Caller
- auto get line numberFormat
- print normal str or format str by Info/Warn/Error, no need Infof/Warnf/ErrorfWithContext
- support OpenTelemetry log tracking by WithContextWithError
- print err field by WithErrorWithField
- print custom field by WithField/WithFieldsCustom Plugin
- support add custom log plugin, such as logrus/zap
Usage
go get github.com/go-cinch/common/log
import (
"context"
"fmt"
"github.com/go-cinch/common/log"
"github.com/go-cinch/common/log/caller"
"github.com/go-kratos/kratos/v2/middleware/tracing"
)
func main() {
// 1. use default wrapper
log.Info("test info")
log.WithField("field1", 1).Info("test info with 1 field")
log.WithField("field1", 1).Info("test info with 1 field and format %s", "yes")
log.WithFields(log.Fields{"field1": 1, "filed2": 2}).Info("test info with 2 field")
log.WithFields(log.Fields{"field1": 1, "filed2": 2}).Info("test info with 2 field and format %d", 1)
log.WithContext(context.WithValue(context.Background(), "ctx", "ctx")).Info("test info with ctx")
log.WithContext(context.WithValue(context.Background(), "ctx", "ctx")).Info("test info with ctx and format %v", fmt.Errorf("error"))
// 2. override default wrapper
logOps := []func(*log.Options){
log.WithJSON(false),
log.WithLevel(log.WarnLevel),
log.WithValuer("custom.field", "ok"),
log.WithValuer("trace.id", tracing.TraceID()),
log.WithValuer("span.id", tracing.SpanID()),
log.WithSkipEmpty(false),
log.WithCallerOptions(
caller.WithSource(false),
caller.WithLevel(2),
caller.WithVersion(true),
),
}
log.DefaultWrapper = log.NewWrapper(logOps...)
// only print test warn
log.Info("test info(not print)")
log.Warn("test warn")
}
Options
- `WithLevel - log level, default debug
WithLogger
- kratos logger, default kratosLog.DefaultLoggerWithLoggerMessageKey
- msg key, default msg