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"
|
2023-07-29 00:40:58 +03:00
|
|
|
|
|
|
|
"go.unistack.org/micro/v4/options"
|
2020-02-20 23:57:59 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type defaultLogger struct {
|
2023-01-30 00:29:49 +03:00
|
|
|
enc *json.Encoder
|
|
|
|
opts Options
|
2022-05-03 14:46:03 +03:00
|
|
|
sync.RWMutex
|
2020-02-20 23:57:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init(opts...) should only overwrite provided options
|
2023-07-29 00:40:58 +03:00
|
|
|
func (l *defaultLogger) Init(opts ...options.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-07-05 22:32:47 +03:00
|
|
|
// wrap the Log func
|
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
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (l *defaultLogger) Clone(opts ...options.Option) Logger {
|
2021-08-30 16:19:35 +03:00
|
|
|
newopts := NewOptions(opts...)
|
|
|
|
oldopts := l.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&newopts)
|
|
|
|
o(&oldopts)
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Lock()
|
2023-01-30 00:29:49 +03:00
|
|
|
cl := &defaultLogger{opts: oldopts, enc: json.NewEncoder(l.opts.Out)}
|
2021-08-30 16:19:35 +03:00
|
|
|
l.Unlock()
|
|
|
|
|
|
|
|
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 {
|
2023-01-30 00:17:29 +03:00
|
|
|
l.RLock()
|
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 {
|
2023-01-30 00:17:29 +03:00
|
|
|
l.RUnlock()
|
2021-08-06 02:15:57 +03:00
|
|
|
return nl
|
|
|
|
} else if len(fields)%2 != 0 {
|
|
|
|
fields = fields[:len(fields)-1]
|
2021-03-06 23:26:47 +03:00
|
|
|
}
|
2023-01-30 00:17:29 +03:00
|
|
|
nl.opts.Fields = copyFields(l.opts.Fields)
|
2021-09-28 23:43:43 +03:00
|
|
|
nl.opts.Fields = append(nl.opts.Fields, fields...)
|
2023-01-30 00:17:29 +03:00
|
|
|
l.RUnlock()
|
2021-03-06 23:26:47 +03:00
|
|
|
return nl
|
2020-02-20 23:57:59 -08: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 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{}) {
|
2023-01-30 00:29:49 +03:00
|
|
|
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{}) {
|
2023-01-30 00:29:49 +03:00
|
|
|
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{}) {
|
2023-01-30 00:29:49 +03:00
|
|
|
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{}) {
|
2023-01-30 00:29:49 +03:00
|
|
|
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{}) {
|
2023-01-30 00:29:49 +03:00
|
|
|
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{}) {
|
2023-01-30 00:29:49 +03:00
|
|
|
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{}) {
|
2023-01-30 00:29:49 +03:00
|
|
|
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{}) {
|
2023-01-30 00:29:49 +03:00
|
|
|
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{}) {
|
2023-01-30 00:29:49 +03:00
|
|
|
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{}) {
|
2023-01-30 00:29:49 +03:00
|
|
|
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{}) {
|
2023-01-30 00:29:49 +03:00
|
|
|
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{}) {
|
2023-01-30 00:29:49 +03:00
|
|
|
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()
|
|
|
|
|
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-20 23:57:59 -08: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-20 23:57:59 -08: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-20 23:57:59 -08:00
|
|
|
}
|
|
|
|
|
2020-07-19 23:55:50 +08:00
|
|
|
func (l *defaultLogger) Options() Options {
|
2021-08-06 02:15:57 +03:00
|
|
|
return l.opts
|
2020-02-20 23:57:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLogger builds a new logger based on options
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewLogger(opts ...options.Option) Logger {
|
2021-07-24 15:22:01 +03:00
|
|
|
l := &defaultLogger{
|
|
|
|
opts: NewOptions(opts...),
|
|
|
|
}
|
2020-11-04 00:38:12 +03:00
|
|
|
l.enc = json.NewEncoder(l.opts.Out)
|
2020-02-20 23:57:59 -08:00
|
|
|
return l
|
|
|
|
}
|