diff --git a/README.md b/README.md index b9df0c5..a919a92 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/level.go b/level.go index 077c0e2..3b16808 100644 --- a/level.go +++ b/level.go @@ -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 diff --git a/logger.go b/logger.go index 95e5102..b658844 100644 --- a/logger.go +++ b/logger.go @@ -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 diff --git a/logger_test.go b/logger_test.go index b47e4ef..538ba5d 100644 --- a/logger_test.go +++ b/logger_test.go @@ -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)