2020-02-21 09:57:59 +02:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-04-26 16:11:53 +02:00
|
|
|
"runtime"
|
2020-03-03 10:07:38 +02:00
|
|
|
"sort"
|
2020-04-27 10:36:09 +02:00
|
|
|
"strings"
|
2020-02-24 15:07:40 +02:00
|
|
|
"sync"
|
2020-02-23 15:45:20 +02:00
|
|
|
"time"
|
|
|
|
|
2024-06-04 22:40:43 +02:00
|
|
|
dlog "go-micro.dev/v5/debug/log"
|
2020-02-21 09:57:59 +02:00
|
|
|
)
|
|
|
|
|
2020-03-26 00:00:43 +02:00
|
|
|
func init() {
|
|
|
|
lvl, err := GetLevel(os.Getenv("MICRO_LOG_LEVEL"))
|
|
|
|
if err != nil {
|
|
|
|
lvl = InfoLevel
|
|
|
|
}
|
|
|
|
|
2021-09-19 11:40:09 +02:00
|
|
|
DefaultLogger = NewLogger(WithLevel(lvl))
|
2020-03-26 00:00:43 +02:00
|
|
|
}
|
|
|
|
|
2020-02-21 09:57:59 +02:00
|
|
|
type defaultLogger struct {
|
|
|
|
opts Options
|
2023-04-26 02:16:34 +02:00
|
|
|
sync.RWMutex
|
2020-02-21 09:57:59 +02:00
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Init (opts...) should only overwrite provided options.
|
2020-02-21 09:57:59 +02:00
|
|
|
func (l *defaultLogger) Init(opts ...Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&l.opts)
|
|
|
|
}
|
2022-10-20 13:00:50 +02:00
|
|
|
|
2020-02-21 09:57:59 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *defaultLogger) String() string {
|
|
|
|
return "default"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *defaultLogger) Fields(fields map[string]interface{}) Logger {
|
2020-02-24 15:07:40 +02:00
|
|
|
l.Lock()
|
2021-09-19 11:40:09 +02:00
|
|
|
nfields := make(map[string]interface{}, len(l.opts.Fields))
|
2022-10-20 13:00:50 +02:00
|
|
|
|
2021-09-19 11:40:09 +02:00
|
|
|
for k, v := range l.opts.Fields {
|
|
|
|
nfields[k] = v
|
|
|
|
}
|
2020-02-24 15:07:40 +02:00
|
|
|
l.Unlock()
|
2021-09-19 11:40:09 +02:00
|
|
|
|
|
|
|
for k, v := range fields {
|
|
|
|
nfields[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return &defaultLogger{opts: Options{
|
|
|
|
Level: l.opts.Level,
|
|
|
|
Fields: nfields,
|
|
|
|
Out: l.opts.Out,
|
|
|
|
CallerSkipCount: l.opts.CallerSkipCount,
|
|
|
|
Context: l.opts.Context,
|
|
|
|
}}
|
2020-02-21 09:57:59 +02:00
|
|
|
}
|
|
|
|
|
2020-02-24 15:07:40 +02: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
|
|
|
|
}
|
2022-10-20 13:00:50 +02:00
|
|
|
|
2020-02-24 15:07:40 +02:00
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2020-05-26 08:33:56 +02: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 08:33:56 +02: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
|
|
|
|
}
|
2022-10-20 13:00:50 +02:00
|
|
|
|
2020-05-26 08:33:56 +02:00
|
|
|
idx = strings.LastIndexByte(loggingFilePath[:idx], '/')
|
2022-10-20 13:00:50 +02:00
|
|
|
|
2020-05-26 08:33:56 +02:00
|
|
|
if idx == -1 {
|
|
|
|
return loggingFilePath
|
|
|
|
}
|
2022-10-20 13:00:50 +02:00
|
|
|
|
2020-05-26 08:33:56 +02:00
|
|
|
return loggingFilePath[idx+1:]
|
2020-04-27 10:36:09 +02:00
|
|
|
}
|
|
|
|
|
2020-02-21 09:57:59 +02:00
|
|
|
func (l *defaultLogger) Log(level Level, v ...interface{}) {
|
2020-02-23 15:45:20 +02:00
|
|
|
// TODO decide does we need to write message if log level not used?
|
2020-02-21 09:57:59 +02:00
|
|
|
if !l.opts.Level.Enabled(level) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-24 15:07:40 +02:00
|
|
|
l.RLock()
|
|
|
|
fields := copyFields(l.opts.Fields)
|
|
|
|
l.RUnlock()
|
|
|
|
|
|
|
|
fields["level"] = level.String()
|
2020-02-21 09:57:59 +02:00
|
|
|
|
2020-04-26 16:11:53 +02:00
|
|
|
if _, file, line, ok := runtime.Caller(l.opts.CallerSkipCount); ok {
|
2020-04-27 10:55:50 +02:00
|
|
|
fields["file"] = fmt.Sprintf("%s:%d", logCallerfilePath(file), line)
|
2020-04-26 16:11:53 +02:00
|
|
|
}
|
|
|
|
|
2020-02-23 15:45:20 +02:00
|
|
|
rec := dlog.Record{
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
Message: fmt.Sprint(v...),
|
2020-02-24 15:07:40 +02:00
|
|
|
Metadata: make(map[string]string, len(fields)),
|
2020-02-21 09:57:59 +02:00
|
|
|
}
|
2020-03-03 10:07:38 +02:00
|
|
|
|
|
|
|
keys := make([]string, 0, len(fields))
|
2020-02-23 15:45:20 +02:00
|
|
|
for k, v := range fields {
|
2020-03-03 10:07:38 +02:00
|
|
|
keys = append(keys, k)
|
2020-02-23 15:45:20 +02:00
|
|
|
rec.Metadata[k] = fmt.Sprintf("%v", v)
|
|
|
|
}
|
|
|
|
|
2020-03-03 10:07:38 +02:00
|
|
|
sort.Strings(keys)
|
2022-10-20 13:00:50 +02:00
|
|
|
|
2020-03-03 10:07:38 +02:00
|
|
|
metadata := ""
|
|
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
metadata += fmt.Sprintf(" %s=%v", k, fields[k])
|
|
|
|
}
|
|
|
|
|
2020-02-23 15:45:20 +02:00
|
|
|
dlog.DefaultLog.Write(rec)
|
|
|
|
|
|
|
|
t := rec.Timestamp.Format("2006-01-02 15:04:05")
|
2020-03-03 10:07:38 +02:00
|
|
|
fmt.Printf("%s %s %v\n", t, metadata, rec.Message)
|
2020-02-21 09:57:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *defaultLogger) Logf(level Level, format string, v ...interface{}) {
|
2020-02-23 15:45:20 +02:00
|
|
|
// TODO decide does we need to write message if log level not used?
|
2022-09-29 16:44:53 +02:00
|
|
|
if !l.opts.Level.Enabled(level) {
|
2020-02-21 09:57:59 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-24 15:07:40 +02:00
|
|
|
l.RLock()
|
|
|
|
fields := copyFields(l.opts.Fields)
|
|
|
|
l.RUnlock()
|
|
|
|
|
|
|
|
fields["level"] = level.String()
|
2020-02-21 09:57:59 +02:00
|
|
|
|
2020-04-26 16:11:53 +02:00
|
|
|
if _, file, line, ok := runtime.Caller(l.opts.CallerSkipCount); ok {
|
2020-04-27 10:55:50 +02:00
|
|
|
fields["file"] = fmt.Sprintf("%s:%d", logCallerfilePath(file), line)
|
2020-04-26 16:11:53 +02:00
|
|
|
}
|
|
|
|
|
2020-02-23 15:45:20 +02:00
|
|
|
rec := dlog.Record{
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
Message: fmt.Sprintf(format, v...),
|
2020-02-24 15:07:40 +02:00
|
|
|
Metadata: make(map[string]string, len(fields)),
|
2020-02-21 09:57:59 +02:00
|
|
|
}
|
2020-03-03 10:07:38 +02:00
|
|
|
|
|
|
|
keys := make([]string, 0, len(fields))
|
2020-02-23 15:45:20 +02:00
|
|
|
for k, v := range fields {
|
2020-03-03 10:07:38 +02:00
|
|
|
keys = append(keys, k)
|
2020-02-23 15:45:20 +02:00
|
|
|
rec.Metadata[k] = fmt.Sprintf("%v", v)
|
|
|
|
}
|
|
|
|
|
2020-03-03 10:07:38 +02:00
|
|
|
sort.Strings(keys)
|
2022-10-20 13:00:50 +02:00
|
|
|
|
2020-03-03 10:07:38 +02:00
|
|
|
metadata := ""
|
|
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
metadata += fmt.Sprintf(" %s=%v", k, fields[k])
|
|
|
|
}
|
|
|
|
|
2020-02-23 15:45:20 +02:00
|
|
|
dlog.DefaultLog.Write(rec)
|
2020-02-21 09:57:59 +02:00
|
|
|
|
2020-02-23 15:45:20 +02:00
|
|
|
t := rec.Timestamp.Format("2006-01-02 15:04:05")
|
2020-03-03 10:07:38 +02:00
|
|
|
fmt.Printf("%s %s %v\n", t, metadata, rec.Message)
|
2020-02-21 09:57:59 +02:00
|
|
|
}
|
|
|
|
|
2021-05-23 09:38:20 +02:00
|
|
|
func (l *defaultLogger) Options() Options {
|
2020-04-26 16:41:36 +02:00
|
|
|
// not guard against options Context values
|
2021-05-23 09:38:20 +02:00
|
|
|
l.RLock()
|
2022-10-20 13:00:50 +02:00
|
|
|
defer l.RUnlock()
|
|
|
|
|
2021-05-23 09:38:20 +02:00
|
|
|
opts := l.opts
|
|
|
|
opts.Fields = copyFields(l.opts.Fields)
|
2022-10-20 13:00:50 +02:00
|
|
|
|
2020-04-26 16:41:36 +02:00
|
|
|
return opts
|
2020-02-21 09:57:59 +02:00
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// NewLogger builds a new logger based on options.
|
2020-02-21 09:57:59 +02:00
|
|
|
func NewLogger(opts ...Option) Logger {
|
|
|
|
// Default options
|
|
|
|
options := Options{
|
2020-04-26 16:11:53 +02:00
|
|
|
Level: InfoLevel,
|
|
|
|
Fields: make(map[string]interface{}),
|
|
|
|
Out: os.Stderr,
|
2020-04-27 10:36:09 +02:00
|
|
|
CallerSkipCount: 2,
|
2020-04-26 16:11:53 +02:00
|
|
|
Context: context.Background(),
|
2020-02-21 09:57:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
l := &defaultLogger{opts: options}
|
2020-02-24 15:07:40 +02:00
|
|
|
if err := l.Init(opts...); err != nil {
|
|
|
|
l.Log(FatalLevel, err)
|
|
|
|
}
|
|
|
|
|
2020-02-21 09:57:59 +02:00
|
|
|
return l
|
|
|
|
}
|