MSP-325 add new logger
This commit is contained in:
1 parent
71e78d01e7
commit
05bdf75e95
7 files changed
+259
No files matched your search
@@ -18,7 +18,9 @@ require (
|
||||
github.com/spf13/cast v1.4.0
|
||||
github.com/stretchr/testify v1.7.0
|
||||
github.com/valyala/fasthttp v1.28.0
|
||||
go.uber.org/zap v1.17.0
|
||||
golang.org/x/net v0.0.0-20210510120150-4163338589ed
|
||||
google.golang.org/grpc v1.38.0
|
||||
google.golang.org/protobuf v1.26.0
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0
|
||||
)
|
||||
@@ -49,6 +49,7 @@ github.com/Azure/go-autorest/autorest/to v0.4.0/go.mod h1:fE8iZBn7LQR7zH/9XU2NcP
|
||||
github.com/Azure/go-autorest/autorest/validation v0.3.1/go.mod h1:yhLgjC0Wda5DYXl6JAsWyUe4KVNffhoDhG0zVzUMo3E=
|
||||
github.com/Azure/go-autorest/logger v0.2.1/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZmbF5NWuPV8+WeEW8=
|
||||
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
|
||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
github.com/DataDog/datadog-go v2.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
|
||||
@@ -757,14 +758,17 @@ go.opentelemetry.io/otel/trace v0.19.0 h1:1ucYlenXIDA1OlHVLDZKX0ObXV5RLaq06DtUKz
|
||||
go.opentelemetry.io/otel/trace v0.19.0/go.mod h1:4IXiNextNOpPnRlI4ryK69mn5iC84bjBWZQA5DXz/qg=
|
||||
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
|
||||
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
|
||||
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
|
||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
|
||||
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
|
||||
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
|
||||
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
|
||||
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
||||
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
|
||||
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
|
||||
go.uber.org/zap v1.14.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
|
||||
go.uber.org/zap v1.17.0 h1:MTjgFu6ZLKvY6Pvaqk97GlxNBuMpV4Hy/3P6tRGlI2U=
|
||||
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
|
||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
@@ -1189,6 +1193,8 @@ gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/R
|
||||
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
|
||||
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
|
||||
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type contextLogKey int
|
||||
|
||||
var (
|
||||
contextKey = contextLogKey(-1)
|
||||
)
|
||||
|
||||
func ContextLogValues(ctx context.Context) []zap.Field {
|
||||
if value, ok := ctx.Value(contextKey).([]zap.Field); ok {
|
||||
return value
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ToContext(ctx context.Context, kvs ...zap.Field) context.Context {
|
||||
existedValues := append(ContextLogValues(ctx), kvs...)
|
||||
return context.WithValue(ctx, contextKey, existedValues)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type Logger interface {
|
||||
Info(ctx context.Context, message interface{}, fields ...zap.Field)
|
||||
Error(ctx context.Context, message interface{}, fields ...zap.Field)
|
||||
Debug(ctx context.Context, message interface{}, fields ...zap.Field)
|
||||
Log(ctx context.Context, level Level, message interface{}, fields ...zap.Field)
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
type Level zapcore.Level
|
||||
|
||||
const (
|
||||
ErrorLevel = Level(zap.ErrorLevel)
|
||||
InfoLevel = Level(zap.InfoLevel)
|
||||
DebugLevel = Level(zap.DebugLevel)
|
||||
)
|
||||
|
||||
// nolint: maligned
|
||||
type Adapter struct {
|
||||
devMode bool
|
||||
rotation *Rotation
|
||||
initialLevel Level
|
||||
|
||||
logger *zap.Logger
|
||||
level zap.AtomicLevel
|
||||
}
|
||||
|
||||
func New(opts ...Option) (*Adapter, error) {
|
||||
a := &Adapter{
|
||||
devMode: false,
|
||||
rotation: nil,
|
||||
initialLevel: InfoLevel,
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(a)
|
||||
}
|
||||
|
||||
cfg := zap.NewProductionConfig()
|
||||
if a.devMode {
|
||||
cfg = zap.NewDevelopmentConfig()
|
||||
}
|
||||
if a.rotation != nil {
|
||||
outputUrl := rotationToUrl(*a.rotation)
|
||||
cfg.OutputPaths = []string{outputUrl.String()}
|
||||
}
|
||||
level := zap.NewAtomicLevelAt(zapcore.Level(a.initialLevel))
|
||||
cfg.Level = level
|
||||
logger, err := cfg.Build(zap.WithCaller(false), zap.AddStacktrace(zap.FatalLevel))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "build logger")
|
||||
}
|
||||
a.logger = logger
|
||||
a.level = level
|
||||
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func (a *Adapter) Info(ctx context.Context, message interface{}, fields ...zap.Field) {
|
||||
a.Log(ctx, InfoLevel, message, fields...)
|
||||
}
|
||||
|
||||
func (a *Adapter) Error(ctx context.Context, message interface{}, fields ...zap.Field) {
|
||||
a.Log(ctx, ErrorLevel, message, fields...)
|
||||
}
|
||||
|
||||
func (a *Adapter) Debug(ctx context.Context, message interface{}, fields ...zap.Field) {
|
||||
a.Log(ctx, DebugLevel, message, fields...)
|
||||
}
|
||||
|
||||
func (a *Adapter) Log(ctx context.Context, level Level, message interface{}, fields ...zap.Field) {
|
||||
entry := a.logger.Check(zapcore.Level(level), fmt.Sprintf("%v", message))
|
||||
if entry != nil {
|
||||
entry.Write(append(ContextLogValues(ctx), fields...)...)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Adapter) SetLevel(level Level) {
|
||||
a.level.SetLevel(zapcore.Level(level))
|
||||
}
|
||||
|
||||
func (a *Adapter) Close() error {
|
||||
return a.logger.Sync()
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package log
|
||||
|
||||
type Option func(a *Adapter)
|
||||
|
||||
func WithDevelopmentMode() Option {
|
||||
return func(a *Adapter) {
|
||||
a.devMode = true
|
||||
}
|
||||
}
|
||||
|
||||
func WithFileRotation(r Rotation) Option {
|
||||
return func(a *Adapter) {
|
||||
a.rotation = &r
|
||||
}
|
||||
}
|
||||
|
||||
func WithLevel(level Level) Option {
|
||||
return func(a *Adapter) {
|
||||
a.initialLevel = level
|
||||
}
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"go.uber.org/zap"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
|
||||
const (
|
||||
lumberjackSchema = "lumberjack"
|
||||
)
|
||||
|
||||
func init() {
|
||||
err := zap.RegisterSink(lumberjackSchema, func(u *url.URL) (zap.Sink, error) {
|
||||
r, err := rotationFromUrl(u)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "unmarshal rotation config")
|
||||
}
|
||||
return newSink(NewFileRotationWriter(*r)), nil
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
type Rotation struct {
|
||||
File string
|
||||
MaxSizeMb int
|
||||
MaxDays int
|
||||
MaxBackups int
|
||||
Compress bool
|
||||
}
|
||||
|
||||
func rotationToUrl(r Rotation) url.URL {
|
||||
values := url.Values{
|
||||
"file": {r.File},
|
||||
"maxSizeMb": {strconv.Itoa(r.MaxSizeMb)},
|
||||
"maxDays": {strconv.Itoa(r.MaxDays)},
|
||||
"maxBackups": {strconv.Itoa(r.MaxBackups)},
|
||||
"compress": {strconv.FormatBool(r.Compress)},
|
||||
}
|
||||
u := url.URL{
|
||||
Scheme: lumberjackSchema,
|
||||
RawQuery: values.Encode(),
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
func rotationFromUrl(u *url.URL) (*Rotation, error) {
|
||||
values, err := url.ParseQuery(u.RawQuery)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "parse lumberjack params")
|
||||
}
|
||||
file := values.Get("file")
|
||||
maxSizeMb, err := strconv.Atoi(values.Get("maxSizeMb"))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "parse maxSizeMb")
|
||||
}
|
||||
maxDays, err := strconv.Atoi(values.Get("maxDays"))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "parse maxDays")
|
||||
}
|
||||
maxBackups, err := strconv.Atoi(values.Get("maxBackups"))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "parse maxBackups")
|
||||
}
|
||||
compress, err := strconv.ParseBool(values.Get("compress"))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "parse compress")
|
||||
}
|
||||
return &Rotation{
|
||||
File: file,
|
||||
MaxSizeMb: maxSizeMb,
|
||||
MaxDays: maxDays,
|
||||
MaxBackups: maxBackups,
|
||||
Compress: compress,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func NewFileRotationWriter(r Rotation) io.WriteCloser {
|
||||
return &lumberjack.Logger{
|
||||
Filename: r.File,
|
||||
MaxSize: r.MaxSizeMb,
|
||||
MaxAge: r.MaxDays,
|
||||
MaxBackups: r.MaxBackups,
|
||||
Compress: r.Compress,
|
||||
}
|
||||
}
|
||||
|
||||
type sink struct {
|
||||
io.WriteCloser
|
||||
}
|
||||
|
||||
func (s sink) Sync() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func newSink(writer io.WriteCloser) sink {
|
||||
return sink{
|
||||
WriteCloser: writer,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user