aboutsummaryrefslogtreecommitdiff
path: root/weed/glog/glog_ctx.go
blob: daae3b1485d39e6987da54e12cb3dd25e2a596c8 (plain)
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
package glog

import (
	"context"
	"fmt"
	"sync/atomic"

	reqid "github.com/seaweedfs/seaweedfs/weed/util/request_id"
)

const requestIDField = "request_id"

// formatMetaTag returns a formatted request ID tag from the context,
// like "request_id:abc123". Returns an empty string if no request ID is found.
func formatMetaTag(ctx context.Context) string {
	if requestID := reqid.Get(ctx); requestID != "" {
		return fmt.Sprintf("%s:%s", requestIDField, requestID)
	}
	return ""
}

// InfoCtx is a context-aware alternative to Verbose.Info.
// Logs to the INFO log, guarded by the value of v, and prepends a request ID from the context if present.
// Arguments are handled in the manner of fmt.Print.
func (v Verbose) InfoCtx(ctx context.Context, args ...interface{}) {
	if !v {
		return
	}
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		args = append([]interface{}{metaTag}, args...)
	}
	logging.print(infoLog, args...)
}

// InfolnCtx is a context-aware alternative to Verbose.Infoln.
// Logs to the INFO log, prepending a request ID from the context if it exists.
// Arguments are handled in the manner of fmt.Println.
func (v Verbose) InfolnCtx(ctx context.Context, args ...interface{}) {
	if !v {
		return
	}
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		args = append([]interface{}{metaTag}, args...)
	}
	logging.println(infoLog, args...)
}

// InfofCtx is a context-aware alternative to Verbose.Infof.
// Logs to the INFO log, guarded by the value of v, and prepends a request ID from the context if present.
// Arguments are handled in the manner of fmt.Printf.
func (v Verbose) InfofCtx(ctx context.Context, format string, args ...interface{}) {
	if !v {
		return
	}
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		format = metaTag + " " + format
	}
	logging.printf(infoLog, format, args...)
}

// InfofCtx logs a formatted message at info level, prepending a request ID from
// the context if it exists. This is a context-aware alternative to Infof.
func InfofCtx(ctx context.Context, format string, args ...interface{}) {
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		format = metaTag + " " + format
	}
	logging.printf(infoLog, format, args...)
}

// InfoCtx logs a message at info level, prepending a request ID from the context
// if it exists. This is a context-aware alternative to Info.
func InfoCtx(ctx context.Context, args ...interface{}) {
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		args = append([]interface{}{metaTag}, args...)
	}
	logging.print(infoLog, args...)
}

// WarningCtx logs to the WARNING and INFO logs.
// Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Print.
// This is a context-aware alternative to Warning.
func WarningCtx(ctx context.Context, args ...interface{}) {
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		args = append([]interface{}{metaTag}, args...)
	}
	logging.print(warningLog, args...)
}

// WarningDepthCtx logs to the WARNING and INFO logs with a custom call depth.
// Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Print.
// This is a context-aware alternative to WarningDepth.
func WarningDepthCtx(ctx context.Context, depth int, args ...interface{}) {
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		args = append([]interface{}{metaTag}, args...)
	}
	logging.printDepth(warningLog, depth, args...)
}

// WarninglnCtx logs to the WARNING and INFO logs.
// Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Println.
// This is a context-aware alternative to Warningln.
func WarninglnCtx(ctx context.Context, args ...interface{}) {
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		args = append([]interface{}{metaTag}, args...)
	}
	logging.println(warningLog, args...)
}

// WarningfCtx logs to the WARNING and INFO logs.
// Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Printf.
// This is a context-aware alternative to Warningf.
func WarningfCtx(ctx context.Context, format string, args ...interface{}) {
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		format = metaTag + " " + format
	}
	logging.printf(warningLog, format, args...)
}

// ErrorCtx logs to the ERROR, WARNING, and INFO logs.
// Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Print.
// This is a context-aware alternative to Error.
func ErrorCtx(ctx context.Context, args ...interface{}) {
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		args = append([]interface{}{metaTag}, args...)
	}
	logging.print(errorLog, args...)
}

