Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/log: add Clear to stop debug logging #3372

Merged
merged 1 commit into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/dnsserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func NewServer(addr string, group []*Config) (*Server, error) {
if site.Debug {
s.debug = true
log.D.Set()
} else {
// When reloading we need to explicitly disable debug logging if it is now disabled.
s.debug = false
log.D.Clear()
}
// set the config per zone
s.zones[site.Zone] = site
Expand Down
19 changes: 13 additions & 6 deletions plugin/pkg/log/log.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Package log implements a small wrapper around the std lib log package.
// It implements log levels by prefixing the logs with the current time
// with in RFC3339Milli and [INFO], [DEBUG], [WARNING] or [ERROR].
// Debug logging is available and enabled if the *debug* plugin is used.
// Package log implements a small wrapper around the std lib log package. It
// implements log levels by prefixing the logs with [INFO], [DEBUG], [WARNING]
// or [ERROR]. Debug logging is available and enabled if the *debug* plugin is
// used.
//
// log.Info("this is some logging"), will log on the Info level.
//
Expand All @@ -25,14 +25,21 @@ type d struct {
sync.RWMutex
}

// Set sets d to true.
// Set enables debug logging.
func (d *d) Set() {
d.Lock()
d.on = true
d.Unlock()
}

// Value return the boolean value of d.
// Clear disables debug logging.
func (d *d) Clear() {
d.Lock()
d.on = false
d.Unlock()
}

// Value returns if debug logging is enabled.
func (d *d) Value() bool {
d.RLock()
b := d.on
Expand Down
7 changes: 7 additions & 0 deletions plugin/pkg/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ func TestDebug(t *testing.T) {
if x := f.String(); !strings.Contains(x, debug+"debug") {
t.Errorf("Expected debug log to be %s, got %s", debug+"debug", x)
}
f.Reset()

D.Clear()
Debug("debug")
if x := f.String(); x != "" {
t.Errorf("Expected no debug logs, got %s", x)
}
}

func TestDebugx(t *testing.T) {
Expand Down