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
|
|
|
)
|
|
|
|
|
2020-03-26 01:00:43 +03:00
|
|
|
func init() {
|
|
|
|
lvl, err := GetLevel(os.Getenv("MICRO_LOG_LEVEL"))
|
|
|
|
if err != nil {
|
|
|
|
lvl = InfoLevel
|
|
|
|
}
|
|
|
|
|
2020-11-04 00:38:12 +03:00
|
|
|
DefaultLogger = NewLogger(WithLevel(lvl))
|
2020-03-26 01:00:43 +03:00
|
|
|
}
|
|
|
|
|
2020-02-21 10:57:59 +03:00
|
|
|
type defaultLogger struct {
|
2020-11-04 00:38:12 +03:00
|
|
|
enc *json.Encoder
|
2021-04-27 00:03:05 +03:00
|
|
|
opts Options
|
2021-03-06 19:45:13 +03:00
|
|
|
sync.RWMutex
|
2021-07-05 22:32:47 +03:00
|
|
|
logFunc LogFunc
|
|
|
|
logfFunc LogfFunc
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-21 10:57:59 +03:00
|
|
|
func (l *defaultLogger) Fields(fields map[string]interface{}) Logger {
|
2021-03-06 23:26:47 +03:00
|
|
|
nl := &defaultLogger{opts: l.opts, enc: l.enc}
|
|
|
|
nl.opts.Fields = make(map[string]interface{}, len(l.opts.Fields)+len(fields))
|
|
|
|
l.RLock()
|
|
|
|
for k, v := range l.opts.Fields {
|
|
|
|
nl.opts.Fields[k] = v
|
|
|
|
}
|
|
|
|
l.RUnlock()
|
|
|
|
|
|
|
|
for k, v := range fields {
|
|
|
|
nl.opts.Fields[k] = v
|
|
|
|
}
|
|
|
|
return nl
|
2020-02-21 10:57:59 +03:00
|
|
|
}
|
|
|
|
|
2020-02-24 16:07:40 +03:00
|
|
|
func copyFields(src map[string]interface{}) map[string]interface{} {
|
|
|
|
dst := make(map[string]interface{}, len(src))
|
|
|
|
for k, v := range src {
|
|
|
|
dst[k] = v
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
|
|
|
|
fields["level"] = level.String()
|
|
|
|
|
|
|
|
if _, file, line, ok := runtime.Caller(l.opts.CallerSkipCount); ok {
|
|
|
|
fields["caller"] = fmt.Sprintf("%s:%d", logCallerfilePath(file), line)
|
|
|
|
}
|
|
|
|
|
|
|
|
fields["timestamp"] = time.Now().Format("2006-01-02 15:04:05")
|
2021-03-06 23:26:47 +03:00
|
|
|
if len(args) > 0 {
|
|
|
|
fields["msg"] = fmt.Sprint(args...)
|
|
|
|
}
|
2020-11-10 22:18:16 +03:00
|
|
|
|
|
|
|
l.RLock()
|
|
|
|
_ = l.enc.Encode(fields)
|
|
|
|
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()
|
|
|
|
|
|
|
|
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 {
|
2020-11-04 00:38:12 +03:00
|
|
|
fields["caller"] = fmt.Sprintf("%s:%d", logCallerfilePath(file), line)
|
2020-02-23 16:45:20 +03:00
|
|
|
}
|
|
|
|
|
2020-11-04 00:38:12 +03:00
|
|
|
fields["timestamp"] = time.Now().Format("2006-01-02 15:04:05")
|
2020-11-10 22:18:16 +03:00
|
|
|
if len(args) > 0 {
|
|
|
|
fields["msg"] = fmt.Sprintf(msg, args...)
|
2021-03-06 23:26:47 +03:00
|
|
|
} else if msg != "" {
|
2020-11-10 22:18:16 +03:00
|
|
|
fields["msg"] = msg
|
2020-03-03 11:07:38 +03:00
|
|
|
}
|
2020-11-04 00:38:12 +03:00
|
|
|
l.RLock()
|
|
|
|
_ = l.enc.Encode(fields)
|
|
|
|
l.RUnlock()
|
2020-02-21 10:57:59 +03:00
|
|
|
}
|
|
|
|
|
2020-07-19 18:55:50 +03:00
|
|
|
func (l *defaultLogger) Options() Options {
|
2020-04-26 17:41:36 +03:00
|
|
|
// not guard against options Context values
|
2020-07-19 18:55:50 +03:00
|
|
|
l.RLock()
|
|
|
|
opts := l.opts
|
|
|
|
opts.Fields = copyFields(l.opts.Fields)
|
|
|
|
l.RUnlock()
|
2020-04-26 17:41:36 +03:00
|
|
|
return 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
|
|
|
|
}
|