mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-05-11 05:55:12 +00:00
Verbose debug logging via the ConsolePrinter and printer package. (#404)
This: * adds a global -v flag for verbosity * refactors the "printer" package to have a DefaultPrinter and package functions that call it, similar to net/http's DefaultServeMux * adds printer tests * moves current users of Debugf to Printf * moves most users of the "log" package to use "printer" * demotes noticably noisy log messages to "Debugf", like "IGNORE"- and "NO_PURGE"-related messages
This commit is contained in:
committed by
Craig Peterson
parent
f58acabe9f
commit
06ee4d6fb1
47
pkg/printer/printer_test.go
Normal file
47
pkg/printer/printer_test.go
Normal file
@ -0,0 +1,47 @@
|
||||
package printer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestDefaultPrinter checks that the DefaultPrinter properly controls output from the package-level
|
||||
// Warnf/Printf/Debugf functions.
|
||||
func TestDefaultPrinter(t *testing.T) {
|
||||
old := DefaultPrinter
|
||||
defer func() {
|
||||
DefaultPrinter = old
|
||||
}()
|
||||
|
||||
output := &bytes.Buffer{}
|
||||
DefaultPrinter = &ConsolePrinter{
|
||||
Writer: output,
|
||||
Verbose: true,
|
||||
}
|
||||
|
||||
Warnf("warn\n")
|
||||
Printf("output\n")
|
||||
Debugf("debugging\n")
|
||||
assert.Equal(t, "WARNING: warn\noutput\ndebugging\n", output.String())
|
||||
}
|
||||
|
||||
func TestVerbose(t *testing.T) {
|
||||
output := &bytes.Buffer{}
|
||||
p := ConsolePrinter{
|
||||
Writer: output,
|
||||
Verbose: false,
|
||||
}
|
||||
|
||||
// Test that verbose output is suppressed.
|
||||
p.Warnf("a dire warning!\n")
|
||||
p.Printf("output\n")
|
||||
p.Debugf("debugging\n")
|
||||
assert.Equal(t, "WARNING: a dire warning!\noutput\n", output.String())
|
||||
|
||||
// Test that Verbose output can be dynamically enabled.
|
||||
p.Verbose = true
|
||||
p.Debugf("more debugging\n")
|
||||
assert.Equal(t, "WARNING: a dire warning!\noutput\nmore debugging\n", output.String())
|
||||
}
|
Reference in New Issue
Block a user