redesign logger (#1272)

* redesign logger

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-02-29 03:31:59 +03:00 committed by GitHub
parent afe6861e2f
commit d0a978bd50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 21 deletions

View File

@ -13,7 +13,6 @@ import (
type defaultLogger struct { type defaultLogger struct {
sync.RWMutex sync.RWMutex
opts Options opts Options
err error
} }
// Init(opts...) should only overwrite provided options // Init(opts...) should only overwrite provided options
@ -35,13 +34,6 @@ func (l *defaultLogger) Fields(fields map[string]interface{}) Logger {
return l return l
} }
func (l *defaultLogger) Error(err error) Logger {
l.Lock()
l.err = err
l.Unlock()
return l
}
func copyFields(src map[string]interface{}) map[string]interface{} { func copyFields(src map[string]interface{}) map[string]interface{} {
dst := make(map[string]interface{}, len(src)) dst := make(map[string]interface{}, len(src))
for k, v := range src { for k, v := range src {
@ -58,9 +50,6 @@ func (l *defaultLogger) Log(level Level, v ...interface{}) {
l.RLock() l.RLock()
fields := copyFields(l.opts.Fields) fields := copyFields(l.opts.Fields)
if l.err != nil {
fields["error"] = l.err.Error()
}
l.RUnlock() l.RUnlock()
fields["level"] = level.String() fields["level"] = level.String()
@ -88,9 +77,6 @@ func (l *defaultLogger) Logf(level Level, format string, v ...interface{}) {
l.RLock() l.RLock()
fields := copyFields(l.opts.Fields) fields := copyFields(l.opts.Fields)
if l.err != nil {
fields["error"] = l.err.Error()
}
l.RUnlock() l.RUnlock()
fields["level"] = level.String() fields["level"] = level.String()

61
logger/helper.go Normal file
View File

@ -0,0 +1,61 @@
package logger
type Helper struct {
Logger
}
func NewHelper(log Logger) *Helper {
return &Helper{log}
}
func (h *Helper) Info(args ...interface{}) {
h.Logger.Log(InfoLevel, args...)
}
func (h *Helper) Infof(template string, args ...interface{}) {
h.Logger.Logf(InfoLevel, template, args...)
}
func (h *Helper) Trace(args ...interface{}) {
h.Logger.Log(TraceLevel, args...)
}
func (h *Helper) Tracef(template string, args ...interface{}) {
h.Logger.Logf(TraceLevel, template, args...)
}
func (h *Helper) Debug(args ...interface{}) {
h.Logger.Log(DebugLevel, args...)
}
func (h *Helper) Debugf(template string, args ...interface{}) {
h.Logger.Logf(DebugLevel, template, args...)
}
func (h *Helper) Warn(args ...interface{}) {
h.Logger.Log(WarnLevel, args...)
}
func (h *Helper) Warnf(template string, args ...interface{}) {
h.Logger.Logf(WarnLevel, template, args...)
}
func (h *Helper) Error(args ...interface{}) {
h.Logger.Log(ErrorLevel, args...)
}
func (h *Helper) Errorf(template string, args ...interface{}) {
h.Logger.Logf(ErrorLevel, template, args...)
}
func (h *Helper) Fatal(args ...interface{}) {
h.Logger.Log(ErrorLevel, args...)
}
func (h *Helper) Fatalf(template string, args ...interface{}) {
h.Logger.Logf(ErrorLevel, template, args...)
}
func (h *Helper) WithError(err error) *Helper {
return &Helper{h.Logger.Fields(map[string]interface{}{"error": err})}
}

View File

@ -3,7 +3,7 @@ package logger
var ( var (
// Default logger // Default logger
DefaultLogger Logger = NewLogger() DefaultLogger Logger = NewHelper(NewLogger())
) )
// Logger is a generic logging interface // Logger is a generic logging interface
@ -12,8 +12,6 @@ type Logger interface {
Init(options ...Option) error Init(options ...Option) error
// The Logger options // The Logger options
Options() Options Options() Options
// Error set `error` field to be logged
Error(err error) Logger
// Fields set fields to always be logged // Fields set fields to always be logged
Fields(fields map[string]interface{}) Logger Fields(fields map[string]interface{}) Logger
// Log writes a log entry // Log writes a log entry
@ -43,7 +41,3 @@ func Logf(level Level, format string, v ...interface{}) {
func String() string { func String() string {
return DefaultLogger.String() return DefaultLogger.String()
} }
func WithError(err error) Logger {
return DefaultLogger.Error(err)
}

9
logger/logger_test.go Normal file
View File

@ -0,0 +1,9 @@
package logger
import "testing"
func TestLogger(t *testing.T) {
l := NewLogger(WithLevel(TraceLevel))
h := NewHelper(l)
h.Trace("trace level")
}