Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make logging keys fluentD compatible #1591

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 14 additions & 12 deletions internal/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,25 @@ import (
"bytes"
"log/slog"
"os"
"strings"
"regexp"
"testing"

"github.com/googlecloudplatform/gcsfuse/internal/config"
. "github.com/jacobsa/ogletest"
)

const (
textTraceString = "severity=TRACE msg=\"TestLogs: www.traceExample.com\""
textDebugString = "severity=DEBUG msg=\"TestLogs: www.debugExample.com\""
textInfoString = "severity=INFO msg=\"TestLogs: www.infoExample.com\""
textWarningString = "severity=WARNING msg=\"TestLogs: www.warningExample.com\""
textErrorString = "severity=ERROR msg=\"TestLogs: www.errorExample.com\""
jsonTraceString = "\"severity\":\"TRACE\",\"msg\":\"TestLogs: www.traceExample.com\"}"
jsonDebugString = "\"severity\":\"DEBUG\",\"msg\":\"TestLogs: www.debugExample.com\"}"
jsonInfoString = "\"severity\":\"INFO\",\"msg\":\"TestLogs: www.infoExample.com\"}"
jsonWarningString = "\"severity\":\"WARNING\",\"msg\":\"TestLogs: www.warningExample.com\"}"
jsonErrorString = "\"severity\":\"ERROR\",\"msg\":\"TestLogs: www.errorExample.com\"}"
textTraceString = "^time=\"[a-zA-Z0-9/:. ]{26}\" severity=TRACE message=\"TestLogs: www.traceExample.com\""
textDebugString = "^time=\"[a-zA-Z0-9/:. ]{26}\" severity=DEBUG message=\"TestLogs: www.debugExample.com\""
textInfoString = "^time=\"[a-zA-Z0-9/:. ]{26}\" severity=INFO message=\"TestLogs: www.infoExample.com\""
textWarningString = "^time=\"[a-zA-Z0-9/:. ]{26}\" severity=WARNING message=\"TestLogs: www.warningExample.com\""
textErrorString = "^time=\"[a-zA-Z0-9/:. ]{26}\" severity=ERROR message=\"TestLogs: www.errorExample.com\""

jsonTraceString = "^{\"timestamp\":{\"seconds\":\\d{10},\"nanos\":\\d{9}},\"severity\":\"TRACE\",\"message\":\"TestLogs: www.traceExample.com\"}"
jsonDebugString = "^{\"timestamp\":{\"seconds\":\\d{10},\"nanos\":\\d{9}},\"severity\":\"DEBUG\",\"message\":\"TestLogs: www.debugExample.com\"}"
jsonInfoString = "^{\"timestamp\":{\"seconds\":\\d{10},\"nanos\":\\d{9}},\"severity\":\"INFO\",\"message\":\"TestLogs: www.infoExample.com\"}"
jsonWarningString = "^{\"timestamp\":{\"seconds\":\\d{10},\"nanos\":\\d{9}},\"severity\":\"WARNING\",\"message\":\"TestLogs: www.warningExample.com\"}"
jsonErrorString = "^{\"timestamp\":{\"seconds\":\\d{10},\"nanos\":\\d{9}},\"severity\":\"ERROR\",\"message\":\"TestLogs: www.errorExample.com\"}"
)

func TestLogger(t *testing.T) { RunTests(t) }
Expand Down Expand Up @@ -99,7 +100,8 @@ func validateOutput(expected []string, output []string) {
if expected[i] == "" {
AssertEq(expected[i], output[i])
} else {
AssertTrue(strings.Contains(output[i], expected[i]))
expectedRegexp := regexp.MustCompile(expected[i])
AssertTrue(expectedRegexp.MatchString(output[i]))
}
}
}
Expand Down
16 changes: 10 additions & 6 deletions internal/logger/slog_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ const (
LevelWarn = slog.LevelWarn
LevelError = slog.LevelError
// LevelOff value is set to 12, so that nothing is logged.
LevelOff = slog.Level(12)

timestampSecondsKey = "timestampSeconds"
timestampNanosKey = "timestampNanos"
LevelOff = slog.Level(12)
messageKey = "message"
timestampKey = "timestamp"
secondsKey = "seconds"
nanosKey = "nanos"
)

func setLoggingLevel(level config.LogSeverity, programLevel *slog.LevelVar) {
Expand Down Expand Up @@ -82,6 +83,9 @@ func customiseLevels(a *slog.Attr) {

// AddPrefixToMessage adds the prefix to the log message.
func addPrefixToMessage(a *slog.Attr, prefix string) {
// Change key name to "message" so that it is compatible with fluentD.
a.Key = messageKey
// Add prefix to the message.
message := a.Value.Any().(string)
var sb strings.Builder
sb.WriteString(prefix)
Expand All @@ -91,13 +95,13 @@ func addPrefixToMessage(a *slog.Attr, prefix string) {

// CustomiseTimeFormat converts the time to below specified format:
// 1. for json logs:
// "time":{"timestampSeconds":1694164762,"timestampNanos":1694164762084862036}
// "timestamp":{"seconds":1704697907,"nanos":553918512}
// 2. for text logs:
// time="08/09/2023 09:24:54.437193"
func customiseTimeFormat(a *slog.Attr, format string) {
currTime := a.Value.Any().(time.Time).Round(0)
if format == "json" {
*a = slog.Group(slog.TimeKey, timestampSecondsKey, currTime.Unix(), timestampNanosKey, currTime.UnixNano())
*a = slog.Group(timestampKey, secondsKey, currTime.Unix(), nanosKey, currTime.Nanosecond())
} else {
a.Value = slog.StringValue(currTime.Round(0).Format("02/01/2006 03:04:05.000000"))
}
Expand Down