diff --git a/log/verbose.go b/log/verbose.go new file mode 100644 index 000000000..f2f44e830 --- /dev/null +++ b/log/verbose.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...) + } +} diff --git a/log/verbose_test.go b/log/verbose_test.go new file mode 100644 index 000000000..06ac38159 --- /dev/null +++ b/log/verbose_test.go @@ -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") +}