2020-02-20 23:57:59 -08: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-20 23:57:59 -08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-04-26 17:11:53 +03:00
|
|
|
"runtime"
|
2020-04-27 10:36:09 +02:00
|
|
|
"strings"
|
2020-02-24 16:07:40 +03:00
|
|
|
"sync"
|
2020-02-23 16:45:20 +03:00
|
|
|
"time"
|
2020-02-20 23:57:59 -08: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-20 23:57:59 -08:00
|
|
|
type defaultLogger struct {
|
2020-02-24 16:07:40 +03:00
|
|
|
sync.RWMutex
|
2020-02-20 23:57:59 -08:00
|
|
|
opts Options
|
2020-11-04 00:38:12 +03:00
|
|
|
enc *json.Encoder
|
2020-02-20 23:57:59 -08: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-20 23:57:59 -08:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&l.opts)
|
|
|
|
}
|
2020-11-04 00:38:12 +03:00
|
|
|
l.enc = json.NewEncoder(l.opts.Out)
|
2021-01-10 18:56:39 +03:00
|
|
|
l.Unlock()
|
2020-02-20 23:57:59 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *defaultLogger) String() string {
|
2020-11-04 00:38:12 +03:00
|
|
|
return "micro"
|
2020-02-20 23:57:59 -08: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-20 23:57:59 -08:00
|
|
|
func (l *defaultLogger) Fields(fields map[string]interface{}) Logger {
|
2020-02-24 16:07:40 +03:00
|
|
|
l.Lock()
|
|
|
|
l.opts.Fields = copyFields(fields)
|
|
|
|
l.Unlock()
|
2020-02-20 23:57:59 -08:00
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
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 14:33:56 +08:00
|
|
|
// logCallerfilePath returns a package/file:line description of the caller,
|
|
|
|
// preserving only the leaf directory name and file name.
|
2020-04-27 10:55:50 +02:00
|
|
|
func logCallerfilePath(loggingFilePath string) string {
|
2020-05-26 14:33:56 +08: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 10:36:09 +02: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-20 23:57:59 -08:00
|
|
|
}
|
|
|
|
|
2021-01-10 18:56:39 +03:00
|
|
|
func (l *defaultLogger) Infof(ctx context.Context, msg string, args ...interface{}) {
|
|
|
|
l.Logf(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{}) {
|
|
|
|
l.Logf(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{}) {
|
|
|
|
l.Logf(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{}) {
|
|
|
|
l.Logf(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{}) {
|
|
|
|
l.Logf(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{}) {
|
|
|
|
l.Logf(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")
|
|
|
|
fields["msg"] = fmt.Sprint(args...)
|
|
|
|
|
|
|
|
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-20 23:57:59 -08: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-20 23:57:59 -08: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...)
|
|
|
|
} else {
|
|
|
|
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-20 23:57:59 -08:00
|
|
|
}
|
|
|
|
|
2020-07-19 23:55:50 +08:00
|
|
|
func (l *defaultLogger) Options() Options {
|
2020-04-26 17:41:36 +03:00
|
|
|
// not guard against options Context values
|
2020-07-19 23:55:50 +08: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-20 23:57:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLogger builds a new logger based on options
|
|
|
|
func NewLogger(opts ...Option) Logger {
|
2020-11-04 00:38:12 +03:00
|
|
|
l := &defaultLogger{opts: NewOptions(opts...)}
|
|
|
|
l.enc = json.NewEncoder(l.opts.Out)
|
2020-02-20 23:57:59 -08:00
|
|
|
return l
|
|
|
|
}
|