-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolog.go
104 lines (86 loc) · 3.4 KB
/
golog.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
// Package golog implements structured logging for Google Cloud Platform as described in https://cloud.google.com/logging/docs/structured-logging.
// It allows attaching a logger to a context to print context-specific information to the log output.
package golog
import (
"context"
"io"
"os"
)
type key string
const loggerKey key = "logger"
// WithLogger creates a new logger and attaches it to the Context.
func WithLogger(ctx context.Context, w io.Writer, fields Fields) context.Context {
l := NewLogger(w, fields)
return context.WithValue(ctx, loggerKey, l)
}
// GetLogger extracts the logger from a context.
// It returns a new empty logger if ctx doesn't contain a logger.
func GetLogger(ctx context.Context) *Logger {
if ctx == nil {
return NewLogger(os.Stdout, nil)
}
l, ok := ctx.Value(loggerKey).(*Logger)
if !ok {
return NewLogger(os.Stdout, nil)
}
return l
}
// AddFields adds new fields to the logger contained in ctx.
// Existing fields might be overwritten.
func AddFields(ctx context.Context, newFields Fields) {
l := GetLogger(ctx)
l.AddFields(newFields)
}
// GetFields returns the fields of the logger contained in ctx.
func GetFields(ctx context.Context) Fields {
l := GetLogger(ctx)
return l.GetFields()
}
// Debug outputs a debug log message using the logger contained in ctx.
// If ctx doesn't contain a logger, it uses a new logger.
func Debug(ctx context.Context, msg string, req *HTTPRequest, fields Fields) {
l := GetLogger(ctx)
l.output(levelDebug, msg, req, fields)
}
// Info outputs an info log message using the logger contained in ctx.
// If ctx doesn't contain a logger, it uses a new logger.
func Info(ctx context.Context, msg string, req *HTTPRequest, fields Fields) {
l := GetLogger(ctx)
l.output(levelInfo, msg, req, fields)
}
// Notice outputs a notice log message using the logger contained in ctx.
// If ctx doesn't contain a logger, it uses a new logger.
func Notice(ctx context.Context, msg string, req *HTTPRequest, fields Fields) {
l := GetLogger(ctx)
l.output(levelNotice, msg, req, fields)
}
// Warning outputs a warning log message using the logger contained in ctx.
// If ctx doesn't contain a logger, it uses a new logger.
func Warning(ctx context.Context, msg string, req *HTTPRequest, fields Fields) {
l := GetLogger(ctx)
l.output(levelWarning, msg, req, fields)
}
// Error outputs an error log message using the logger contained in ctx.
// If ctx doesn't contain a logger, it uses a new logger.
func Error(ctx context.Context, msg string, req *HTTPRequest, fields Fields) {
l := GetLogger(ctx)
l.output(levelError, msg, req, fields)
}
// Critical outputs a critical log message using the logger contained in ctx.
// If ctx doesn't contain a logger, it uses a new logger.
func Critical(ctx context.Context, msg string, req *HTTPRequest, fields Fields) {
l := GetLogger(ctx)
l.output(levelCritical, msg, req, fields)
}
// Alert outputs an alert log message using the logger contained in ctx.
// If ctx doesn't contain a logger, it uses a new logger.
func Alert(ctx context.Context, msg string, req *HTTPRequest, fields Fields) {
l := GetLogger(ctx)
l.output(levelAlert, msg, req, fields)
}
// Emergency outputs an emergency log message using the logger contained in ctx.
// If ctx doesn't contain a logger, it uses a new logger.
func Emergency(ctx context.Context, msg string, req *HTTPRequest, fields Fields) {
l := GetLogger(ctx)
l.output(levelEmergency, msg, req, fields)
}