From 637e6ccb67864ecaacbde549bfc8d51df2c1c16e Mon Sep 17 00:00:00 2001 From: Grigory Zubankov Date: Thu, 2 Dec 2021 11:30:03 +0300 Subject: [PATCH 1/3] add: WithLevel to stack additional level checker --- level.go | 2 +- logger.go | 14 +++++++++++++- logger_test.go | 17 +++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) 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..6393c0a 100644 --- a/logger.go +++ b/logger.go @@ -63,10 +63,22 @@ func (l *Logger) AtLevel(lvl Level, fn func(LogFunc)) { }) } +// WithLevel returns a new logger with the given additional level checker. +func (l *Logger) WithLevel(levelChecker LevelChecker) *Logger { + newLevel := levelChecker //levelChecker.LevelChecker() + + cc := l.clone() + cc.level = func(lvl Level) bool { + return newLevel(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) From 8d450369c0c07741aa91e2ab0acc5858716f9298 Mon Sep 17 00:00:00 2001 From: Grigory Zubankov Date: Thu, 2 Dec 2021 11:51:06 +0300 Subject: [PATCH 2/3] fix: simplify code in WithLevel --- logger.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/logger.go b/logger.go index 6393c0a..b658844 100644 --- a/logger.go +++ b/logger.go @@ -64,12 +64,10 @@ func (l *Logger) AtLevel(lvl Level, fn func(LogFunc)) { } // WithLevel returns a new logger with the given additional level checker. -func (l *Logger) WithLevel(levelChecker LevelChecker) *Logger { - newLevel := levelChecker //levelChecker.LevelChecker() - +func (l *Logger) WithLevel(level LevelChecker) *Logger { cc := l.clone() cc.level = func(lvl Level) bool { - return newLevel(lvl) && l.level(lvl) + return level(lvl) && l.level(lvl) } return cc From ff9206d847aea122490c0112baee0bceebc31555 Mon Sep 17 00:00:00 2001 From: Grigory Zubankov Date: Thu, 2 Dec 2021 11:59:56 +0300 Subject: [PATCH 3/3] fix: update codecov badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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.