-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathroot.go
187 lines (160 loc) · 4.9 KB
/
root.go
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
181
182
183
184
185
186
187
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package log
import (
"context"
"log/slog"
"os"
"sync/atomic"
)
var root atomic.Value
func init() {
root.Store(&logger{slog.New(DiscardHandler())})
}
// SetDefault sets the default global logger
func SetDefault(l Logger) {
root.Store(l)
if lg, ok := l.(*logger); ok {
slog.SetDefault(lg.inner)
}
}
// Root returns the root logger
func Root() Logger {
return root.Load().(Logger)
}
// The following functions bypass the exported logger methods (logger.Debug,
// etc.) to keep the call depth the same for all paths to logger.Write so
// runtime.Caller(2) always refers to the call site in client code.
// Trace is a convenient alias for Root().Trace
//
// Log a message at the trace level with context key/value pairs
//
// # Usage
//
// log.Trace("msg")
// log.Trace("msg", "key1", val1)
// log.Trace("msg", "key1", val1, "key2", val2)
func Trace(msg string, ctx ...interface{}) {
Root().Write(LevelTrace, msg, ctx...)
}
// Debug is a convenient alias for Root().Debug
//
// Log a message at the debug level with context key/value pairs
//
// # Usage Examples
//
// log.Debug("msg")
// log.Debug("msg", "key1", val1)
// log.Debug("msg", "key1", val1, "key2", val2)
func Debug(msg string, ctx ...interface{}) {
Root().Write(slog.LevelDebug, msg, ctx...)
}
// Info is a convenient alias for Root().Info
//
// Log a message at the info level with context key/value pairs
//
// # Usage Examples
//
// log.Info("msg")
// log.Info("msg", "key1", val1)
// log.Info("msg", "key1", val1, "key2", val2)
func Info(msg string, ctx ...interface{}) {
Root().Write(slog.LevelInfo, msg, ctx...)
}
// Warn is a convenient alias for Root().Warn
//
// Log a message at the warn level with context key/value pairs
//
// # Usage Examples
//
// log.Warn("msg")
// log.Warn("msg", "key1", val1)
// log.Warn("msg", "key1", val1, "key2", val2)
func Warn(msg string, ctx ...interface{}) {
Root().Write(slog.LevelWarn, msg, ctx...)
}
// Error is a convenient alias for Root().Error
//
// Log a message at the error level with context key/value pairs
//
// # Usage Examples
//
// log.Error("msg")
// log.Error("msg", "key1", val1)
// log.Error("msg", "key1", val1, "key2", val2)
func Error(msg string, ctx ...interface{}) {
Root().Write(slog.LevelError, msg, ctx...)
}
// Crit is a convenient alias for Root().Crit
//
// Log a message at the crit level with context key/value pairs, and then exit.
//
// # Usage Examples
//
// log.Crit("msg")
// log.Crit("msg", "key1", val1)
// log.Crit("msg", "key1", val1, "key2", val2)
func Crit(msg string, ctx ...interface{}) {
Root().Write(LevelCrit, msg, ctx...)
os.Exit(1)
}
// WithContext returns a logger that uses Root with the provided context
func WithContext(ctx ...interface{}) Logger {
return &RootWithContext{
ctx: ctx,
}
}
// RootWithContext is a logger than can be initialized at a global scope
type RootWithContext struct {
ctx []interface{}
}
func (r *RootWithContext) Handler() slog.Handler {
return Root().Handler()
}
func (r *RootWithContext) With(ctx ...interface{}) Logger {
return Root().With(append(r.ctx, ctx...)...)
}
func (r *RootWithContext) New(ctx ...interface{}) Logger {
return Root().With(append(r.ctx, ctx...)...)
}
func (r *RootWithContext) Log(level slog.Level, msg string, ctx ...interface{}) {
Root().Write(level, msg, append(r.ctx, ctx...)...)
}
func (r *RootWithContext) Trace(msg string, ctx ...interface{}) {
r.Log(LevelTrace, msg, ctx...)
}
func (r *RootWithContext) Debug(msg string, ctx ...interface{}) {
r.Log(slog.LevelDebug, msg, ctx...)
}
func (r *RootWithContext) Info(msg string, ctx ...interface{}) {
r.Log(slog.LevelInfo, msg, ctx...)
}
func (r *RootWithContext) Warn(msg string, ctx ...interface{}) {
r.Log(slog.LevelWarn, msg, ctx...)
}
func (r *RootWithContext) Error(msg string, ctx ...interface{}) {
r.Log(slog.LevelError, msg, ctx...)
}
func (r *RootWithContext) Crit(msg string, ctx ...interface{}) {
r.Log(LevelCrit, msg, ctx...)
os.Exit(1)
}
func (r *RootWithContext) Write(level slog.Level, msg string, ctx ...interface{}) {
Root().Write(level, msg, append(r.ctx, ctx...)...)
}
func (r *RootWithContext) Enabled(ctx context.Context, level slog.Level) bool {
return Root().Enabled(ctx, level)
}