add log verbose (#772)

pull/774/head v2.0.0-beta1
Tony Chen 4 years ago committed by GitHub
parent 5a8acec808
commit 0b93bef031
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 30
      log/verbose.go
  2. 11
      log/verbose_test.go

@ -0,0 +1,30 @@
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)}
}
// Print is equivalent to the Print function, guarded by the value of v.
func (v Verbose) Print(a ...interface{}) {
if v.enabled {
v.log.Print(a...)
}
}

@ -0,0 +1,11 @@
package log
import "testing"
func TestVerbose(t *testing.T) {
v := NewVerbose(DefaultLogger, 20)
v.V(10).Print("foo", "bar1")
v.V(20).Print("foo", "bar2")
v.V(30).Print("foo", "bar3")
}
Loading…
Cancel
Save