Skip to content

Commit

Permalink
Merge pull request #18 from ssgreg/feature/with-level
Browse files Browse the repository at this point in the history
add: WithLevel to stack additional level checker
  • Loading branch information
pamburus authored Dec 2, 2021
2 parents 89febc5 + ff9206d commit a9b25c7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![GoDoc](https://godoc.org/github.com/ssgreg/logf?status.svg)](https://godoc.org/github.com/ssgreg/logf)
[![Build Status](https://github.com/ssgreg/logf/actions/workflows/go.yml/badge.svg)](https://github.com/ssgreg/logf/actions/workflows/go.yml)
[![Go Report Status](https://goreportcard.com/badge/github.com/ssgreg/logf)](https://goreportcard.com/report/github.com/ssgreg/logf)
[![Coverage Status](https://coveralls.io/repos/github/ssgreg/logf/badge.svg?branch=master&service=github)](https://coveralls.io/github/ssgreg/logf?branch=master)
[![Coverage Status](https://codecov.io/gh/ssgreg/logf/branch/master/graph/badge.svg)](https://codecov.io/gh/ssgreg/logf)

Faster-than-light, asynchronous, structured logger in Go with zero allocation count.

Expand Down
2 changes: 1 addition & 1 deletion level.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Level int8
const (
// LevelError allows to log errors only.
LevelError Level = iota
// LevelWarning allows to log errors and warnings.
// LevelWarn allows to log errors and warnings.
LevelWarn
// LevelInfo is the default logging level. Allows to log errors, warnings and infos.
LevelInfo
Expand Down
12 changes: 11 additions & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,20 @@ func (l *Logger) AtLevel(lvl Level, fn func(LogFunc)) {
})
}

// WithLevel returns a new logger with the given additional level checker.
func (l *Logger) WithLevel(level LevelChecker) *Logger {
cc := l.clone()
cc.level = func(lvl Level) bool {
return level(lvl) && l.level(lvl)
}

return cc
}

// WithName returns a new Logger adding the given name to the calling one.
// Name separator is a period.
//
// Loggers has no name by default.
// Loggers have no name by default.
func (l *Logger) WithName(n string) *Logger {
if n == "" {
return l
Expand Down
17 changes: 17 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ func TestLoggerName(t *testing.T) {
assert.Equal(t, "1.2", w.Entry.LoggerName)
}

func TestLoggerWithLevel(t *testing.T) {
w := &testEntryWriter{}

// Set a name for the logger.
logger := NewLogger(LevelInfo, w).WithLevel(
func(lvl Level) bool {
return lvl == LevelError
},
)

logger.Info("")
assert.Nil(t, w.Entry)

logger.Error("")
assert.NotNil(t, w.Entry)
}

func TestLoggerAtLevel(t *testing.T) {
w := &testEntryWriter{}
logger := NewLogger(LevelError, w)
Expand Down

0 comments on commit a9b25c7

Please sign in to comment.