zrepl/logger/logger.go

179 lines
3.6 KiB
Go
Raw Permalink Normal View History

package logger
import (
"fmt"
"runtime/debug"
"sync"
"time"
)
const (
// The field set by WithError function
FieldError = "err"
)
const DefaultUserFieldCapacity = 5
2018-08-10 17:06:00 +02:00
type Logger interface {
WithOutlet(outlet Outlet, level Level) Logger
ReplaceField(field string, val interface{}) Logger
WithField(field string, val interface{}) Logger
WithFields(fields Fields) Logger
WithError(err error) Logger
Debug(msg string)
Info(msg string)
Warn(msg string)
Error(msg string)
Printf(format string, args ...interface{})
}
type loggerImpl struct {
fields Fields
outlets *Outlets
outletTimeout time.Duration
mtx *sync.Mutex
}
2018-08-10 17:06:00 +02:00
var _ Logger = &loggerImpl{}
func NewLogger(outlets *Outlets, outletTimeout time.Duration) Logger {
return &loggerImpl{
make(Fields, DefaultUserFieldCapacity),
outlets,
outletTimeout,
&sync.Mutex{},
}
}
type outletResult struct {
Outlet Outlet
Error error
}
2018-08-10 17:06:00 +02:00
func (l *loggerImpl) logInternalError(outlet Outlet, err string) {
fields := Fields{}
if outlet != nil {
if _, ok := outlet.(fmt.Stringer); ok {
fields["outlet"] = fmt.Sprintf("%s", outlet)
}
fields["outlet_type"] = fmt.Sprintf("%T", outlet)
}
fields[FieldError] = err
entry := Entry{
Error,
"outlet error",
time.Now(),
fields,
}
l.outlets.GetLoggerErrorOutlet().WriteEntry(entry)
}
2018-08-10 17:06:00 +02:00
func (l *loggerImpl) log(level Level, msg string) {
l.mtx.Lock()
defer l.mtx.Unlock()
entry := Entry{level, msg, time.Now(), l.fields}
louts := l.outlets.Get(level)
ech := make(chan outletResult, len(louts))
for i := range louts {
go func(outlet Outlet, entry Entry) {
ech <- outletResult{outlet, outlet.WriteEntry(entry)}
}(louts[i], entry)
}
for fin := 0; fin < len(louts); fin++ {
res := <-ech
if res.Error != nil {
l.logInternalError(res.Outlet, res.Error.Error())
}
}
close(ech)
2017-09-30 16:39:52 +02:00
}
2018-08-10 17:06:00 +02:00
func (l *loggerImpl) WithOutlet(outlet Outlet, level Level) Logger {
l.mtx.Lock()
defer l.mtx.Unlock()
newOutlets := l.outlets.DeepCopy()
newOutlets.Add(outlet, level)
2018-08-10 17:06:00 +02:00
child := &loggerImpl{
fields: l.fields,
outlets: newOutlets,
outletTimeout: l.outletTimeout,
mtx: l.mtx,
}
return child
}
// callers must hold l.mtx
2018-08-10 17:06:00 +02:00
func (l *loggerImpl) forkLogger(field string, val interface{}) *loggerImpl {
2018-08-10 17:06:00 +02:00
child := &loggerImpl{
fields: make(Fields, len(l.fields)+1),
outlets: l.outlets,
outletTimeout: l.outletTimeout,
mtx: l.mtx,
}
for k, v := range l.fields {
child.fields[k] = v
}
child.fields[field] = val
return child
}
2018-08-10 17:06:00 +02:00
func (l *loggerImpl) ReplaceField(field string, val interface{}) Logger {
l.mtx.Lock()
defer l.mtx.Unlock()
return l.forkLogger(field, val)
}
2018-08-10 17:06:00 +02:00
func (l *loggerImpl) WithField(field string, val interface{}) Logger {
l.mtx.Lock()
defer l.mtx.Unlock()
if val, ok := l.fields[field]; ok && val != nil {
l.logInternalError(nil,
fmt.Sprintf("caller overwrites field '%s'. Stack: %s", field, string(debug.Stack())))
}
return l.forkLogger(field, val)
}
2018-08-10 17:06:00 +02:00
func (l *loggerImpl) WithFields(fields Fields) Logger {
// TODO optimize
2018-08-10 17:06:00 +02:00
var ret Logger = l
for field, value := range fields {
ret = ret.WithField(field, value)
}
return ret
}
2018-08-10 17:06:00 +02:00
func (l *loggerImpl) WithError(err error) Logger {
val := interface{}(nil)
if err != nil {
val = err.Error()
}
return l.WithField(FieldError, val)
}
2018-08-10 17:06:00 +02:00
func (l *loggerImpl) Debug(msg string) {
l.log(Debug, msg)
}
2018-08-10 17:06:00 +02:00
func (l *loggerImpl) Info(msg string) {
l.log(Info, msg)
}
2018-08-10 17:06:00 +02:00
func (l *loggerImpl) Warn(msg string) {
l.log(Warn, msg)
}
2018-08-10 17:06:00 +02:00
func (l *loggerImpl) Error(msg string) {
l.log(Error, msg)
}
2018-08-10 17:06:00 +02:00
func (l *loggerImpl) Printf(format string, args ...interface{}) {
l.log(Error, fmt.Sprintf(format, args...))
}