12
const errorKey = "LOG15_ERROR"
24
// Returns the name of a Lvl
25
func (l Lvl) String() string {
42
// Returns the appropriate Lvl from a string name.
43
// Useful for parsing command line args and configuration files.
44
func LvlFromString(lvlString string) (Lvl, error) {
57
return LvlDebug, fmt.Errorf("Unknown level: %v", lvlString)
61
// A Record is what a Logger asks its handler to write
68
KeyNames RecordKeyNames
71
type RecordKeyNames struct {
77
// A Logger writes key/value pairs to a Handler
78
type Logger interface {
79
// New returns a new Logger that has this logger's context plus the given context
80
New(ctx ...interface{}) Logger
82
// SetHandler updates the logger to write records to the specified handler.
85
// Log a message at the given level with context key/value pairs
86
Debug(msg string, ctx ...interface{})
87
Info(msg string, ctx ...interface{})
88
Warn(msg string, ctx ...interface{})
89
Error(msg string, ctx ...interface{})
90
Crit(msg string, ctx ...interface{})
98
func (l *logger) write(msg string, lvl Lvl, ctx []interface{}) {
103
Ctx: newContext(l.ctx, ctx),
104
KeyNames: RecordKeyNames{
110
runtime.Callers(3, r.CallPC[:])
114
func (l *logger) New(ctx ...interface{}) Logger {
115
child := &logger{newContext(l.ctx, ctx), new(swapHandler)}
116
child.SetHandler(l.h)
120
func newContext(prefix []interface{}, suffix []interface{}) []interface{} {
121
normalizedSuffix := normalize(suffix)
122
newCtx := make([]interface{}, len(prefix)+len(normalizedSuffix))
123
n := copy(newCtx, prefix)
124
copy(newCtx[n:], normalizedSuffix)
128
func (l *logger) Debug(msg string, ctx ...interface{}) {
129
l.write(msg, LvlDebug, ctx)
132
func (l *logger) Info(msg string, ctx ...interface{}) {
133
l.write(msg, LvlInfo, ctx)
136
func (l *logger) Warn(msg string, ctx ...interface{}) {
137
l.write(msg, LvlWarn, ctx)
140
func (l *logger) Error(msg string, ctx ...interface{}) {
141
l.write(msg, LvlError, ctx)
144
func (l *logger) Crit(msg string, ctx ...interface{}) {
145
l.write(msg, LvlCrit, ctx)
148
func (l *logger) SetHandler(h Handler) {
152
func normalize(ctx []interface{}) []interface{} {
153
// if the caller passed a Ctx object, then expand it
155
if ctxMap, ok := ctx[0].(Ctx); ok {
156
ctx = ctxMap.toArray()
160
// ctx needs to be even because it's a series of key/value pairs
161
// no one wants to check for errors on logging functions,
162
// so instead of erroring on bad input, we'll just make sure
163
// that things are the right length and users can fix bugs
164
// when they see the output looks wrong
166
ctx = append(ctx, nil, errorKey, "Normalized odd number of arguments by adding nil")
172
// Lazy allows you to defer calculation of a logged value that is expensive
173
// to compute until it is certain that it must be evaluated with the given filters.
175
// Lazy may also be used in conjunction with a Logger's New() function
176
// to generate a child logger which always reports the current value of changing
179
// You may wrap any function which takes no arguments to Lazy. It may return any
180
// number of values of any type.
185
// Ctx is a map of key/value pairs to pass as context to a log function
186
// Use this only if you really need greater safety around the arguments you pass
187
// to the logging functions.
188
type Ctx map[string]interface{}
190
func (c Ctx) toArray() []interface{} {
191
arr := make([]interface{}, len(c)*2)
194
for k, v := range c {