pkg/log: ability for debug logs (#1689)
* pkg/log: ability for debug logs When the debug plugin is enabled all log.Debug calls will print to standard; if not there are a noop (almost). The log package wraps some standard log functions as well, so just replacing "log" with "plugin/pkg/log" should be enough to use this package. * docs * Add docs * lint * Test fallthrough to log pkg as well * simple package - up test coverage * add other log levels as well * update docs
This commit is contained in:
parent
51e1442bd9
commit
b4b65fbc18
4 changed files with 140 additions and 4 deletions
61
plugin/pkg/log/log_test.go
Normal file
61
plugin/pkg/log/log_test.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
package log
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
golog "log"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDebug(t *testing.T) {
|
||||
var f bytes.Buffer
|
||||
golog.SetOutput(&f)
|
||||
|
||||
// D == false
|
||||
Debug("debug")
|
||||
if x := f.String(); x != "" {
|
||||
t.Errorf("Expected no debug logs, got %s", x)
|
||||
}
|
||||
|
||||
D = true
|
||||
Debug("debug")
|
||||
if x := f.String(); !strings.Contains(x, debug+"debug") {
|
||||
t.Errorf("Expected debug log to be %s, got %s", debug+"debug", x)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDebugx(t *testing.T) {
|
||||
var f bytes.Buffer
|
||||
golog.SetOutput(&f)
|
||||
|
||||
D = true
|
||||
|
||||
Debugf("%s", "debug")
|
||||
if x := f.String(); !strings.Contains(x, debug+"debug") {
|
||||
t.Errorf("Expected debug log to be %s, got %s", debug+"debug", x)
|
||||
}
|
||||
|
||||
Debug("debug")
|
||||
if x := f.String(); !strings.Contains(x, debug+"debug") {
|
||||
t.Errorf("Expected debug log to be %s, got %s", debug+"debug", x)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLevels(t *testing.T) {
|
||||
var f bytes.Buffer
|
||||
const ts = "test"
|
||||
golog.SetOutput(&f)
|
||||
|
||||
Info(ts)
|
||||
if x := f.String(); !strings.Contains(x, info+ts) {
|
||||
t.Errorf("Expected log to be %s, got %s", info+ts, x)
|
||||
}
|
||||
Warning(ts)
|
||||
if x := f.String(); !strings.Contains(x, warning+ts) {
|
||||
t.Errorf("Expected log to be %s, got %s", warning+ts, x)
|
||||
}
|
||||
Error(ts)
|
||||
if x := f.String(); !strings.Contains(x, err+ts) {
|
||||
t.Errorf("Expected log to be %s, got %s", err+ts, x)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue