/
logger.go
77 lines (65 loc) · 1.92 KB
/
logger.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
// package logger is an HTTP request logger that implements correlated logging to one of several supported platforms.
// Each HTTP request is logged as the parent log, with all logs generated during the request as child logs.
//
// The Logging destination is configured with an Exporter. This package provides Exporters for Google Cloud Logging
// and Console Logging.
//
// The GoogleCloudExporter will also correlate logs to Cloud Trace if you instrument your code with tracing.
package logger
import (
"context"
"net/http"
)
// Logger implements logging methods for this package
type Logger struct {
ctx context.Context
lg ctxLogger
}
// Ctx returns the logger from the context. If
// no logger is found, it will write to stderr
func Ctx(ctx context.Context) *Logger {
return &Logger{
ctx: ctx,
lg: fromCtx(ctx),
}
}
// Req returns the logger from the http request. If
// no logger is found, it will write to stderr
func Req(r *http.Request) *Logger {
return &Logger{
ctx: r.Context(),
lg: fromReq(r),
}
}
// Debug logs a debug message.
func (l *Logger) Debug(v interface{}) {
l.lg.Debug(l.ctx, v)
}
// Debugf logs a debug message with format.
func (l *Logger) Debugf(format string, v ...interface{}) {
l.lg.Debugf(l.ctx, format, v...)
}
// Info logs a info message.
func (l *Logger) Info(v interface{}) {
l.lg.Info(l.ctx, v)
}
// Infof logs a info message with format.
func (l *Logger) Infof(format string, v ...interface{}) {
l.lg.Infof(l.ctx, format, v...)
}
// Warn logs a warning message.
func (l *Logger) Warn(v interface{}) {
l.lg.Warn(l.ctx, v)
}
// Warnf logs a warning message with format.
func (l *Logger) Warnf(format string, v ...interface{}) {
l.lg.Warnf(l.ctx, format, v...)
}
// Error logs an error message.
func (l *Logger) Error(v interface{}) {
l.lg.Error(l.ctx, v)
}
// Errorf logs an error message with format.
func (l *Logger) Errorf(format string, v ...interface{}) {
l.lg.Errorf(l.ctx, format, v...)
}