Skip to content

Commit

Permalink
feat: new flag sensitive
Browse files Browse the repository at this point in the history
- lets you log messages without showing the values
  • Loading branch information
Subhrajit Prusty committed Jun 10, 2024
1 parent f5324c8 commit 3a28a75
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 5 deletions.
88 changes: 83 additions & 5 deletions log/slog/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
)

type loggerWrapper struct {
logger log.Logger
logger log.Logger
sensitive bool // if sensitive to true, dont log the values
}

const defaultMsgKey = "msg"
Expand All @@ -21,28 +22,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)
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 +76,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 {
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 +122,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 +187,51 @@ 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.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 +247,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
1 change: 1 addition & 0 deletions log/slog/slogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ type Logger interface {
Errorf(err error, format string, args ...any)
WithTraceId(ctx context.Context) Logger
WithFields(fields map[string]any) Logger
SetSensitive(val bool)
}

0 comments on commit 3a28a75

Please sign in to comment.