log: add level printer (#918)
* add level printer * fix helper args * fix test * Update README.mdpull/920/head
parent
24b1ca6bc3
commit
d78eb3ee4b
@ -1,31 +0,0 @@ |
||||
package log |
||||
|
||||
// Verbose is a verbose type that implements Logger Print.
|
||||
type Verbose struct { |
||||
log Logger |
||||
level Level |
||||
enabled bool |
||||
} |
||||
|
||||
// NewVerbose new a verbose with level.
|
||||
func NewVerbose(log Logger, level Level) Verbose { |
||||
return Verbose{log: log, level: level} |
||||
} |
||||
|
||||
// Enabled will return true if this log level is enabled, guarded by the value of v.
|
||||
func (v Verbose) Enabled(level Level) bool { |
||||
return v.level <= level |
||||
} |
||||
|
||||
// V reports whether verbosity at the call site is at least the requested level.
|
||||
func (v Verbose) V(level Level) Verbose { |
||||
return Verbose{log: v.log, enabled: v.Enabled(level)} |
||||
} |
||||
|
||||
// Log is equivalent to the Print function, guarded by the value of v.
|
||||
func (v Verbose) Log(a ...interface{}) error { |
||||
if v.enabled { |
||||
return v.log.Log(a...) |
||||
} |
||||
return nil |
||||
} |
@ -1,12 +0,0 @@ |
||||
package log |
||||
|
||||
import "testing" |
||||
|
||||
func TestVerbose(t *testing.T) { |
||||
logger := With(DefaultLogger, "caller", DefaultCaller, "ts", DefaultTimestamp) |
||||
v := NewVerbose(logger, 20) |
||||
|
||||
v.V(10).Log("foo", "bar1") |
||||
v.V(20).Log("foo", "bar2") |
||||
v.V(30).Log("foo", "bar3") |
||||
} |
Loading…
Reference in new issue