launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/testing/testbase/log.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testbase
     5  
     6  import (
     7  	"fmt"
     8  	"time"
     9  
    10  	"github.com/loggo/loggo"
    11  	gc "launchpad.net/gocheck"
    12  )
    13  
    14  // LoggingSuite redirects the juju logger to the test logger
    15  // when embedded in a gocheck suite type.
    16  type LoggingSuite struct {
    17  	CleanupSuite
    18  }
    19  
    20  type gocheckWriter struct {
    21  	c *gc.C
    22  }
    23  
    24  func (w *gocheckWriter) Write(level loggo.Level, module, filename string, line int, timestamp time.Time, message string) {
    25  	// Magic calldepth value...
    26  	w.c.Output(3, fmt.Sprintf("%s %s %s", level, module, message))
    27  }
    28  
    29  func (t *LoggingSuite) SetUpSuite(c *gc.C) {
    30  	t.CleanupSuite.SetUpSuite(c)
    31  	t.setUp(c)
    32  	t.AddSuiteCleanup(func(*gc.C) {
    33  		loggo.ResetLoggers()
    34  		loggo.ResetWriters()
    35  	})
    36  }
    37  
    38  func (t *LoggingSuite) SetUpTest(c *gc.C) {
    39  	t.CleanupSuite.SetUpTest(c)
    40  	t.PatchEnvironment("JUJU_LOGGING_CONFIG", "")
    41  	t.setUp(c)
    42  }
    43  
    44  func (t *LoggingSuite) setUp(c *gc.C) {
    45  	loggo.ResetWriters()
    46  	loggo.ReplaceDefaultWriter(&gocheckWriter{c})
    47  	loggo.ResetLoggers()
    48  	loggo.GetLogger("juju").SetLogLevel(loggo.DEBUG)
    49  	loggo.GetLogger("unit").SetLogLevel(loggo.DEBUG)
    50  }