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

Slog Interface #61

Merged
merged 4 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
67 changes: 36 additions & 31 deletions log/slog/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@
)

type loggerWrapper struct {
logger log.Logger
logger log.Logger
}

const defaultMsgKey = "msg"
const defaultErrKey = "error"

var defaultLoggerWrapper *loggerWrapper

func init() {
logLevel := env.String("LOG_LEVEL", "info")
defaultLoggerWrapper = newloggerWrapper(logLevel)
}

// NewLogger returns a new instance of Logger.
func NewLogger() Logger {
return defaultLoggerWrapper
}

func newloggerWrapper(logLevel string) *loggerWrapper {
logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
logger = level.NewFilter(logger, levelFilter(logLevel))
Expand Down Expand Up @@ -52,27 +58,26 @@
}

func mapToSlice(m map[string]any) []any {
var args []any
for k, v := range m {
args = append(args, k, v)
}
return args
var args []any
for k, v := range m {
args = append(args, k, v)
}
return args
}


// Info logs a line with level info using the loggerWrapper instance.
func (l *loggerWrapper) Info(msg string, args ...any) {
level.Info(log.With(l.logger, defaultMsgKey, msg)).Log(args...)
level.Info(log.With(l.logger, defaultMsgKey, msg)).Log(args...)

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}

// Warn logs a line with level warn using the loggerWrapper instance.
func (l *loggerWrapper) Warn(msg string, args ...any) {
level.Warn(log.With(l.logger, defaultMsgKey, msg)).Log(args...)
level.Warn(log.With(l.logger, defaultMsgKey, msg)).Log(args...)

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}

// Debug logs a line with level debug using a loggerWrapper instance.
func (l *loggerWrapper) Debug(msg string, args ...any) {
level.Debug(log.With(defaultLoggerWrapper.logger, defaultMsgKey, msg)).Log(args...)
level.Debug(log.With(defaultLoggerWrapper.logger, defaultMsgKey, msg)).Log(args...)

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}

// Error logs a line with level error using a loggerWrapper instance.
Expand All @@ -88,22 +93,22 @@
return
}

level.Error(log.With(l.logger, defaultMsgKey, msg, defaultErrKey, err.Error())).Log(args...)
level.Error(log.With(l.logger, defaultMsgKey, msg, defaultErrKey, err.Error())).Log(args...)

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}

// Infof logs a format line with level info using the loggerWrapper instance.
func (l *loggerWrapper) Infof(format string, args ...any) {
level.Info(l.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))
level.Info(l.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}

// Warnf logs a format line with level warn using the loggerWrapper instance.
func (l *loggerWrapper) Warnf(format string, args ...any) {
level.Warn(l.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))
level.Warn(l.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}

// Debugf logs a format line with level debug using the loggerWrapper instance.
func (l *loggerWrapper) Debugf(format string, args ...any) {
level.Debug(l.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))
level.Debug(l.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}

// Errorf logs a format line with level error using a loggerWrapper instance.
Expand All @@ -119,23 +124,23 @@
return
}

level.Error(l.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...), defaultErrKey, err.Error())
level.Error(l.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...), defaultErrKey, err.Error())

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}

// WithTraceId returns a pointer to updated loggerWrapper with trace_id attached to the logger.
func (l *loggerWrapper) WithTraceId(ctx context.Context) *loggerWrapper {
traceId := instruments.ExtractTraceID(ctx)
logger := log.With(l.logger, "trace_id", traceId)
func (l *loggerWrapper) WithTraceId(ctx context.Context) Logger {
traceId := instruments.ExtractTraceID(ctx)
logger := log.With(l.logger, "trace_id", traceId)

return &loggerWrapper{
logger: logger,
}
}

// WithFields returns a pointer to updated loggerWrapper with custom fields attached to the logger.
func (l *loggerWrapper) WithFields(fields map[string]any) *loggerWrapper {
fieldArgs := mapToSlice(fields)
logger := log.With(l.logger, fieldArgs...)
func (l *loggerWrapper) WithFields(fields map[string]any) Logger {
fieldArgs := mapToSlice(fields)
logger := log.With(l.logger, fieldArgs...)

return &loggerWrapper{
logger: logger,
Expand All @@ -144,17 +149,17 @@

// Info logs a line with level Info.
func Info(msg string, args ...any) {
level.Info(log.With(defaultLoggerWrapper.logger, defaultMsgKey, msg)).Log(args...)
level.Info(log.With(defaultLoggerWrapper.logger, defaultMsgKey, msg)).Log(args...)

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}

// Warn logs a line with level warn.
func Warn(msg string, args ...any) {
level.Warn(log.With(defaultLoggerWrapper.logger, defaultMsgKey, msg)).Log(args...)
level.Warn(log.With(defaultLoggerWrapper.logger, defaultMsgKey, msg)).Log(args...)

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}

// Debug logs a line with level debug.
func Debug(msg string, args ...any) {
level.Debug(log.With(defaultLoggerWrapper.logger, defaultMsgKey, msg)).Log(args...)
level.Debug(log.With(defaultLoggerWrapper.logger, defaultMsgKey, msg)).Log(args...)

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}

// Error logs a line with level error.
Expand All @@ -170,22 +175,22 @@
return
}

level.Error(log.With(defaultLoggerWrapper.logger, defaultMsgKey, msg, defaultErrKey, err.Error())).Log(args...)
level.Error(log.With(defaultLoggerWrapper.logger, defaultMsgKey, msg, defaultErrKey, err.Error())).Log(args...)

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}

// Infof logs a format line with level info.
func Infof(format string, args ...any) {
level.Info(defaultLoggerWrapper.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))
level.Info(defaultLoggerWrapper.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}

// Warnf logs a format line with level warn.
func Warnf(format string, args ...any) {
level.Warn(defaultLoggerWrapper.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))
level.Warn(defaultLoggerWrapper.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}

// Debugf logs a format line with level debug.
func Debugf(format string, args ...any) {
level.Debug(defaultLoggerWrapper.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))
level.Debug(defaultLoggerWrapper.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...))

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}

// Errorf logs a format line with level error.
Expand All @@ -201,16 +206,16 @@
return
}

level.Error(defaultLoggerWrapper.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...), defaultErrKey, err.Error())
level.Error(defaultLoggerWrapper.logger).Log(defaultMsgKey, fmt.Sprintf(format, args...), defaultErrKey, err.Error())

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}

// WithFields returns WithFields using the defaultLoggerWrapper.
func WithFields(fields map[string]any) *loggerWrapper {
func WithFields(fields map[string]any) Logger {
return defaultLoggerWrapper.WithFields(fields)
}

// WithTraceId returns WithTraceId using the defaultLoggerWrapper.
func WithTraceId(ctx context.Context) *loggerWrapper {
func WithTraceId(ctx context.Context) Logger {
return defaultLoggerWrapper.WithTraceId(ctx)
}

Expand Down
17 changes: 17 additions & 0 deletions log/slog/slogger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package slog

import "context"

// Logger represents the methods supported by slog
type Logger interface {
Info(msg string, args ...any)
Warn(msg string, args ...any)
Debug(msg string, args ...any)
Error(err error, msg string, args ...any)
Infof(format string, args ...any)
Warnf(format string, args ...any)
Debugf(format string, args ...any)
Errorf(err error, format string, args ...any)
WithTraceId(ctx context.Context) Logger
WithFields(fields map[string]any) Logger
}
Loading