Skip to content

Commit

Permalink
Merge pull request #8 from packethost/logging
Browse files Browse the repository at this point in the history
Fix requests using echo logger instead of global zap logger
  • Loading branch information
code-vespa authored Mar 29, 2022
2 parents 8e6c85f + 53d0091 commit 0932957
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 13 deletions.
6 changes: 3 additions & 3 deletions cmd/aws-s3-proxy/pkg/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ func setupLogging() {
cfg = zap.NewDevelopmentConfig()
}

// Level is already at InfoLevel, so only change to DebugLevel
// when the flag is set
if viper.GetBool("logging.debug") {
cfg.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
} else {
cfg.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
cfg.Level.SetLevel(zap.DebugLevel)
}

l, err := cfg.Build()
Expand Down
9 changes: 3 additions & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import (
// Cfg represents its configurations
var Cfg *Config

// Logger exports a logger to be used by all parts of the application
var Logger *zap.SugaredLogger

// ReadThrough holds info if we are transparently reading back to upstream
type ReadThrough struct {
Enabled bool
Expand Down Expand Up @@ -66,7 +63,7 @@ type ServerOpts struct {
type Config struct {
HTTPOpts HTTPOpts
ServerOpts ServerOpts
Logger zap.SugaredLogger
Logger *zap.SugaredLogger
SecondaryStore Bucket
PrimaryStore Bucket
ReadThrough ReadThrough
Expand All @@ -78,12 +75,12 @@ func Load(ctx context.Context, l *zap.SugaredLogger) {
log.Fatalf("Unable to decode into struct, %v", err)
}

Logger = l
Cfg.Logger = l

Cfg.PrimaryStore.BuildS3API()
Cfg.SecondaryStore.BuildS3API()

Logger.Info("configuration loaded")
Cfg.Logger.Info("configuration loaded")
}

// BuildS3API creates a client per bucket
Expand Down
4 changes: 2 additions & 2 deletions internal/s3/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func AwsS3Get(e echo.Context) error {
get, err := get(req.Context(), &c.PrimaryStore, path, rangeHeader)
if err != nil {
if c.ReadThrough.Enabled {
e.Logger().Warn("err in primary, trying secondary")
c.Logger.Warn("err in primary, trying secondary")
return trySecondary(e)
}

Expand All @@ -115,7 +115,7 @@ func AwsS3Get(e echo.Context) error {
status, _ = strconv.Atoi(statusStr[1])
}

e.Logger().Errorf("status %d", status)
c.Logger.Errorf("status %d", status)

return e.String(status, err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion internal/s3/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/packethost/aws-s3-proxy/internal/config"
)

// Handler wraps every the controllers
// Handler wraps every controller
func Handler(handler echo.HandlerFunc) echo.HandlerFunc {
return echo.HandlerFunc(func(e echo.Context) error {
h := config.Cfg.HTTPOpts
Expand Down
2 changes: 1 addition & 1 deletion internal/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Upload struct {
// Get returns a specified object from Amazon S3
func get(ctx context.Context, bucket *config.Bucket, key, rangeHeader *string) (*Download, error) {
if bucket.Session == nil {
config.Logger.Panic("bad s3 client")
config.Cfg.Logger.Panic("bad s3 client")
}

req := &s3.GetObjectInput{
Expand Down

0 comments on commit 0932957

Please sign in to comment.