Skip to content

Commit

Permalink
feat: add structured logging (#40)
Browse files Browse the repository at this point in the history
We were previously using the log package as a backend to `logr`. Since
1.21, go supports `slog`, which provides native structured logging.

This commit swaps out our `logr` backend to use `slog`. We still want to
use `logr`, since it provides a nicer interface imo. There are some
compatibility quirks, in particular around log levels. `logr` expects
levels of verbosity, whereas in `slog` levels are tied to severity. For
now we just negate it.
  • Loading branch information
jta authored Oct 30, 2023
1 parent 24e1c3d commit 592df95
Show file tree
Hide file tree
Showing 9 changed files with 427 additions and 385 deletions.
14 changes: 7 additions & 7 deletions cmd/forwarder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ package main
import (
"context"
"fmt"
stdlog "log"
"os"

"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/go-logr/logr"
"github.com/go-logr/stdr"
"github.com/sethvargo/go-envconfig"

"github.com/observeinc/aws-sam-testing/handlers/forwarder"
"github.com/observeinc/aws-sam-testing/logging"
)

var env struct {
Expand Down Expand Up @@ -41,9 +39,11 @@ func realInit() error {
return fmt.Errorf("failed to load environment variables: %w", err)
}

stdr.SetVerbosity(env.Verbosity)
logger = stdr.NewWithOptions(stdlog.New(os.Stderr, "", stdlog.LstdFlags), stdr.Options{LogCaller: stdr.All})
logger.V(6).Info("initialized", "config", env)
logger = logging.New(&logging.Config{
Verbosity: env.Verbosity,
})

logger.V(4).Info("initialized", "config", env)

awsCfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
Expand All @@ -68,7 +68,7 @@ func realInit() error {
}

if awsCfg.Region != region {
logger.V(6).Info("modifying s3 client region", "region", region)
logger.V(4).Info("modifying s3 client region", "region", region)
regionCfg := awsCfg.Copy()
regionCfg.Region = region
handler.S3Client = s3.NewFromConfig(regionCfg)
Expand Down
21 changes: 21 additions & 0 deletions logging/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package logging

import (
"log/slog"
"os"

"github.com/go-logr/logr"
"github.com/go-logr/logr/slogr"
)

type Config struct {
Verbosity int
}

func New(config *Config) logr.Logger {
logOptions := slog.HandlerOptions{
AddSource: true,
Level: slog.Level(-config.Verbosity),
}
return slogr.NewLogr(slog.NewJSONHandler(os.Stderr, &logOptions))
}
168 changes: 168 additions & 0 deletions vendor/github.com/go-logr/logr/slogr/sloghandler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 108 additions & 0 deletions vendor/github.com/go-logr/logr/slogr/slogr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 592df95

Please sign in to comment.