-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.go
78 lines (65 loc) · 2.15 KB
/
setup.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
package auzerolog
import (
"bytes"
aulogging "github.com/StephanHCB/go-autumn-logging"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"os"
)
var RequestIdFieldName = "request-id"
// SetupJsonLogging configures go-autumn-logging to log via rs/zerolog using a subset of ECS
// see https://www.elastic.co/guide/en/ecs/1.4
func SetupJsonLogging(serviceId string) {
zerolog.TimestampFieldName = "@timestamp"
zerolog.LevelFieldName = "log.level"
zerolog.MessageFieldName = "message" // correct by default
log.Logger = zerolog.New(os.Stdout).With().
Timestamp().
Str("service.id", serviceId).
Logger()
zerolog.SetGlobalLevel(zerolog.InfoLevel)
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMicro
IsJson = true
}
var IsJson = false
func plaintextFieldNameOmitter(name interface{}) string {
return ""
}
func plaintextFieldValueOmitter(value interface{}) string {
return ""
}
func plaintextRequestIdFormatter(value interface{}) string {
vStr, ok := value.(string)
if !ok {
return aulogging.DefaultRequestIdValue
}
return "[" + vStr + "]"
}
// SetupPlaintextLogging configures go-autumn-logging to log via rs/zerolog using a simple plaintext logger
func SetupPlaintextLogging() {
zerolog.CallerFieldName = RequestIdFieldName // avoid double printing the request id (bit of a hack)
log.Logger = log.With().Str(RequestIdFieldName, aulogging.DefaultRequestIdValue).Logger().Output(
zerolog.ConsoleWriter{
Out: os.Stdout,
NoColor: true,
TimeFormat: "15:04:05.000",
PartsOrder: []string{
zerolog.TimestampFieldName,
zerolog.LevelFieldName,
RequestIdFieldName,
zerolog.MessageFieldName,
},
FormatFieldName: plaintextFieldNameOmitter,
FormatFieldValue: plaintextFieldValueOmitter,
FormatCaller: plaintextRequestIdFormatter,
})
zerolog.SetGlobalLevel(zerolog.InfoLevel)
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMicro
IsJson = false
}
var RecordedLogForTesting = new(bytes.Buffer)
// Setup function for testing that records log entries instead of writing them to console
func SetupForTesting() {
SetupPlaintextLogging()
log.Logger = zerolog.New(RecordedLogForTesting).With().Timestamp().Logger()
}