-
Notifications
You must be signed in to change notification settings - Fork 1
/
logging.go
128 lines (100 loc) · 2.94 KB
/
logging.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
package logging
import (
"context"
aulogging "github.com/StephanHCB/go-autumn-logging"
auzerolog "github.com/StephanHCB/go-autumn-logging-zerolog"
auloggingapi "github.com/StephanHCB/go-autumn-logging/api"
"github.com/rs/zerolog"
)
type Logger interface {
Debug(format string, v ...interface{})
Info(format string, v ...interface{})
Warn(format string, v ...interface{})
Error(format string, v ...interface{})
// expected to terminate the process
Fatal(format string, v ...interface{})
}
type loggingWrapper struct {
logger auloggingapi.ContextAwareLoggingImplementation
}
func (l *loggingWrapper) Debug(format string, v ...interface{}) {
l.logger.Debug().Printf(format, v...)
}
func (l *loggingWrapper) Info(format string, v ...interface{}) {
l.logger.Info().Printf(format, v...)
}
func (l *loggingWrapper) Warn(format string, v ...interface{}) {
l.logger.Warn().Printf(format, v...)
}
func (l *loggingWrapper) Error(format string, v ...interface{}) {
l.logger.Error().Printf(format, v...)
}
// expected to terminate the process
func (l *loggingWrapper) Fatal(format string, v ...interface{}) {
l.logger.Fatal().Printf(format, v...)
}
// context key with a separate type, so no other package has a chance of accessing it
type key int
// the value actually doesn't matter, the type alone will guarantee no package gets at this context value
const RequestIdKey key = 0
const defaultReqId = "00000000"
func GetRequestID(ctx context.Context) string {
reqId := ctx.Value(RequestIdKey)
if reqId == nil {
return defaultReqId
}
return reqId.(string)
}
func SetupLogging(applicationName string, useEcsLogging bool) {
aulogging.RequestIdRetriever = GetRequestID
if useEcsLogging {
auzerolog.SetupJsonLogging(applicationName)
zerolog.TimeFieldFormat = "2006-01-02T15:04:05.000Z"
} else {
aulogging.DefaultRequestIdValue = defaultReqId
auzerolog.SetupPlaintextLogging()
}
}
func SetLoglevel(severity string) {
switch severity {
case "DEBUG":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case "INFO":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "WARN":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "ERROR":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
default:
}
}
func LoggerFromContext(ctx context.Context) Logger {
logger := aulogging.Logger.Ctx(ctx)
return &loggingWrapper{
logger: logger,
}
}
func NewLogger() Logger {
logger := aulogging.Logger.NoCtx()
return &loggingWrapper{
logger: logger,
}
}
func ChildCtxWithRequestID(ctx context.Context, reqId string) context.Context {
return context.WithValue(ctx, RequestIdKey, reqId)
}
func NewNoopLogger() Logger {
return &noopLogger{}
}
type noopLogger struct {
}
func (l *noopLogger) Debug(format string, v ...interface{}) {
}
func (l *noopLogger) Info(format string, v ...interface{}) {
}
func (l *noopLogger) Warn(format string, v ...interface{}) {
}
func (l *noopLogger) Error(format string, v ...interface{}) {
}
func (l *noopLogger) Fatal(format string, v ...interface{}) {
}