diff --git a/pkg/log/gcp_handler.go b/pkg/log/gcp_handler.go new file mode 100644 index 0000000..771430d --- /dev/null +++ b/pkg/log/gcp_handler.go @@ -0,0 +1,41 @@ +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 4fe38ee..4a939e3 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -2,23 +2,20 @@ package log import ( "context" - "fmt" "log/slog" "os" - "runtime" - - "cloud.google.com/go/logging" ) //nolint:gochecknoglobals var ( - 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) + // 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) ) // logger is the global logger. @@ -45,10 +42,7 @@ func newHandler(format, service, env string) slog.Handler { ReplaceAttr: attrReplacerForCloudLogging, }) handler.WithAttrs([]slog.Attr{ - slog.Group("logging.googleapis.com/labels", - slog.String("app", service), - slog.String("env", env), - ), + labelsAttr(service, env), }) return handler case "json": @@ -74,34 +68,6 @@ 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"