// ErrorDepthCtx logs to the ERROR, WARNING, and INFO logs with a custom call depth.
// Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Print.
// This is a context-aware alternative to ErrorDepth.
func ErrorDepthCtx(ctx context.Context, depth int, args ...interface{}) {
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		args = append([]interface{}{metaTag}, args...)
	}
	logging.printDepth(errorLog, depth, args...)
}

// ErrorlnCtx logs to the ERROR, WARNING, and INFO logs.
// Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Println.
// This is a context-aware alternative to Errorln.
func ErrorlnCtx(ctx context.Context, args ...interface{}) {
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		args = append([]interface{}{metaTag}, args...)
	}
	logging.println(errorLog, args...)
}

// ErrorfCtx logs to the ERROR, WARNING, and INFO logs.
// Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Printf.
// This is a context-aware alternative to Errorf.
func ErrorfCtx(ctx context.Context, format string, args ...interface{}) {
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		format = metaTag + " " + format
	}
	logging.printf(errorLog, format, args...)
}

// FatalCtx logs to the FATAL, ERROR, WARNING, and INFO logs,
// including a stack trace of all running goroutines, then calls os.Exit(255).
// Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Print.
// This is a context-aware alternative to Fatal.
func FatalCtx(ctx context.Context, args ...interface{}) {
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		args = append([]interface{}{metaTag}, args...)
	}
	logging.print(fatalLog, args...)
}

// FatalDepthCtx logs to the FATAL, ERROR, WARNING, and INFO logs with a custom call depth,
// including a stack trace of all running goroutines, then calls os.Exit(255).
// Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Print.
// This is a context-aware alternative to FatalDepth.
func FatalDepthCtx(ctx context.Context, depth int, args ...interface{}) {
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		args = append([]interface{}{metaTag}, args...)
	}
	logging.printDepth(fatalLog, depth, args...)
}

// FatallnCtx logs to the FATAL, ERROR, WARNING, and INFO logs,
// including a stack trace of all running goroutines, then calls os.Exit(255).
// Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Println.
// This is a context-aware alternative to Fatalln.
func FatallnCtx(ctx context.Context, args ...interface{}) {
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		args = append([]interface{}{metaTag}, args...)
	}
	logging.println(fatalLog, args...)
}

// FatalfCtx logs to the FATAL, ERROR, WARNING, and INFO logs,
// including a stack trace of all running goroutines, then calls os.Exit(255).
// Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Printf.
// This is a context-aware alternative to Fatalf.
func FatalfCtx(ctx context.Context, format string, args ...interface{}) {
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		format = metaTag + " " + format
	}
	logging.printf(fatalLog, format, args...)
}

// ExitCtx logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1).
// Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Print.
// This is a context-aware alternative to ExitCtx
func ExitCtx(ctx context.Context, args ...interface{}) {
	atomic.StoreUint32(&fatalNoStacks, 1)
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		args = append([]interface{}{metaTag}, args...)
	}
	logging.print(fatalLog, args...)
}

// ExitDepthCtx logs to the FATAL, ERROR, WARNING, and INFO logs with a custom call depth,
// then calls os.Exit(1). Prepends a request ID from the context if it exists.
// Arguments are handled in the manner of fmt.Print.
// This is a context-aware alternative to ExitDepth.
func ExitDepthCtx(ctx context.Context, depth int, args ...interface{}) {
	atomic.StoreUint32(&fatalNoStacks, 1)
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		args = append([]interface{}{metaTag}, args...)
	}
	logging.printDepth(fatalLog, depth, args...)
}

// ExitlnCtx logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1).
// Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Println.
// This is a context-aware alternative to Exitln.
func ExitlnCtx(ctx context.Context, args ...interface{}) {
	atomic.StoreUint32(&fatalNoStacks, 1)
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		args = append([]interface{}{metaTag}, args...)
	}
	logging.println(fatalLog, args...)
}

// ExitfCtx logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1).
// Prepends a request ID from the context if it exists. Arguments are handled in the manner of fmt.Printf.
// This is a context-aware alternative to Exitf.
func ExitfCtx(ctx context.Context, format string, args ...interface{}) {
	atomic.StoreUint32(&fatalNoStacks, 1)
	if metaTag := formatMetaTag(ctx); metaTag != "" {
		format = metaTag + " " + format
	}
	logging.printf(fatalLog, format, args...)
}