Skip to content

Commit

Permalink
replace slog with zap since we dont use go1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
hspitzley-czi committed Nov 13, 2023
1 parent 81f331f commit 42893bc
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions api/pkg/request/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ package request

import (
"context"
"log/slog"
"os"
"time"

"github.com/chanzuckerberg/happy/api/pkg/ent/ogent"
"github.com/chanzuckerberg/happy/api/pkg/setup"
"github.com/ogen-go/ogen/middleware"
"go.uber.org/zap"
)

type LoggerKey struct{}

func MakeOgentLoggerMiddleware(cfg *setup.Configuration) ogent.Middleware {
return func(req middleware.Request, next middleware.Next) (middleware.Response, error) {
log := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: resolveLogLevel(cfg.Api.LogLevel),
}))
logger, err := newLogger(cfg.Api.LogLevel)

Check failure on line 17 in api/pkg/request/logger.go

View workflow job for this annotation

GitHub Actions / lint-api

ineffectual assignment to err (ineffassign)
defer logger.Sync()

Check failure on line 18 in api/pkg/request/logger.go

View workflow job for this annotation

GitHub Actions / lint-api

Error return value of `logger.Sync` is not checked (errcheck)

start := time.Now()
req.Context = context.WithValue(req.Context, LoggerKey{}, log)
req.Context = context.WithValue(req.Context, LoggerKey{}, logger)

res, err := next(req)

Expand All @@ -31,36 +30,40 @@ func MakeOgentLoggerMiddleware(cfg *setup.Configuration) ogent.Middleware {
}

if err == nil {
log.Info("Success", getLogArgs(status, start, req)...)
logger.Info("Success", getLogArgs(status, start, req)...)
} else {
if terr, ok := err.(interface{ GetCode() int }); ok {

Check failure on line 35 in api/pkg/request/logger.go

View workflow job for this annotation

GitHub Actions / lint-api

type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors (errorlint)
status = terr.GetCode()
}
args := append([]any{"error", err}, getLogArgs(status, start, req)...)
log.Error("Fail", args...)
args := append([]zap.Field{zap.String("error", err.Error())}, getLogArgs(status, start, req)...)
logger.Error("Fail", args...)
}

return res, err
}
}

func getLogArgs(status int, start time.Time, req middleware.Request) []any {
return []any{
"status", status, "duration", time.Since(start), "method", req.Raw.Method, "path", req.Raw.URL.Path, "query", req.Raw.URL.RawQuery,
func getLogArgs(status int, start time.Time, req middleware.Request) []zap.Field {
return []zap.Field{
zap.Int("status", status),
zap.Duration("duration", time.Since(start)),
zap.String("method", req.Raw.Method),
zap.String("path", req.Raw.URL.Path),
zap.String("query", req.Raw.URL.RawQuery),
}
}

func resolveLogLevel(logLevel string) slog.Level {
func newLogger(logLevel string) (*zap.Logger, error) {
switch logLevel {
case "debug":
return slog.LevelDebug
return zap.NewDevelopment()
case "error":
return slog.LevelError
return zap.NewProduction()
case "warn":
return slog.LevelWarn
return zap.NewProduction()
case "silent":
return slog.LevelError
return zap.NewNop(), nil
default:
return slog.LevelInfo
return zap.NewProduction()
}
}

0 comments on commit 42893bc

Please sign in to comment.