Repository navigation
Expand file tree
/
Copy pathdefault.go
More file actions
126 lines (95 loc) · 4.83 KB
/
Copy pathdefault.go
File metadata and controls
126 lines (95 loc) · 4.83 KB
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
package logging
import (
"context"
"sync/atomic"
)
// Logger implemented by Default.
var _ Logger = (*Default)(nil)
// defaultInner is a pair of loggers made for a backend.
type defaultInner struct {
backend *Backend
plain Logger // to derive new loggers
direct *zapLogger // to write records, skips Default method frame in caller detection
}
// Default logger provides logging methods with default setting in case customisation is not required.
// Zero value is ready to use, so it fits package level variable: var log logging.Default.
// It follows Init: after the backend is replaced it logs through the new one.
// It still allows to make descendant fully-functional loggers when required.
type Default struct {
inner atomic.Value // *defaultInner
}
// get returns loggers for the active backend.
func (l *Default) get() *defaultInner {
backend := activeBackend()
if cur, ok := l.inner.Load().(*defaultInner); ok && cur.backend == backend {
return cur
}
plain := backend.newLogger()
cur := &defaultInner{backend: backend, plain: plain, direct: plain.addCallerSkip(1)}
l.inner.Store(cur)
return cur
}
// Level returns current logger level, which is the configured global level.
func (l *Default) Level() Level {
return l.get().plain.Level()
}
// IsEnabledForLevel detects if internal logging level suitable to produce messages with specified logging.Level.
func (l *Default) IsEnabledForLevel(level Level) bool {
return l.get().plain.IsEnabledForLevel(level)
}
// WithKeys provides a new logger instance having specified key-value set.
func (l *Default) WithKeys(fields Keys) Logger {
return l.get().plain.WithKeys(fields)
}
// WithKey provides a new logger instance having specified key-value pair set.
func (l *Default) WithKey(key string, value any) Logger {
return l.get().plain.WithKey(key, value)
}
// WithError provides a new logger instance having specified error key.
func (l *Default) WithError(err error) Logger {
return l.get().plain.WithError(err)
}
// WithLevel provides a new logger instance inherit settings from parent except specified logging level.
func (l *Default) WithLevel(level Level) Logger {
return l.get().plain.WithLevel(level)
}
// WithName provides a new named logger instance. Its level is chosen like NewNamedLogger does.
func (l *Default) WithName(name string) Logger {
return l.get().backend.NewNamedLogger(name)
}
// WithContext provides a new logger instance with context data attached.
func (l *Default) WithContext(ctx context.Context) Logger {
return l.get().plain.WithContext(ctx)
}
// Sync flushes any buffered log entries.
func (l *Default) Sync() error {
return l.get().direct.Sync()
}
// Panic writes a record of level panic and panics with the message regardless of the logger level.
func (l *Default) Panic(args ...interface{}) { l.get().direct.Panic(args...) }
// Panicf writes a formatted record of level panic and panics with the message regardless of the logger level.
func (l *Default) Panicf(fmt string, args ...interface{}) { l.get().direct.Panicf(fmt, args...) }
// Trace sends trace level data onto logging.
func (l *Default) Trace(args ...interface{}) { l.get().direct.Trace(args...) }
// Tracef sends trace level message template formatted with specified arguments.
func (l *Default) Tracef(fmt string, args ...interface{}) { l.get().direct.Tracef(fmt, args...) }
// Debug sends debug level data onto logging.
func (l *Default) Debug(args ...interface{}) { l.get().direct.Debug(args...) }
// Debugf sends debug level message template formatted with specified arguments.
func (l *Default) Debugf(fmt string, args ...interface{}) { l.get().direct.Debugf(fmt, args...) }
// Info sends info level data onto logging.
func (l *Default) Info(args ...interface{}) { l.get().direct.Info(args...) }
// Infof sends info level message template formatted with specified arguments.
func (l *Default) Infof(fmt string, args ...interface{}) { l.get().direct.Infof(fmt, args...) }
// Warn sends warning level data onto logging.
func (l *Default) Warn(args ...interface{}) { l.get().direct.Warn(args...) }
// Warnf sends warning level message template formatted with specified arguments.
func (l *Default) Warnf(fmt string, args ...interface{}) { l.get().direct.Warnf(fmt, args...) }
// Error sends error level data onto logging.
func (l *Default) Error(args ...interface{}) { l.get().direct.Error(args...) }
// Errorf sends error level message template formatted with specified arguments.
func (l *Default) Errorf(fmt string, args ...interface{}) { l.get().direct.Errorf(fmt, args...) }
// Fatal sends fatal level data onto logging and terminates the process.
func (l *Default) Fatal(args ...interface{}) { l.get().direct.Fatal(args...) }
// Fatalf sends fatal level message template formatted with specified arguments and terminates the process.
func (l *Default) Fatalf(fmt string, args ...interface{}) { l.get().direct.Fatalf(fmt, args...) }