Skip to content

Commit

Permalink
feat(log): Add set logger to context function and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyno-zeta committed Jul 3, 2021
1 parent 08f5395 commit 5db6244
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
14 changes: 14 additions & 0 deletions pkg/s3-proxy/log/contextkey.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
package log

import "context"

// contextKey is a value for use with context.WithValue. It's used as
// a pointer so it fits in an interface{} without allocation. This technique
// for defining context keys was copied from Go 1.7's new use of context in net/http.
type contextKey struct {
name string
}

var loggerContextKey = &contextKey{name: "LOGGER_CONTEXT_KEY"}

func GetLoggerFromContext(ctx context.Context) Logger {
res, _ := ctx.Value(loggerContextKey).(Logger)

return res
}

func SetLoggerInContext(ctx context.Context, logger Logger) context.Context {
return context.WithValue(ctx, loggerContextKey, logger)
}
10 changes: 0 additions & 10 deletions pkg/s3-proxy/log/log.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package log

import (
"context"

"github.com/sirupsen/logrus"
)

var loggerContextKey = &contextKey{name: "LOGGER_CONTEXT_KEY"}

type Logger interface {
Configure(level string, format string, filePath string) error

Expand Down Expand Up @@ -61,9 +57,3 @@ func NewLogger() Logger {
FieldLogger: logrus.New(),
}
}

func GetLoggerFromContext(ctx context.Context) Logger {
res, _ := ctx.Value(loggerContextKey).(Logger)

return res
}

0 comments on commit 5db6244

Please sign in to comment.