MSP-650 remove old logger
This commit is contained in:
1 parent
0f002b5633
commit
67d0512c6b
5 files changed
-251
No files matched your search
@@ -1,25 +0,0 @@
|
||||
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)
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
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)
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
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()
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
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
@@ -1,106 +0,0 @@
|
||||
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