2017-09-23 18:20:22 +02:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"runtime/debug"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// The field set by WithError function
|
|
|
|
FieldError = "err"
|
|
|
|
)
|
|
|
|
|
|
|
|
const DefaultUserFieldCapacity = 5
|
|
|
|
|
|
|
|
type Logger struct {
|
|
|
|
fields Fields
|
2017-12-23 00:55:02 +01:00
|
|
|
outlets *Outlets
|
2017-09-23 18:20:22 +02:00
|
|
|
outletTimeout time.Duration
|
|
|
|
|
|
|
|
mtx *sync.Mutex
|
|
|
|
}
|
|
|
|
|
2017-12-23 00:55:02 +01:00
|
|
|
func NewLogger(outlets *Outlets, outletTimeout time.Duration) *Logger {
|
2017-09-23 18:20:22 +02:00
|
|
|
return &Logger{
|
|
|
|
make(Fields, DefaultUserFieldCapacity),
|
|
|
|
outlets,
|
|
|
|
outletTimeout,
|
|
|
|
&sync.Mutex{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-16 23:49:47 +01:00
|
|
|
type outletResult struct {
|
|
|
|
Outlet Outlet
|
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) 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(context.Background(), entry)
|
|
|
|
}
|
|
|
|
|
2017-09-23 18:20:22 +02:00
|
|
|
func (l *Logger) log(level Level, msg string) {
|
|
|
|
|
|
|
|
l.mtx.Lock()
|
|
|
|
defer l.mtx.Unlock()
|
|
|
|
|
|
|
|
entry := Entry{level, msg, time.Now(), l.fields}
|
|
|
|
|
2017-09-30 16:39:52 +02:00
|
|
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(l.outletTimeout))
|
2017-11-16 23:49:47 +01:00
|
|
|
ech := make(chan outletResult)
|
2017-09-23 18:20:22 +02:00
|
|
|
|
2017-11-16 23:49:47 +01:00
|
|
|
louts := l.outlets.Get(level)
|
2017-09-23 18:20:22 +02:00
|
|
|
for i := range louts {
|
|
|
|
go func(ctx context.Context, outlet Outlet, entry Entry) {
|
2017-11-16 23:49:47 +01:00
|
|
|
ech <- outletResult{outlet, outlet.WriteEntry(ctx, entry)}
|
2017-09-23 18:20:22 +02:00
|
|
|
}(ctx, louts[i], entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
for fin := 0; fin < len(louts); fin++ {
|
|
|
|
select {
|
2017-11-16 23:49:47 +01:00
|
|
|
case res := <-ech:
|
|
|
|
if res.Error != nil {
|
|
|
|
l.logInternalError(res.Outlet, res.Error.Error())
|
2017-09-23 18:20:22 +02:00
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
if ctx.Err() == context.DeadlineExceeded {
|
2017-11-16 23:49:47 +01:00
|
|
|
l.logInternalError(nil, "one or more outlets exceeded timeout but will keep waiting anyways")
|
2017-09-23 18:20:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-30 16:39:52 +02:00
|
|
|
cancel() // make go vet happy
|
|
|
|
|
2017-09-23 18:20:22 +02:00
|
|
|
}
|
|
|
|
|
2017-12-23 00:55:02 +01:00
|
|
|
func (l *Logger) WithOutlet(outlet Outlet, level Level) *Logger {
|
|
|
|
l.mtx.Lock()
|
|
|
|
defer l.mtx.Unlock()
|
|
|
|
newOutlets := l.outlets.DeepCopy()
|
|
|
|
newOutlets.Add(outlet, level)
|
|
|
|
child := &Logger{
|
|
|
|
fields: l.fields,
|
|
|
|
outlets: newOutlets,
|
|
|
|
outletTimeout: l.outletTimeout,
|
|
|
|
mtx: l.mtx,
|
|
|
|
}
|
|
|
|
return child
|
|
|
|
}
|
|
|
|
|
2017-09-23 18:20:22 +02:00
|
|
|
func (l *Logger) WithField(field string, val interface{}) *Logger {
|
|
|
|
|
|
|
|
l.mtx.Lock()
|
|
|
|
defer l.mtx.Unlock()
|
|
|
|
|
|
|
|
if _, ok := l.fields[field]; ok {
|
2017-11-16 23:49:47 +01:00
|
|
|
l.logInternalError(nil,
|
|
|
|
fmt.Sprintf("caller overwrites field '%s'. Stack: %s", field, string(debug.Stack())))
|
2017-09-23 18:20:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
child := &Logger{
|
|
|
|
fields: make(Fields, len(l.fields)+1),
|
2017-12-23 00:55:02 +01:00
|
|
|
outlets: l.outlets,
|
2017-09-23 18:20:22 +02:00
|
|
|
outletTimeout: l.outletTimeout,
|
|
|
|
mtx: l.mtx,
|
|
|
|
}
|
|
|
|
for k, v := range l.fields {
|
|
|
|
child.fields[k] = v
|
|
|
|
}
|
|
|
|
child.fields[field] = val
|
|
|
|
|
|
|
|
return child
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) WithFields(fields Fields) (ret *Logger) {
|
|
|
|
// TODO optimize
|
|
|
|
ret = l
|
|
|
|
for field, value := range fields {
|
|
|
|
ret = l.WithField(field, value)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) WithError(err error) *Logger {
|
2017-10-05 21:23:39 +02:00
|
|
|
val := interface{}(nil)
|
|
|
|
if err != nil {
|
|
|
|
val = err.Error()
|
|
|
|
}
|
|
|
|
return l.WithField(FieldError, val)
|
2017-09-23 18:20:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Debug(msg string) {
|
|
|
|
l.log(Debug, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Info(msg string) {
|
|
|
|
l.log(Info, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Warn(msg string) {
|
|
|
|
l.log(Warn, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Error(msg string) {
|
|
|
|
l.log(Error, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Printf(format string, args ...interface{}) {
|
|
|
|
l.log(Error, fmt.Sprintf(format, args...))
|
|
|
|
}
|