Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new option sensitive, which can be used to hide sensitive args #70

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 87 additions & 5 deletions log/slog/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"sync"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
Expand All @@ -12,7 +13,9 @@ import (
)

type loggerWrapper struct {
logger log.Logger
logger log.Logger
mutex sync.Mutex
sensitive bool // if sensitive is true, dont log the values
}

const defaultMsgKey = "msg"
Expand All @@ -21,28 +24,31 @@ const defaultErrKey = "error"
var (
defaultLoggerWrapper *loggerWrapper
logLevel string
logSensitive bool
callerDepth int
)

func init() {
logLevel = env.String("LOG_LEVEL", "info")
logSensitive = env.Bool("LOG_SENSITIVE", true)
SubhrajitPrusty marked this conversation as resolved.
Show resolved Hide resolved
callerDepth = env.Int("LOG_CALLER_DEPTH", 4)
defaultLoggerWrapper = newloggerWrapper(logLevel)
defaultLoggerWrapper = newloggerWrapper(logLevel, logSensitive)
}

// NewLogger returns a new instance of Logger.
func NewLogger() Logger {
return newloggerWrapper(logLevel)
return newloggerWrapper(logLevel, logSensitive)
}

func newloggerWrapper(logLevel string) *loggerWrapper {
func newloggerWrapper(logLevel string, sensitive bool) *loggerWrapper {
logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
logger = level.NewFilter(logger, levelFilter(logLevel))
logger = log.With(logger, "ts", log.DefaultTimestamp)
logger = log.With(logger, "caller", log.Caller(callerDepth))

return &loggerWrapper{
logger: logger,
logger: logger,
sensitive: sensitive,
}
}

Expand Down Expand Up @@ -72,22 +78,37 @@ func mapToSlice(m map[string]any) []any {

// Info logs a line with level info using the loggerWrapper instance.
func (l *loggerWrapper) Info(msg string, args ...any) {
if l.sensitive {
vabshere marked this conversation as resolved.
Show resolved Hide resolved
args = make([]any, 0)
}
level.Info(log.With(l.logger, defaultMsgKey, msg)).Log(args...)
}

// Warn logs a line with level warn using the loggerWrapper instance.
func (l *loggerWrapper) Warn(msg string, args ...any) {
if l.sensitive {
args = make([]any, 0)
}

level.Warn(log.With(l.logger, defaultMsgKey, msg)).Log(args...)
}

// Debug logs a line with level debug using a loggerWrapper instance.
func (l *loggerWrapper) Debug(msg string, args ...any) {
if l.sensitive {
args = make([]any, 0)
}

level.Debug(log.With(l.logger, defaultMsgKey, msg)).Log(args...)
}

// Error logs a line with level error using a loggerWrapper instance.
// If err is not nil it adds only the msg string or vice-versa. Otherwise adds both.
func (l *loggerWrapper) Error(err error, msg string, args ...any) {
if l.sensitive {
args = make([]any, 0)
}

if err == nil {
level.Error(log.With(l.logger, defaultMsgKey, msg)).Log(args...)
return
Expand All @@ -103,22 +124,38 @@ func (l *loggerWrapper) Error(err error, msg string, args ...any) {

// Infof logs a format line with level info using the loggerWrapper instance.
func (l *loggerWrapper) Infof(format string, args ...any) {
if l.sensitive {
args = make([]any, 0)
}

level.Info(l.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))
}

// Warnf logs a format line with level warn using the loggerWrapper instance.
func (l *loggerWrapper) Warnf(format string, args ...any) {
if l.sensitive {
args = make([]any, 0)
}

level.Warn(l.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))
}

// Debugf logs a format line with level debug using the loggerWrapper instance.
func (l *loggerWrapper) Debugf(format string, args ...any) {
if l.sensitive {
args = make([]any, 0)
}

level.Debug(l.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))
}

// Errorf logs a format line with level error using a loggerWrapper instance.
// If err is not nil it adds only the msg string or vice-versa. Otherwise adds both.
func (l *loggerWrapper) Errorf(err error, format string, args ...any) {
if l.sensitive {
args = make([]any, 0)
}

if err == nil {
level.Error(l.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))
return
Expand Down Expand Up @@ -152,24 +189,53 @@ func (l *loggerWrapper) WithFields(fields map[string]any) Logger {
}
}

// WithSensitive returns a pointer to updated loggerWrapper with sensitive flag updated to the logger.
func (l *loggerWrapper) WithSensitive(sensitive bool) Logger {
logger := log.With(l.logger)

return &loggerWrapper{
logger: logger,
sensitive: sensitive,
}
}

func (l *loggerWrapper) SetSensitive(val bool) {
l.mutex.Lock()
defer l.mutex.Unlock()
l.sensitive = val
}

// Info logs a line with level Info.
func Info(msg string, args ...any) {
if defaultLoggerWrapper.sensitive {
args = make([]any, 0)
}
level.Info(log.With(defaultLoggerWrapper.logger, defaultMsgKey, msg)).Log(args...)
}

// Warn logs a line with level warn.
func Warn(msg string, args ...any) {
if defaultLoggerWrapper.sensitive {
args = make([]any, 0)
}
level.Warn(log.With(defaultLoggerWrapper.logger, defaultMsgKey, msg)).Log(args...)
}

// Debug logs a line with level debug.
func Debug(msg string, args ...any) {
if defaultLoggerWrapper.sensitive {
args = make([]any, 0)
}
level.Debug(log.With(defaultLoggerWrapper.logger, defaultMsgKey, msg)).Log(args...)
}

// Error logs a line with level error.
// If err is not nil it adds only the msg string or vice-versa. Otherwise adds both.
func Error(err error, msg string, args ...any) {
if defaultLoggerWrapper.sensitive {
args = make([]any, 0)
}

if err == nil {
level.Error(log.With(defaultLoggerWrapper.logger, defaultMsgKey, msg)).Log(args...)
return
Expand All @@ -185,22 +251,38 @@ func Error(err error, msg string, args ...any) {

// Infof logs a format line with level info.
func Infof(format string, args ...any) {
if defaultLoggerWrapper.sensitive {
args = make([]any, 0)
}

level.Info(defaultLoggerWrapper.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))
}

// Warnf logs a format line with level warn.
func Warnf(format string, args ...any) {
if defaultLoggerWrapper.sensitive {
args = make([]any, 0)
}

level.Warn(defaultLoggerWrapper.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))
}

// Debugf logs a format line with level debug.
func Debugf(format string, args ...any) {
if defaultLoggerWrapper.sensitive {
args = make([]any, 0)
}

level.Debug(defaultLoggerWrapper.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))
}

// Errorf logs a format line with level error.
// If err is not nil it adds only the msg string or vice-versa. Otherwise adds both.
func Errorf(err error, format string, args ...any) {
if defaultLoggerWrapper.sensitive {
args = make([]any, 0)
}

if err == nil {
level.Error(defaultLoggerWrapper.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))
return
Expand Down
2 changes: 2 additions & 0 deletions log/slog/slogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ type Logger interface {
Errorf(err error, format string, args ...any)
WithTraceId(ctx context.Context) Logger
WithFields(fields map[string]any) Logger
WithSensitive(bool) Logger
SetSensitive(val bool)
ydlamba marked this conversation as resolved.
Show resolved Hide resolved
}
Loading