-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathlog.go
254 lines (228 loc) · 9.18 KB
/
log.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright 2014 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package log
import (
"context"
"fmt"
"io"
"strings"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
opentracing "github.com/opentracing/opentracing-go"
"github.com/petermattis/goid"
)
func init() {
copyStandardLogTo("INFO")
}
// FatalOnPanic recovers from a panic and exits the process with a
// Fatal log. This is useful for avoiding a panic being caught through
// a CGo exported function or preventing HTTP handlers from recovering
// panics and ignoring them.
func FatalOnPanic() {
if r := recover(); r != nil {
Fatalf(context.Background(), "unexpected panic: %s", r)
}
}
// SetExitFunc allows setting a function that will be called to exit the
// process when a Fatal message is generated. The supplied bool, if true,
// supresses the stack trace, which is useful for test callers wishing
// to keep the logs reasonably clean.
//
// Call with a nil function to undo.
func SetExitFunc(hideStack bool, f func(int)) {
logging.mu.Lock()
defer logging.mu.Unlock()
if f == nil && hideStack {
panic("can't pass hideStack when resetting logger")
}
logging.exitOverride.f = f
logging.exitOverride.hideStack = hideStack
}
// logDepth uses the PrintWith to format the output string and
// formulate the context information into the machine-readable
// dictionary for separate binary-log output.
func logDepth(ctx context.Context, depth int, sev Severity, format string, args []interface{}) {
// TODO(tschottdorf): logging hooks should have their entry point here.
addStructured(ctx, sev, depth+1, format, args)
}
// Shout logs to the specified severity's log, and also to the real
// stderr if logging is currently redirected to a file.
func Shout(ctx context.Context, sev Severity, args ...interface{}) {
logDepth(ctx, 1, sev, "", args)
if stderrRedirected {
fmt.Fprintf(OrigStderr, "*\n* %s: %s\n*\n", sev.String(),
strings.Replace(MakeMessage(ctx, "", args), "\n", "\n* ", -1))
}
}
// Infof logs to the INFO log.
// It extracts log tags from the context and logs them along with the given
// message. Arguments are handled in the manner of fmt.Printf; a newline is
// appended.
func Infof(ctx context.Context, format string, args ...interface{}) {
logDepth(ctx, 1, Severity_INFO, format, args)
}
// Info logs to the INFO log.
// It extracts log tags from the context and logs them along with the given
// message. Arguments are handled in the manner of fmt.Print; a newline is
// appended.
func Info(ctx context.Context, args ...interface{}) {
logDepth(ctx, 1, Severity_INFO, "", args)
}
// InfoDepth logs to the INFO log, offsetting the caller's stack frame by
// 'depth'.
// It extracts log tags from the context and logs them along with the given
// message. Arguments are handled in the manner of fmt.Print; a newline is
// appended.
func InfoDepth(ctx context.Context, depth int, args ...interface{}) {
logDepth(ctx, depth+1, Severity_INFO, "", args)
}
// InfofDepth logs to the INFO log, offsetting the caller's stack frame by
// 'depth'.
// It extracts log tags from the context and logs them along with the given
// message. Arguments are handled in the manner of fmt.Printf; a newline is
// appended.
func InfofDepth(ctx context.Context, depth int, format string, args ...interface{}) {
logDepth(ctx, depth+1, Severity_INFO, format, args)
}
// Warningf logs to the WARNING and INFO logs.
// It extracts log tags from the context and logs them along with the given
// message. Arguments are handled in the manner of fmt.Printf; a newline is
// appended.
func Warningf(ctx context.Context, format string, args ...interface{}) {
logDepth(ctx, 1, Severity_WARNING, format, args)
}
// Warning logs to the WARNING and INFO logs.
// It extracts log tags from the context and logs them along with the given
// message. Arguments are handled in the manner of fmt.Print; a newline is
// appended.
func Warning(ctx context.Context, args ...interface{}) {
logDepth(ctx, 1, Severity_WARNING, "", args)
}
// WarningfDepth logs to the WARNING and INFO logs, offsetting the caller's
// stack frame by 'depth'.
// It extracts log tags from the context and logs them along with the given
// message. Arguments are handled in the manner of fmt.Printf; a newline is
// appended.
func WarningfDepth(ctx context.Context, depth int, format string, args ...interface{}) {
logDepth(ctx, depth+1, Severity_WARNING, format, args)
}
// Errorf logs to the ERROR, WARNING, and INFO logs.
// It extracts log tags from the context and logs them along with the given
// message. Arguments are handled in the manner of fmt.Printf; a newline is
// appended.
func Errorf(ctx context.Context, format string, args ...interface{}) {
logDepth(ctx, 1, Severity_ERROR, format, args)
}
// Error logs to the ERROR, WARNING, and INFO logs.
// It extracts log tags from the context and logs them along with the given
// message. Arguments are handled in the manner of fmt.Print; a newline is
// appended.
func Error(ctx context.Context, args ...interface{}) {
logDepth(ctx, 1, Severity_ERROR, "", args)
}
// ErrorfDepth logs to the ERROR, WARNING, and INFO logs, offsetting the
// caller's stack frame by 'depth'.
// It extracts log tags from the context and logs them along with the given
// message. Arguments are handled in the manner of fmt.Printf; a newline is
// appended.
func ErrorfDepth(ctx context.Context, depth int, format string, args ...interface{}) {
logDepth(ctx, depth+1, Severity_ERROR, format, args)
}
// Fatalf logs to the INFO, WARNING, ERROR, and FATAL logs, including a stack
// trace of all running goroutines, then calls os.Exit(255).
// It extracts log tags from the context and logs them along with the given
// message. Arguments are handled in the manner of fmt.Printf; a newline is
// appended.
func Fatalf(ctx context.Context, format string, args ...interface{}) {
logDepth(ctx, 1, Severity_FATAL, format, args)
}
// Fatal logs to the INFO, WARNING, ERROR, and FATAL logs, including a stack
// trace of all running goroutines, then calls os.Exit(255).
// It extracts log tags from the context and logs them along with the given
// message. Arguments are handled in the manner of fmt.Print; a newline is
// appended.
func Fatal(ctx context.Context, args ...interface{}) {
logDepth(ctx, 1, Severity_FATAL, "", args)
}
// FatalfDepth logs to the INFO, WARNING, ERROR, and FATAL logs (offsetting the
// caller's stack frame by 'depth'), including a stack trace of all running
// goroutines, then calls os.Exit(255).
// It extracts log tags from the context and logs them along with the given
// message. Arguments are handled in the manner of fmt.Printf; a newline is
// appended.
func FatalfDepth(ctx context.Context, depth int, format string, args ...interface{}) {
logDepth(ctx, depth+1, Severity_FATAL, format, args)
}
// V returns true if the logging verbosity is set to the specified level or
// higher.
//
// See also ExpensiveLogEnabled().
//
// TODO(andrei): Audit uses of V() and see which ones should actually use the
// newer ExpensiveLogEnabled().
func V(level int32) bool {
return VDepth(level, 1)
}
// ExpensiveLogEnabled is used to test whether effort should be used to produce
// log messages whose construction has a measurable cost. It returns true if
// either the current context is recording the trace, or if the caller's
// verbosity is above level.
//
// NOTE: This doesn't take into consideration whether tracing is generally
// enabled or whether a trace.EventLog or a trace.Trace (i.e. sp.netTr) is
// attached to ctx. In particular, if some OpenTracing collection is enabled
// (e.g. LightStep), that, by itself, does NOT cause the expensive messages to
// be enabled. "SET tracing" and friends, on the other hand, does cause
// these messages to be enabled, as it shows that a user has expressed
// particular interest in a trace.
//
// Usage:
//
// if ExpensiveLogEnabled(ctx, 2) {
// msg := constructExpensiveMessage()
// log.VEventf(ctx, 2, msg)
// }
//
func ExpensiveLogEnabled(ctx context.Context, level int32) bool {
if sp := opentracing.SpanFromContext(ctx); sp != nil {
if tracing.IsRecording(sp) {
return true
}
}
if VDepth(level, 1 /* depth */) {
return true
}
return false
}
// MakeEntry creates an Entry.
func MakeEntry(s Severity, t int64, file string, line int, msg string) Entry {
return Entry{
Severity: s,
Time: t,
Goroutine: goid.Get(),
File: file,
Line: int64(line),
Message: msg,
}
}
// Format writes the log entry to the specified writer.
func (e Entry) Format(w io.Writer) error {
buf := formatLogEntry(e, nil, nil)
defer logging.putBuffer(buf)
_, err := w.Write(buf.Bytes())
return err
}
// SetVModule alters the vmodule logging level to the passed in value.
func SetVModule(value string) error {
return logging.vmodule.Set(value)
}