aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/Xe/ln/logger.go
blob: 79a9a63bd7fa79eb31b8ef6ea572a314279abd31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package ln

import (
	"context"
	"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{}

// 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.
type Fer interface {
	F() F
}

// Event represents an event
type Event struct {
	Time    time.Time
	Data    F
	Message string
}

// Log is the generic logging method.
func (l *Logger) Log(ctx context.Context, xs ...Fer) {
	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
			}
		}
	}

	for _, f := range xs {
		addF(f.F())
	}

	ctxf, ok := FFromContext(ctx)
	if ok {
		addF(ctxf)
	}

	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
	}

	l.filter(ctx, event)
}

func (l *Logger) filter(ctx context.Context, e Event) {
	for _, f := range l.Filters {
		if !f.Apply(ctx, e) {
			return
		}
	}
}

// Error logs an error and information about the context of said error.
func (l *Logger) Error(ctx context.Context, err error, xs ...Fer) {
	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...)
}

// Fatal logs this set of values, then exits with status code 1.
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...)

	os.Exit(1)
}

// Default Implementation

// Log is the generic logging method.
func Log(ctx context.Context, xs ...Fer) {
	DefaultLogger.Log(ctx, xs...)
}

// Error logs an error and information about the context of said error.
func Error(ctx context.Context, err error, xs ...Fer) {
	DefaultLogger.Error(ctx, err, xs...)
}

// Fatal logs this set of values, then exits with status code 1.
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...)
}