Skip to content

Commit

Permalink
Added log level support to SimpleLogger (#426)
Browse files Browse the repository at this point in the history
Implement Log levels.
Can be used as the default logger.
Default log level can be later specified via configuration, or
environment variable.
I had been using the library and found out that I needed to define a
custom logger to suppress logs, to be specific
I was using the `dbfsAPI` to upload files, and I was getting bombarded
with logs with each `add_block` request.

## Changes
<!-- Summary of your changes that are easy to understand -->
Add new Implementation for the `Logger` interface which respects the log
level.
## Tests

added 2 new tests found in [level
-logger_test.go](logger/level_logger_test.go)


- [x] `make test` passing
- [x] `make fmt` applied
- [ ] relevant integration tests applied
  • Loading branch information
marwansalem authored Jun 14, 2023
1 parent 08a9083 commit 91595e3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
26 changes: 26 additions & 0 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,29 @@ func TestGetLogger(t *testing.T) {
logger = Get(ctx)
assert.Equal(t, logger, t2)
}

func TestWhenInfoLevelThenDebugDisabled(t *testing.T) {
t.Cleanup(func() {
DefaultLogger = &SimpleLogger{}
})

infoLevelLogger := &SimpleLogger{
Level: LevelInfo,
}
debugEnabled := infoLevelLogger.Enabled(context.Background(), LevelDebug)
assert.False(t, debugEnabled)
}

func TestWhenInfoLevelThenErrorEnabled(t *testing.T) {
infoLevelLogger := &SimpleLogger{
Level: LevelInfo,
}

errorEnabled := infoLevelLogger.Enabled(context.Background(), LevelError)
assert.True(t, errorEnabled)
}

func TestDefaultLevelInfo(t *testing.T) {
logger := &SimpleLogger{}
assert.EqualValues(t, LevelInfo, logger.Level)
}
31 changes: 24 additions & 7 deletions logger/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,45 @@ import (
"log"
)

type SimpleLogger struct{}
type SimpleLogger struct {
Level Level
}

func (l *SimpleLogger) Enabled(_ context.Context, level Level) bool {
return true
return level >= l.Level
}

func (l *SimpleLogger) Tracef(_ context.Context, format string, v ...any) {
func (l *SimpleLogger) Tracef(ctx context.Context, format string, v ...any) {
if !l.Enabled(ctx, LevelTrace) {
return
}
log.Printf("[TRACE] "+format, v...)
}

func (l *SimpleLogger) Debugf(_ context.Context, format string, v ...any) {
func (l *SimpleLogger) Debugf(ctx context.Context, format string, v ...any) {
if !l.Enabled(ctx, LevelDebug) {
return
}
log.Printf("[DEBUG] "+format, v...)
}

func (l *SimpleLogger) Infof(_ context.Context, format string, v ...any) {
func (l *SimpleLogger) Infof(ctx context.Context, format string, v ...any) {
if !l.Enabled(ctx, LevelInfo) {
return
}
log.Printf("[INFO] "+format, v...)
}

func (l *SimpleLogger) Warnf(_ context.Context, format string, v ...any) {
func (l *SimpleLogger) Warnf(ctx context.Context, format string, v ...any) {
if !l.Enabled(ctx, LevelWarn) {
return
}
log.Printf("[WARN] "+format, v...)
}

func (l *SimpleLogger) Errorf(_ context.Context, format string, v ...any) {
func (l *SimpleLogger) Errorf(ctx context.Context, format string, v ...any) {
if !l.Enabled(ctx, LevelError) {
return
}
log.Printf("[ERROR] "+format, v...)
}

0 comments on commit 91595e3

Please sign in to comment.