2017-03-29 07:26:50 +00:00
|
|
|
package ln
|
|
|
|
|
|
|
|
import (
|
2017-12-13 18:43:58 +00:00
|
|
|
"context"
|
2017-03-29 07:26:50 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Logger holds the current priority and list of filters
|
|
|
|
type Logger struct {
|
|
|
|
Filters []Filter
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultLogger is the default implementation of Logger
|
|
|
|
var DefaultLogger *Logger
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var defaultFilters []Filter
|
|
|
|
|
|
|
|
// Default to STDOUT for logging, but allow LN_OUT to change it.
|
|
|
|
out := os.Stdout
|
|
|
|
if os.Getenv("LN_OUT") == "<stderr>" {
|
|
|
|
out = os.Stderr
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultFilters = append(defaultFilters, NewWriterFilter(out, nil))
|
|
|
|
|
|
|
|
DefaultLogger = &Logger{
|
|
|
|
Filters: defaultFilters,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// F is a key-value mapping for structured data.
|
|
|
|
type F map[string]interface{}
|
|
|
|
|
2017-12-13 18:43:58 +00:00
|
|
|
// Extend concatentates one F with one or many Fer instances.
|
|
|
|
func (f F) Extend(other ...Fer) {
|
|
|
|
for _, ff := range other {
|
|
|
|
for k, v := range ff.F() {
|
|
|
|
f[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// F makes F an Fer
|
|
|
|
func (f F) F() F {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fer allows any type to add fields to the structured logging key->value pairs.
|
2017-03-29 07:26:50 +00:00
|
|
|
type Fer interface {
|
2017-12-13 18:43:58 +00:00
|
|
|
F() F
|
2017-03-29 07:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Event represents an event
|
|
|
|
type Event struct {
|
|
|
|
Time time.Time
|
|
|
|
Data F
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log is the generic logging method.
|
2017-12-13 18:43:58 +00:00
|
|
|
func (l *Logger) Log(ctx context.Context, xs ...Fer) {
|
2017-03-29 07:26:50 +00:00
|
|
|
event := Event{Time: time.Now()}
|
|
|
|
|
|
|
|
addF := func(bf F) {
|
|
|
|
if event.Data == nil {
|
|
|
|
event.Data = bf
|
|
|
|
} else {
|
|
|
|
for k, v := range bf {
|
|
|
|
event.Data[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 18:43:58 +00:00
|
|
|
for _, f := range xs {
|
|
|
|
addF(f.F())
|
2017-03-29 07:26:50 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 18:43:58 +00:00
|
|
|
ctxf, ok := FFromContext(ctx)
|
|
|
|
if ok {
|
|
|
|
addF(ctxf)
|
|
|
|
}
|
2017-03-29 07:26:50 +00:00
|
|
|
|
|
|
|
if os.Getenv("LN_DEBUG_ALL_EVENTS") == "1" {
|
|
|
|
frame := callersFrame()
|
|
|
|
if event.Data == nil {
|
|
|
|
event.Data = make(F)
|
|
|
|
}
|
|
|
|
event.Data["_lineno"] = frame.lineno
|
|
|
|
event.Data["_function"] = frame.function
|
|
|
|
event.Data["_filename"] = frame.filename
|
|
|
|
}
|
|
|
|
|
2017-12-13 18:43:58 +00:00
|
|
|
l.filter(ctx, event)
|
2017-03-29 07:26:50 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 18:43:58 +00:00
|
|
|
func (l *Logger) filter(ctx context.Context, e Event) {
|
2017-03-29 07:26:50 +00:00
|
|
|
for _, f := range l.Filters {
|
2017-12-13 18:43:58 +00:00
|
|
|
if !f.Apply(ctx, e) {
|
2017-03-29 07:26:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error logs an error and information about the context of said error.
|
2017-12-13 18:43:58 +00:00
|
|
|
func (l *Logger) Error(ctx context.Context, err error, xs ...Fer) {
|
2017-03-29 07:26:50 +00:00
|
|
|
data := F{}
|
|
|
|
frame := callersFrame()
|
|
|
|
|
|
|
|
data["_lineno"] = frame.lineno
|
|
|
|
data["_function"] = frame.function
|
|
|
|
data["_filename"] = frame.filename
|
|
|
|
data["err"] = err
|
|
|
|
|
|
|
|
cause := errors.Cause(err)
|
|
|
|
if cause != nil {
|
|
|
|
data["cause"] = cause.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
xs = append(xs, data)
|
|
|
|
|
2017-12-13 18:43:58 +00:00
|
|
|
l.Log(ctx, xs...)
|
2017-03-29 07:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fatal logs this set of values, then exits with status code 1.
|
2017-12-13 18:43:58 +00:00
|
|
|
func (l *Logger) Fatal(ctx context.Context, xs ...Fer) {
|
|
|
|
xs = append(xs, F{"fatal": true})
|
|
|
|
|
|
|
|
l.Log(ctx, xs...)
|
|
|
|
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FatalErr combines Fatal and Error.
|
|
|
|
func (l *Logger) FatalErr(ctx context.Context, err error, xs ...Fer) {
|
|
|
|
xs = append(xs, F{"fatal": true})
|
|
|
|
|
|
|
|
data := F{}
|
|
|
|
frame := callersFrame()
|
|
|
|
|
|
|
|
data["_lineno"] = frame.lineno
|
|
|
|
data["_function"] = frame.function
|
|
|
|
data["_filename"] = frame.filename
|
|
|
|
data["err"] = err
|
|
|
|
|
|
|
|
cause := errors.Cause(err)
|
|
|
|
if cause != nil {
|
|
|
|
data["cause"] = cause.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
xs = append(xs, data)
|
|
|
|
l.Log(ctx, xs...)
|
2017-03-29 07:26:50 +00:00
|
|
|
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default Implementation
|
|
|
|
|
|
|
|
// Log is the generic logging method.
|
2017-12-13 18:43:58 +00:00
|
|
|
func Log(ctx context.Context, xs ...Fer) {
|
|
|
|
DefaultLogger.Log(ctx, xs...)
|
2017-03-29 07:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error logs an error and information about the context of said error.
|
2017-12-13 18:43:58 +00:00
|
|
|
func Error(ctx context.Context, err error, xs ...Fer) {
|
|
|
|
DefaultLogger.Error(ctx, err, xs...)
|
2017-03-29 07:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fatal logs this set of values, then exits with status code 1.
|
2017-12-13 18:43:58 +00:00
|
|
|
func Fatal(ctx context.Context, xs ...Fer) {
|
|
|
|
DefaultLogger.Fatal(ctx, xs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FatalErr combines Fatal and Error.
|
|
|
|
func FatalErr(ctx context.Context, err error, xs ...Fer) {
|
|
|
|
DefaultLogger.FatalErr(ctx, err, xs...)
|
2017-03-29 07:26:50 +00:00
|
|
|
}
|