Skip to content

Commit

Permalink
refactor: modify logging for cloud logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ucpr committed May 16, 2024
1 parent 3656fc5 commit 108421a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 43 deletions.
41 changes: 41 additions & 0 deletions pkg/log/gcp_handler.go
Original file line number Diff line number Diff line change
@@ -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
}
52 changes: 9 additions & 43 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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":
Expand All @@ -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"
Expand Down

0 comments on commit 108421a

Please sign in to comment.