From 79f2814fef82867187d08f8ceb58de683c10afc5 Mon Sep 17 00:00:00 2001 From: ucpr Date: Sun, 26 May 2024 00:27:46 +0900 Subject: [PATCH] Revert "refactor: modify logging for cloud logging" This reverts commit 108421ad607200ee654d6294c3f3522bf8030f4c. --- pkg/log/gcp_handler.go | 41 --------------------------------- pkg/log/log.go | 52 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 50 deletions(-) delete mode 100644 pkg/log/gcp_handler.go diff --git a/pkg/log/gcp_handler.go b/pkg/log/gcp_handler.go deleted file mode 100644 index 771430d..0000000 --- a/pkg/log/gcp_handler.go +++ /dev/null @@ -1,41 +0,0 @@ -package log - -import ( - "fmt" - "log/slog" - "runtime" -) - -func labelsAttr(service, env string) slog.Attr { - return slog.Group("logging.googleapis.com/labels", - slog.String("app", service), - slog.String("env", env), - ) -} - -// attrReplacerForCloudLogging replaces slog's default attributes for Google Cloud Logging. -func attrReplacerForCloudLogging(groups []string, attr slog.Attr) slog.Attr { - switch attr.Key { - case slog.MessageKey: - attr.Key = "message" - case slog.LevelKey: - attr.Key = "severity" - // Replace the value of the "severity" attribute with the value of the "level" attribute. - level, ok := attr.Value.Any().(slog.Level) - if ok { - attr.Value = toLogLevel(level) - } - case slog.SourceKey: - attr.Key = "logging.googleapis.com/sourceLocation" - // Replace the value of the "source" attribute with the value of the "sourceLocation" attribute. - const skip = 7 - _, file, line, ok := runtime.Caller(skip) - if !ok { - return attr - } - v := fmt.Sprintf("%s:%d", file, line) - attr.Value = slog.StringValue(v) - } - - return attr -} diff --git a/pkg/log/log.go b/pkg/log/log.go index 4a939e3..4fe38ee 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -2,20 +2,23 @@ package log import ( "context" + "fmt" "log/slog" "os" + "runtime" + + "cloud.google.com/go/logging" ) //nolint:gochecknoglobals var ( - // Custom severity levels. - SeverityDebug = slog.Level(-1) - SeverityInfo = slog.Level(10) - SeverityNotice = slog.Level(20) - SeverityWarning = slog.Level(30) - SeverityError = slog.Level(40) - SeverityCritical = slog.Level(50) - SeverityDefault = slog.Level(SeverityInfo) + SeverityDefault = slog.Level(logging.Default) + SeverityDebug = slog.Level(logging.Debug) + SeverityInfo = slog.Level(logging.Info) + SeverityNotice = slog.Level(logging.Notice) + SeverityWarning = slog.Level(logging.Warning) + SeverityError = slog.Level(logging.Error) + SeverityCritical = slog.Level(logging.Critical) ) // logger is the global logger. @@ -42,7 +45,10 @@ func newHandler(format, service, env string) slog.Handler { ReplaceAttr: attrReplacerForCloudLogging, }) handler.WithAttrs([]slog.Attr{ - labelsAttr(service, env), + slog.Group("logging.googleapis.com/labels", + slog.String("app", service), + slog.String("env", env), + ), }) return handler case "json": @@ -68,6 +74,34 @@ func attrReplacerForDefault(groups []string, attr slog.Attr) slog.Attr { return attr } +// attrReplacerForCloudLogging replaces slog's default attributes for Google Cloud Logging. +func attrReplacerForCloudLogging(groups []string, attr slog.Attr) slog.Attr { + switch attr.Key { + case slog.MessageKey: + attr.Key = "message" + case slog.LevelKey: + attr.Key = "severity" + attr.Value = slog.StringValue(logging.Severity(attr.Value.Any().(slog.Level)).String()) + // Replace the value of the "severity" attribute with the value of the "level" attribute. + level, ok := attr.Value.Any().(slog.Level) + if ok { + attr.Value = toLogLevel(level) + } + case slog.SourceKey: + attr.Key = "logging.googleapis.com/sourceLocation" + // Replace the value of the "source" attribute with the value of the "sourceLocation" attribute. + const skip = 7 + _, file, line, ok := runtime.Caller(skip) + if !ok { + return attr + } + v := fmt.Sprintf("%s:%d", file, line) + attr.Value = slog.StringValue(v) + } + + return attr +} + // toLogLevel converts a slog.Level to a slog.Value. func toLogLevel(level slog.Level) slog.Value { ls := "DEFAULT"