Skip to content

Commit aec53fb

Browse files
authored
Add helper for GoogleCloud field mapping (#177)
1 parent 6dbf6ef commit aec53fb

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

helper.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3536
type 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.
4857
func 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
}

helper_test.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package logging
22

33
import (
4+
"bytes"
45
"context"
6+
"encoding/json"
57
"net/http"
68
"testing"
79
"time"
810

9-
"github.com/snabble/go-logging/v2/tracex"
1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
13+
14+
"github.com/snabble/go-logging/v2/tracex"
1215
)
1316

1417
func Test_LifecycleStart_AcceptsNil(t *testing.T) {
@@ -34,3 +37,51 @@ func Test_Call_UsesTrace(t *testing.T) {
3437
assert.Contains(t, capture.String(), "trace")
3538
assert.Contains(t, capture.String(), "span")
3639
}
40+
41+
func Test_SetGoogle(t *testing.T) {
42+
require.NoError(t, SetGoogle("debug"))
43+
defer SetWithConfig("info", &DefaultLogConfig) // Reset to default
44+
45+
tt := []struct {
46+
level string
47+
message string
48+
logFn func()
49+
}{
50+
{
51+
level: "debug",
52+
message: "__debug__",
53+
logFn: func() { Log.Debug("__debug__") },
54+
},
55+
{
56+
level: "info",
57+
message: "__info__",
58+
logFn: func() { Log.Info("__info__") },
59+
},
60+
{
61+
level: "warning",
62+
message: "__warn__",
63+
logFn: func() { Log.Warn("__warn__") },
64+
},
65+
{
66+
level: "error",
67+
message: "__error__",
68+
logFn: func() { Log.Error("__error__") },
69+
},
70+
}
71+
72+
for _, tc := range tt {
73+
t.Run(tc.level, func(t *testing.T) {
74+
b := bytes.NewBuffer(nil)
75+
Log.Out = b
76+
77+
tc.logFn()
78+
79+
result := map[string]string{}
80+
require.NoError(t, json.Unmarshal(b.Bytes(), &result))
81+
82+
assert.Equal(t, tc.message, result["message"])
83+
assert.Equal(t, tc.level, result["severity"])
84+
assert.NotEmpty(t, result["timestamp"])
85+
})
86+
}
87+
}

0 commit comments

Comments
 (0)