forked from ruiaylin/pgparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
215 lines (193 loc) · 7.7 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
// Copyright 2014 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package log
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/ruiaylin/pgparser/utils/tracing"
"github.com/cockroachdb/errors"
opentracing "github.com/opentracing/opentracing-go"
)
func init() {
errors.SetWarningFn(Warningf)
}
// 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)
}
}
// 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, msg string) {
Shoutf(ctx, sev, msg)
}
// Shoutf is like Shout but uses formatting.
func Shoutf(ctx context.Context, sev Severity, format string, args ...interface{}) {
if sev == Severity_FATAL {
// Fatal error handling later already tries to exit even if I/O should
// block, but crash reporting might also be in the way.
t := time.AfterFunc(10*time.Second, func() {
os.Exit(1)
})
defer t.Stop()
}
if !LoggingToStderr(sev) {
// The logging call below would not otherwise appear on stderr;
// however this is what the Shout() contract guarantees, so we do
// it here.
fmt.Fprintf(OrigStderr, "*\n* %s: %s\n*\n", sev.String(),
strings.Replace(
FormatWithContextTags(ctx, format, args...),
"\n", "\n* ", -1))
}
logDepth(ctx, 1, sev, format, args)
}
// 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.
func Info(ctx context.Context, msg string) {
logDepth(ctx, 1, Severity_INFO, msg, nil)
}
// 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.
func Warning(ctx context.Context, msg string) {
logDepth(ctx, 1, Severity_WARNING, msg, nil)
}
// 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.
func Error(ctx context.Context, msg string) {
logDepth(ctx, 1, Severity_ERROR, msg, nil)
}
// 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.
func Fatal(ctx context.Context, msg string) {
logDepth(ctx, 1, Severity_FATAL, msg, nil)
}
// 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 Level) 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 Level) bool {
if sp := opentracing.SpanFromContext(ctx); sp != nil {
if tracing.IsRecording(sp) {
return true
}
}
if VDepth(level, 1 /* depth */) {
return true
}
return false
}