141 | 141 |
|
142 | 142 |
// TODO: consider adding back in format strings if they're really needed
|
143 | 143 |
// TODO: consider other bits of zap/zapcore functionality like ObjectMarshaller (for arbitrary objects)
|
144 | |
// TODO: consider other bits of glog functionality like Flush, InfoDepth, OutputStats
|
|
144 |
// TODO: consider other bits of glog functionality like Flush, OutputStats
|
145 | 145 |
|
146 | 146 |
// Logger represents the ability to log messages, both errors and not.
|
147 | 147 |
type Logger interface {
|
|
218 | 218 |
func NewContext(ctx context.Context, l Logger) context.Context {
|
219 | 219 |
return context.WithValue(ctx, contextKey{}, l)
|
220 | 220 |
}
|
|
221 |
|
|
222 |
// CallDepthLogger represents a Logger that knows how to climb the call stack
|
|
223 |
// to identify the original call site and can offset the depth by a specified
|
|
224 |
// number of frames. This is useful for users who have helper functions
|
|
225 |
// between the "real" call site and the actual calls to Logger methods.
|
|
226 |
// Implementations that log information about the call site (such as file,
|
|
227 |
// function, or line) would otherwise log information about the intermediate
|
|
228 |
// helper functions.
|
|
229 |
//
|
|
230 |
// This is an optional interface and implementations are not required to
|
|
231 |
// support it.
|
|
232 |
type CallDepthLogger interface {
|
|
233 |
Logger
|
|
234 |
|
|
235 |
// WithCallDepth returns a Logger that will offset the call stack by the
|
|
236 |
// specified number of frames when logging call site information. If depth
|
|
237 |
// is 0 the attribution should be to the direct caller of this method. If
|
|
238 |
// depth is 1 the attribution should skip 1 call frame, and so on.
|
|
239 |
// Successive calls to this are additive.
|
|
240 |
WithCallDepth(depth int) Logger
|
|
241 |
}
|
|
242 |
|
|
243 |
// WithCallDepth returns a Logger that will offset the call stack by the
|
|
244 |
// specified number of frames when logging call site information, if possible.
|
|
245 |
// This is useful for users who have helper functions between the "real" call
|
|
246 |
// site and the actual calls to Logger methods. If depth is 0 the attribution
|
|
247 |
// should be to the direct caller of this function. If depth is 1 the
|
|
248 |
// attribution should skip 1 call frame, and so on. Successive calls to this
|
|
249 |
// are additive.
|
|
250 |
//
|
|
251 |
// If the underlying log implementation supports the CallDepthLogger interface,
|
|
252 |
// the WithCallDepth method will be called and the result returned. If the
|
|
253 |
// implementation does not support CallDepthLogger, the original Logger will be
|
|
254 |
// returned.
|
|
255 |
//
|
|
256 |
// Callers which care about whether this was supported or not should test for
|
|
257 |
// CallDepthLogger support themselves.
|
|
258 |
func WithCallDepth(logger Logger, depth int) Logger {
|
|
259 |
if decorator, ok := logger.(CallDepthLogger); ok {
|
|
260 |
return decorator.WithCallDepth(depth)
|
|
261 |
}
|
|
262 |
return logger
|
|
263 |
}
|