@@ -11,6 +11,7 @@ import (
11
11
"time"
12
12
13
13
"github.com/sirupsen/logrus"
14
+
14
15
"github.com/snabble/go-logging/v2/tracex"
15
16
)
16
17
@@ -33,8 +34,9 @@ var DefaultLogConfig = LogConfig{
33
34
}
34
35
35
36
type LogConfig struct {
36
- EnableTraces bool
37
- EnableTextLogging bool
37
+ EnableTraces bool
38
+ EnableTextLogging bool
39
+ googleCloudLogging bool
38
40
}
39
41
40
42
// Set creates a new Logger with the matching specification
@@ -43,6 +45,13 @@ func Set(level string, textLogging bool) error {
43
45
return SetWithConfig (level , config )
44
46
}
45
47
48
+ // SetGoogle configures the Logger to use GoogleCloud compatible fields in JSON format
49
+ // https://cloud.google.com/logging/docs/structured-logging#structured_logging_special_fields
50
+ func SetGoogle (level string ) error {
51
+ config := & LogConfig {EnableTraces : true , googleCloudLogging : true }
52
+ return SetWithConfig (level , config )
53
+ }
54
+
46
55
// SetWithConfig creates a new Logger with the matching specification based on the config, pass nil to use
47
56
// the defaults.
48
57
func SetWithConfig (level string , config * LogConfig ) error {
@@ -58,6 +67,15 @@ func SetWithConfig(level string, config *LogConfig) error {
58
67
logger := logrus .New ()
59
68
if config .EnableTextLogging {
60
69
logger .Formatter = & logrus.TextFormatter {DisableColors : true }
70
+ } else if config .googleCloudLogging {
71
+ logger .Formatter = & logrus.JSONFormatter {
72
+ FieldMap : logrus.FieldMap {
73
+ logrus .FieldKeyTime : "timestamp" ,
74
+ logrus .FieldKeyLevel : "severity" ,
75
+ logrus .FieldKeyMsg : "message" ,
76
+ },
77
+ TimestampFormat : time .RFC3339Nano ,
78
+ }
61
79
} else {
62
80
logger .Formatter = & LogstashFormatter {TimestampFormat : time .RFC3339Nano }
63
81
}
0 commit comments