2020-02-21 10:57:59 +03:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
2021-01-10 18:56:39 +03:00
|
|
|
"context"
|
2020-11-04 00:38:12 +03:00
|
|
|
"encoding/json"
|
2020-02-21 10:57:59 +03:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-04-26 17:11:53 +03:00
|
|
|
"runtime"
|
2020-04-27 11:36:09 +03:00
|
|
|
"strings"
|
2020-02-24 16:07:40 +03:00
|
|
|
"sync"
|
2020-02-23 16:45:20 +03:00
|
|
|
"time"
|
2020-02-21 10:57:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type defaultLogger struct {
|
2021-09-30 20:32:59 +03:00
|
|
|
enc *json.Encoder
|
2021-07-05 22:32:47 +03:00
|
|
|
logFunc LogFunc
|
|
|
|
logfFunc LogfFunc
|
2021-09-30 20:32:59 +03:00
|
|
|
opts Options
|
|
|
|
sync.RWMutex
|
2020-02-21 10:57:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init(opts...) should only overwrite provided options
|
|
|
|
func (l *defaultLogger) Init(opts ...Option) error {
|
2020-11-04 00:38:12 +03:00
|
|
|
l.Lock()
|
2020-02-21 10:57:59 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&l.opts)
|
|
|
|
}
|
2020-11-04 00:38:12 +03:00
|
|
|
l.enc = json.NewEncoder(l.opts.Out)
|
2021-07-05 22:32:47 +03:00
|
|
|
// wrap the Log func
|
|
|
|
for i := len(l.opts.Wrappers); i > 0; i-- {
|
|
|
|
l.logFunc = l.opts.Wrappers[i-1].Log(l.logFunc)
|
|
|
|
l.logfFunc = l.opts.Wrappers[i-1].Logf(l.logfFunc)
|
|
|
|
}
|
2021-01-10 18:56:39 +03:00
|
|
|
l.Unlock()
|
2020-02-21 10:57:59 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *defaultLogger) String() string {
|
2020-11-04 00:38:12 +03:00
|
|
|
return "micro"
|
2020-02-21 10:57:59 +03:00
|
|
|
}
|
|
|
|
|
2021-08-30 16:19:35 +03:00
|
|
|
func (l *defaultLogger) Clone(opts ...Option) Logger {
|
|
|
|
newopts := NewOptions(opts...)
|
|
|
|
oldopts := l.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&newopts)
|
|
|
|
o(&oldopts)
|
|
|
|
}
|
|
|
|
|
|
|
|
oldopts.Wrappers = newopts.Wrappers
|
|
|
|
l.Lock()
|
|
|
|
cl := &defaultLogger{opts: oldopts, logFunc: l.logFunc, logfFunc: l.logfFunc}
|
|
|
|
l.Unlock()
|
|
|
|
|
|
|
|
// wrap the Log func
|
|
|
|
for i := len(newopts.Wrappers); i > 0; i-- {
|
|
|
|
cl.logFunc = newopts.Wrappers[i-1].Log(cl.logFunc)
|
|
|
|
cl.logfFunc = newopts.Wrappers[i-1].Logf(cl.logfFunc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cl
|
|
|
|
}
|
|
|
|
|
2020-08-29 17:43:06 +03:00
|
|
|
func (l *defaultLogger) V(level Level) bool {
|
2021-01-10 18:56:39 +03:00
|
|
|
l.RLock()
|
|
|
|
ok := l.opts.Level.Enabled(level)
|
|
|
|
l.RUnlock()
|
|
|
|
return ok
|
2020-08-29 17:43:06 +03:00
|
|
|
}
|
|
|
|
|
2021-08-30 16:19:35 +03:00
|
|
|
func (l *defaultLogger) Level(level Level) {
|
|
|
|
l.Lock()
|
|
|
|
l.opts.Level = level
|
|
|
|
l.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-08-06 02:15:57 +03:00
|
|
|
func (l *defaultLogger) Fields(fields ...interface{}) Logger {
|
2021-03-06 23:26:47 +03:00
|
|
|
nl := &defaultLogger{opts: l.opts, enc: l.enc}
|
2021-08-06 02:15:57 +03:00
|
|
|
if len(fields) == 0 {
|
|
|
|
return nl
|
|
|
|
} else if len(fields)%2 != 0 {
|
|
|
|
fields = fields[:len(fields)-1]
|
2021-03-06 23:26:47 +03:00
|
|
|
}
|
2022-01-19 19:54:33 +03:00
|
|
|
nl.logFunc = l.logFunc
|
|
|
|
nl.logfFunc = l.logfFunc
|
2021-09-28 23:43:43 +03:00
|
|
|
nl.opts.Fields = append(nl.opts.Fields, fields...)
|
2021-03-06 23:26:47 +03:00
|
|
|
return nl
|
2020-02-21 10:57:59 +03:00
|
|
|
}
|
|
|
|
|
2021-08-06 02:15:57 +03:00
|
|
|
func copyFields(src []interface{}) []interface{} {
|
|
|
|
dst := make([]interface{}, len(src))
|
|
|
|
copy(dst, src)
|
2020-02-24 16:07:40 +03:00
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2020-05-26 09:33:56 +03:00
|
|
|
// logCallerfilePath returns a package/file:line description of the caller,
|
|
|
|
// preserving only the leaf directory name and file name.
|
2020-04-27 11:55:50 +03:00
|
|
|
func logCallerfilePath(loggingFilePath string) string {
|
2020-05-26 09:33:56 +03:00
|
|
|
// To make sure we trim the path correctly on Windows too, we
|
|
|
|
// counter-intuitively need to use '/' and *not* os.PathSeparator here,
|
|
|
|
// because the path given originates from Go stdlib, specifically
|
|
|
|
// runtime.Caller() which (as of Mar/17) returns forward slashes even on
|
|
|
|
// Windows.
|
|
|
|
//
|
|
|
|
// See https://github.com/golang/go/issues/3335
|
|
|
|
// and https://github.com/golang/go/issues/18151
|
|
|
|
//
|
|
|
|
// for discussion on the issue on Go side.
|
|
|
|
idx := strings.LastIndexByte(loggingFilePath, '/')
|
|
|
|
if idx == -1 {
|
|
|
|
return loggingFilePath
|
|
|
|
}
|
|
|
|
idx = strings.LastIndexByte(loggingFilePath[:idx], '/')
|
|
|
|
if idx == -1 {
|
|
|
|
return loggingFilePath
|
|
|
|
}
|
|
|
|
return loggingFilePath[idx+1:]
|
2020-04-27 11:36:09 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 18:56:39 +03:00
|
|
|
func (l *defaultLogger) Info(ctx context.Context, args ...interface{}) {
|
|
|
|
l.Log(ctx, InfoLevel, args...)
|
2020-11-04 00:38:12 +03:00
|
|
|
}
|
2020-04-26 17:11:53 +03:00
|
|
|
|
2021-01-10 18:56:39 +03:00
|
|
|
func (l *defaultLogger) Error(ctx context.Context, args ...interface{}) {
|
|
|
|
l.Log(ctx, ErrorLevel, args...)
|
2020-11-04 00:38:12 +03:00
|
|
|
}
|
2020-03-03 11:07:38 +03:00
|
|
|
|
2021-01-10 18:56:39 +03:00
|
|
|
func (l *defaultLogger) Debug(ctx context.Context, args ...interface{}) {
|
|
|
|
l.Log(ctx, DebugLevel, args...)
|
2020-11-04 00:38:12 +03:00
|
|
|
}
|
2020-02-23 16:45:20 +03:00
|
|
|
|
2021-01-10 18:56:39 +03:00
|
|
|
func (l *defaultLogger) Warn(ctx context.Context, args ...interface{}) {
|
|
|
|
l.Log(ctx, WarnLevel, args...)
|
2020-11-04 00:38:12 +03:00
|
|
|
}
|
2020-03-03 11:07:38 +03:00
|
|
|
|
2021-01-10 18:56:39 +03:00
|
|
|
func (l *defaultLogger) Trace(ctx context.Context, args ...interface{}) {
|
|
|
|
l.Log(ctx, TraceLevel, args...)
|
2020-11-04 00:38:12 +03:00
|
|
|
}
|
2020-03-03 11:07:38 +03:00
|
|
|
|
2021-01-10 18:56:39 +03:00
|
|
|
func (l *defaultLogger) Fatal(ctx context.Context, args ...interface{}) {
|
|
|
|
l.Log(ctx, FatalLevel, args...)
|
2020-11-04 00:38:12 +03:00
|
|
|
os.Exit(1)
|
2020-02-21 10:57:59 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 18:56:39 +03:00
|
|
|
func (l *defaultLogger) Infof(ctx context.Context, msg string, args ...interface{}) {
|
2021-07-05 22:32:47 +03:00
|
|
|
l.logfFunc(ctx, InfoLevel, msg, args...)
|
2020-11-10 22:18:16 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 18:56:39 +03:00
|
|
|
func (l *defaultLogger) Errorf(ctx context.Context, msg string, args ...interface{}) {
|
2021-07-05 22:32:47 +03:00
|
|
|
l.logfFunc(ctx, ErrorLevel, msg, args...)
|
2020-11-10 22:18:16 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 18:56:39 +03:00
|
|
|
func (l *defaultLogger) Debugf(ctx context.Context, msg string, args ...interface{}) {
|
2021-07-05 22:32:47 +03:00
|
|
|
l.logfFunc(ctx, DebugLevel, msg, args...)
|
2020-11-10 22:18:16 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 18:56:39 +03:00
|
|
|
func (l *defaultLogger) Warnf(ctx context.Context, msg string, args ...interface{}) {
|
2021-07-05 22:32:47 +03:00
|
|
|
l.logfFunc(ctx, WarnLevel, msg, args...)
|
2020-11-10 22:18:16 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 18:56:39 +03:00
|
|
|
func (l *defaultLogger) Tracef(ctx context.Context, msg string, args ...interface{}) {
|
2021-07-05 22:32:47 +03:00
|
|
|
l.logfFunc(ctx, TraceLevel, msg, args...)
|
2020-11-10 22:18:16 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 18:56:39 +03:00
|
|
|
func (l *defaultLogger) Fatalf(ctx context.Context, msg string, args ...interface{}) {
|
2021-07-05 22:32:47 +03:00
|
|
|
l.logfFunc(ctx, FatalLevel, msg, args...)
|
2020-11-10 22:18:16 +03:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-01-10 18:56:39 +03:00
|
|
|
func (l *defaultLogger) Log(ctx context.Context, level Level, args ...interface{}) {
|
2020-11-10 22:18:16 +03:00
|
|
|
if !l.V(level) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l.RLock()
|
|
|
|
fields := copyFields(l.opts.Fields)
|
|
|
|
l.RUnlock()
|
|
|
|
|
2021-08-06 02:15:57 +03:00
|
|
|
fields = append(fields, "level", level.String())
|
2020-11-10 22:18:16 +03:00
|
|
|
|
|
|
|
if _, file, line, ok := runtime.Caller(l.opts.CallerSkipCount); ok {
|
2021-08-06 02:15:57 +03:00
|
|
|
fields = append(fields, "caller", fmt.Sprintf("%s:%d", logCallerfilePath(file), line))
|
2020-11-10 22:18:16 +03:00
|
|
|
}
|
2021-08-06 02:15:57 +03:00
|
|
|
fields = append(fields, "timestamp", time.Now().Format("2006-01-02 15:04:05"))
|
2020-11-10 22:18:16 +03:00
|
|
|
|
2021-03-06 23:26:47 +03:00
|
|
|
if len(args) > 0 {
|
2021-08-06 02:15:57 +03:00
|
|
|
fields = append(fields, "msg", fmt.Sprint(args...))
|
2021-03-06 23:26:47 +03:00
|
|
|
}
|
2020-11-10 22:18:16 +03:00
|
|
|
|
2021-08-06 02:15:57 +03:00
|
|
|
out := make(map[string]interface{}, len(fields)/2)
|
|
|
|
for i := 0; i < len(fields); i += 2 {
|
|
|
|
out[fields[i].(string)] = fields[i+1]
|
|
|
|
}
|
2020-11-10 22:18:16 +03:00
|
|
|
l.RLock()
|
2021-08-06 02:15:57 +03:00
|
|
|
_ = l.enc.Encode(out)
|
2020-11-10 22:18:16 +03:00
|
|
|
l.RUnlock()
|
|
|
|
}
|
|
|
|
|
2021-01-10 18:56:39 +03:00
|
|
|
func (l *defaultLogger) Logf(ctx context.Context, level Level, msg string, args ...interface{}) {
|
2020-08-29 17:43:06 +03:00
|
|
|
if !l.V(level) {
|
2020-02-21 10:57:59 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-24 16:07:40 +03:00
|
|
|
l.RLock()
|
|
|
|
fields := copyFields(l.opts.Fields)
|
|
|
|
l.RUnlock()
|
|
|
|
|
2021-08-06 02:15:57 +03:00
|
|
|
fields = append(fields, "level", level.String())
|
2020-02-21 10:57:59 +03:00
|
|
|
|
2020-04-26 17:11:53 +03:00
|
|
|
if _, file, line, ok := runtime.Caller(l.opts.CallerSkipCount); ok {
|
2021-08-06 02:15:57 +03:00
|
|
|
fields = append(fields, "caller", fmt.Sprintf("%s:%d", logCallerfilePath(file), line))
|
2020-02-23 16:45:20 +03:00
|
|
|
}
|
|
|
|
|
2021-08-06 02:15:57 +03:00
|
|
|
fields = append(fields, "timestamp", time.Now().Format("2006-01-02 15:04:05"))
|
2020-11-10 22:18:16 +03:00
|
|
|
if len(args) > 0 {
|
2021-08-06 02:15:57 +03:00
|
|
|
fields = append(fields, "msg", fmt.Sprintf(msg, args...))
|
2021-03-06 23:26:47 +03:00
|
|
|
} else if msg != "" {
|
2021-08-06 02:15:57 +03:00
|
|
|
fields = append(fields, "msg", msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make(map[string]interface{}, len(fields)/2)
|
|
|
|
for i := 0; i < len(fields); i += 2 {
|
|
|
|
out[fields[i].(string)] = fields[i+1]
|
2020-03-03 11:07:38 +03:00
|
|
|
}
|
2020-11-04 00:38:12 +03:00
|
|
|
l.RLock()
|
2021-08-06 02:15:57 +03:00
|
|
|
_ = l.enc.Encode(out)
|
2020-11-04 00:38:12 +03:00
|
|
|
l.RUnlock()
|
2020-02-21 10:57:59 +03:00
|
|
|
}
|
|
|
|
|
2020-07-19 18:55:50 +03:00
|
|
|
func (l *defaultLogger) Options() Options {
|
2021-08-06 02:15:57 +03:00
|
|
|
return l.opts
|
2020-02-21 10:57:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLogger builds a new logger based on options
|
|
|
|
func NewLogger(opts ...Option) Logger {
|
2021-07-24 15:22:01 +03:00
|
|
|
l := &defaultLogger{
|
|
|
|
opts: NewOptions(opts...),
|
|
|
|
}
|
|
|
|
l.logFunc = l.Log
|
|
|
|
l.logfFunc = l.Logf
|
2020-11-04 00:38:12 +03:00
|
|
|
l.enc = json.NewEncoder(l.opts.Out)
|
2020-02-21 10:57:59 +03:00
|
|
|
return l
|
|
|
|
}
|