Skip to content

Task/augment logging #98

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

Merged
merged 2 commits into from
Feb 20, 2025
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
1 change: 1 addition & 0 deletions logging/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type LoggerInterface interface {
Verbosef(fmt string, msg ...interface{})

WithContext(ctx context.Context) LoggerInterface
AugmentFromContext(ctx context.Context, values ...string) (LoggerInterface, context.Context)
}

// ParamsFn is a function that returns a slice of interface{}
Expand Down
9 changes: 9 additions & 0 deletions logging/levels.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ func (l *LevelFilteredLoggerWrapper) WithContext(ctx context.Context) LoggerInte
}
}

// AugmentFromContext
func (l *LevelFilteredLoggerWrapper) AugmentFromContext(ctx context.Context, values ...string) (LoggerInterface, context.Context) {
extLogger, ctx := l.delegate.AugmentFromContext(ctx, values...)
return &LevelFilteredLoggerWrapper{
delegate: extLogger,
level: l.level,
}, ctx
}

var _ LoggerInterface = (*LevelFilteredLoggerWrapper)(nil)

var levels map[string]int = map[string]int{
Expand Down
3 changes: 3 additions & 0 deletions logging/levels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ func (l *mockedLogger) Verbose(msg ...interface{}) { l.msgs["Verbose"
func (l *mockedLogger) WithContext(ctx context.Context) LoggerInterface {
panic("unimplemented")
}
func (l *mockedLogger) AugmentFromContext(ctx context.Context, values ...string) (LoggerInterface, context.Context) {
panic("unimplemented")
}

func writelog(logger *ExtendedLevelFilteredLoggerWrapper) {
logger.ErrorFn("hello %s", func() []interface{} { return []interface{}{"world"} })
Expand Down
15 changes: 15 additions & 0 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ func (l *Logger) WithContext(ctx context.Context) LoggerInterface {
}
}

// AugmentFromContext
func (l *Logger) AugmentFromContext(ctx context.Context, values ...string) (LoggerInterface, context.Context) {
if len(values)%2 == 1 {
return nil, nil
}
newContextData := NewContext()
for i := 0; i < len(values); i += 2 {
newContextData = newContextData.WithTag(values[i], values[i+1])
}
contextData := Merge(l.contextData, newContextData)
ctx = context.WithValue(ctx, ContextKey{}, contextData)

return l.WithContext(ctx), ctx
}

func normalizeOptions(options *LoggerOptions) *LoggerOptions {
var toRet *LoggerOptions
if options == nil {
Expand Down
10 changes: 10 additions & 0 deletions logging/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"log"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestLoggerContext(t *testing.T) {
Expand All @@ -14,6 +16,7 @@ func TestLoggerContext(t *testing.T) {
mW := &MockWriter{}
mW.On("Write", []byte(fmt.Sprintf("DEBUG - %s test\n", asFormat))).Once().Return(0, nil)
mW.On("Write", []byte(fmt.Sprintf("DEBUG - %s [txID=tx] test\n", asFormat))).Once().Return(0, nil)
mW.On("Write", []byte(fmt.Sprintf("DEBUG - %s [orgID=org, txID=tx, userID=user] test\n", asFormat))).Once().Return(0, nil)
logger := NewLogger(&LoggerOptions{
StandardLoggerFlags: log.Ldate,
LogLevel: LevelVerbose,
Expand All @@ -30,6 +33,13 @@ func TestLoggerContext(t *testing.T) {
bg2 := context.WithValue(bg, ContextKey{}, info)
loggerWithContext := logger.WithContext(bg2)
loggerWithContext.Debug("test")

logger, ctx := loggerWithContext.AugmentFromContext(bg2, "orgID", "org", "userID", "user")
logger.Debug("test")
ctxData := ctx.Value(ContextKey{}).(*ContextData)
assert.Equal(t, "org", ctxData.Get("orgID"))
assert.Equal(t, "tx", ctxData.Get("txID"))
assert.Equal(t, "user", ctxData.Get("userID"))
}

func TestLoggerWrapper(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions logging/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ func (l *MockLogger) WithContext(ctx context.Context) LoggerInterface {
return args.Get(0).(LoggerInterface)
}

// AugmentFromContext implements logging.LoggerInterface.
func (l *MockLogger) AugmentFromContext(ctx context.Context, values ...string) (LoggerInterface, context.Context) {
args := l.Called(ctx, values)
return args.Get(0).(LoggerInterface), args.Get(1).(context.Context)
}

type MockWriter struct {
mock.Mock
}
Expand Down