@@ -11,6 +11,7 @@ import (
1111 "time"
1212
1313 "github.com/sirupsen/logrus"
14+
1415 "github.com/snabble/go-logging/v2/tracex"
1516)
1617
@@ -33,8 +34,9 @@ var DefaultLogConfig = LogConfig{
3334}
3435
3536type LogConfig struct {
36- EnableTraces bool
37- EnableTextLogging bool
37+ EnableTraces bool
38+ EnableTextLogging bool
39+ googleCloudLogging bool
3840}
3941
4042// Set creates a new Logger with the matching specification
@@ -43,6 +45,13 @@ func Set(level string, textLogging bool) error {
4345 return SetWithConfig (level , config )
4446}
4547
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+
4655// SetWithConfig creates a new Logger with the matching specification based on the config, pass nil to use
4756// the defaults.
4857func SetWithConfig (level string , config * LogConfig ) error {
@@ -58,6 +67,15 @@ func SetWithConfig(level string, config *LogConfig) error {
5867 logger := logrus .New ()
5968 if config .EnableTextLogging {
6069 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+ }
6179 } else {
6280 logger .Formatter = & LogstashFormatter {TimestampFormat : time .RFC3339Nano }
6381 }
0 commit comments