diff --git a/pkg/log/format.go b/pkg/log/format.go index 13f373e7b..57a97e250 100644 --- a/pkg/log/format.go +++ b/pkg/log/format.go @@ -10,9 +10,21 @@ import ( ) func getGCPFormatter(level slog.Level) *slog.Logger { - - l := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: level})) - return l + return slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ + Level: level, + ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { + switch a.Key { + case slog.LevelKey: + return slog.String("severity", a.Value.String()) + case slog.MessageKey: + return slog.String("message", a.Value.String()) + case slog.TimeKey: + return slog.String("timestamp", a.Value.Time().Format(time.RFC3339)) + default: + return a + } + }, + })) } const lightGrey = 0xffccc diff --git a/pkg/middleware/log_entry.go b/pkg/middleware/log_entry.go index 8d6016cf7..980637cae 100644 --- a/pkg/middleware/log_entry.go +++ b/pkg/middleware/log_entry.go @@ -6,7 +6,6 @@ import ( "github.com/gomods/athens/pkg/log" "github.com/gomods/athens/pkg/requestid" "github.com/gorilla/mux" - "github.com/sirupsen/logrus" ) // LogEntryMiddleware builds a log.Entry, setting the request fields @@ -15,7 +14,7 @@ func LogEntryMiddleware(lggr *log.Logger) mux.MiddlewareFunc { return func(h http.Handler) http.Handler { f := func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - ent := lggr.WithFields(logrus.Fields{ + ent := lggr.WithFields(map[string]interface{}{ "http-method": r.Method, "http-path": r.URL.Path, "request-id": requestid.FromContext(ctx), diff --git a/pkg/middleware/log_entry_test.go b/pkg/middleware/log_entry_test.go index 7ce3e87b2..1b7b4e9a2 100644 --- a/pkg/middleware/log_entry_test.go +++ b/pkg/middleware/log_entry_test.go @@ -23,10 +23,10 @@ func TestLogContext(t *testing.T) { r := mux.NewRouter() r.HandleFunc("/test", h) - var buf bytes.Buffer + buf := &bytes.Buffer{} lggr := log.New("", slog.LevelDebug, "") opts := slog.HandlerOptions{Level: slog.LevelDebug} - handler := slog.NewJSONHandler(&buf, &opts) + handler := slog.NewJSONHandler(buf, &opts) lggr.Logger = slog.New(handler) r.Use(LogEntryMiddleware(lggr))