Skip to content

Commit

Permalink
logger: allow logging HTTP traffic enabling all logs
Browse files Browse the repository at this point in the history
When using SNAPD_DEBUG_HTTP, it was also necessary to set SNAPD_DEBUG
for the HTTP traffic to be logged. So it was impossible to log the HTTP
traffic without logging everything. This commit changes that so that
setting the former logs the HTTP traffic and setting SNAPD_DEBUG
enables the debug logs in the rest of snapd.

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
  • Loading branch information
miguelpires authored and mvo5 committed Dec 13, 2022
1 parent 3d5b58b commit 0728dfe
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
4 changes: 2 additions & 2 deletions httputil/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ func (tr *LoggedTransport) RoundTrip(req *http.Request) (*http.Response, error)

if flags.debugRequest() {
buf, _ := httputil.DumpRequestOut(req, tr.body && flags.debugBody())
logger.Debugf("> %q", buf)
logger.NoGuardDebugf("> %q", buf)
}

rsp, err := tr.Transport.RoundTrip(req)

if err == nil && flags.debugResponse() {
buf, _ := httputil.DumpResponse(rsp, tr.body && flags.debugBody())
logger.Debugf("< %q", buf)
logger.NoGuardDebugf("< %q", buf)
}

return rsp, err
Expand Down
2 changes: 0 additions & 2 deletions httputil/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ type loggerSuite struct {
var _ = check.Suite(&loggerSuite{})

func (s *loggerSuite) TearDownTest(c *check.C) {
os.Unsetenv("SNAPD_DEBUG")
s.restoreLogger()
}

func (s *loggerSuite) SetUpTest(c *check.C) {
os.Setenv("SNAPD_DEBUG", "true")
s.logbuf = bytes.NewBuffer(nil)
s.logbuf, s.restoreLogger = logger.MockLogger()
}
Expand Down
26 changes: 23 additions & 3 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type Logger interface {
Notice(msg string)
// Debug is for messages that the user should be able to find if they're debugging something
Debug(msg string)
// NoGuardDebug is for messages that we always want to print (e.g., configurations
// were checked by the caller, etc)
NoGuardDebug(msg string)
}

const (
Expand All @@ -46,8 +49,9 @@ const (

type nullLogger struct{}

func (nullLogger) Notice(string) {}
func (nullLogger) Debug(string) {}
func (nullLogger) Notice(string) {}
func (nullLogger) Debug(string) {}
func (nullLogger) NoGuardDebug(string) {}

// NullLogger is a logger that does nothing
var NullLogger = nullLogger{}
Expand Down Expand Up @@ -88,6 +92,16 @@ func Debugf(format string, v ...interface{}) {
logger.Debug(msg)
}

// NoGuardDebugf records something in the debug log
func NoGuardDebugf(format string, v ...interface{}) {
msg := fmt.Sprintf(format, v...)

lock.Lock()
defer lock.Unlock()

logger.NoGuardDebug(msg)
}

// MockLogger replaces the exiting logger with a buffer and returns
// the log buffer and a restore function.
func MockLogger() (buf *bytes.Buffer, restore func()) {
Expand Down Expand Up @@ -129,7 +143,7 @@ type Log struct {
// Debug only prints if SNAPD_DEBUG is set
func (l *Log) Debug(msg string) {
if l.debug || osutil.GetenvBool("SNAPD_DEBUG") {
l.log.Output(3, "DEBUG: "+msg)
l.NoGuardDebug(msg)
}
}

Expand All @@ -138,6 +152,12 @@ func (l *Log) Notice(msg string) {
l.log.Output(3, msg)
}

// NoGuardDebug always prints the message, w/o gating it based on environment
// variables or other configurations.
func (l *Log) NoGuardDebug(msg string) {
l.log.Output(3, "DEBUG: "+msg)
}

// New creates a log.Logger using the given io.Writer and flag.
func New(w io.Writer, flag int) (Logger, error) {
logger := &Log{
Expand Down
13 changes: 13 additions & 0 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ func (s *LogSuite) TestWithLoggerLock(c *C) {
c.Check(called, Equals, true)
}

func (s *LogSuite) TestNoGuardDebug(c *C) {
debugValue, ok := os.LookupEnv("SNAPD_DEBUG")
if ok {
defer func() {
os.Setenv("SNAPD_DEBUG", debugValue)
}()
os.Unsetenv("SNAPD_DEBUG")
}

logger.NoGuardDebugf("xyzzy")
c.Check(s.logbuf.String(), testutil.Contains, `DEBUG: xyzzy`)
}

func (s *LogSuite) TestIntegrationDebugFromKernelCmdline(c *C) {
// must enable actually checking the command line, because by default the
// logger package will skip checking for the kernel command line parameter
Expand Down

0 comments on commit 0728dfe

Please sign in to comment.