1
// Copyright 2012-2014 Canonical Ltd.
2
// Licensed under the LGPLv3, see LICENCE file for details.
12
"github.com/juju/loggo"
13
gc "gopkg.in/check.v1"
16
var logLocation = flag.Bool("loggo.location", false, "Also log the location of the loggo call")
18
// LoggingSuite redirects the juju logger to the test logger
19
// when embedded in a gocheck suite type.
20
type LoggingSuite struct{}
22
type gocheckWriter struct {
26
var logConfig = func() string {
27
if cfg := os.Getenv("TEST_LOGGING_CONFIG"); cfg != "" {
33
func (w *gocheckWriter) Write(entry loggo.Entry) {
34
filename := filepath.Base(entry.Filename)
37
message = fmt.Sprintf("%s %s %s:%d %s", entry.Level, entry.Module, filename, entry.Line, entry.Message)
39
message = fmt.Sprintf("%s %s %s", entry.Level, entry.Module, entry.Message)
41
// Magic calldepth value...
42
// The value says "how far up the call stack do we go to find the location".
43
// It is used to match the standard library log function, and isn't actually
45
w.c.Output(3, message)
48
func (s *LoggingSuite) SetUpSuite(c *gc.C) {
52
func (s *LoggingSuite) TearDownSuite(c *gc.C) {
56
func (s *LoggingSuite) SetUpTest(c *gc.C) {
60
func (s *LoggingSuite) TearDownTest(c *gc.C) {
63
type discardWriter struct{}
65
func (discardWriter) Write(entry loggo.Entry) {
68
func (s *LoggingSuite) setUp(c *gc.C) {
70
// Don't use the default writer for the test logging, which
71
// means we can still get logging output from tests that
72
// replace the default writer.
73
loggo.RegisterWriter(loggo.DefaultWriterName, discardWriter{})
74
loggo.RegisterWriter("loggingsuite", &gocheckWriter{c})
75
err := loggo.ConfigureLoggers(logConfig)
76
c.Assert(err, gc.IsNil)
79
// LoggingCleanupSuite is defined for backward compatibility.
80
// Do not use this suite in new tests.
81
type LoggingCleanupSuite struct {
86
func (s *LoggingCleanupSuite) SetUpSuite(c *gc.C) {
87
s.CleanupSuite.SetUpSuite(c)
88
s.LoggingSuite.SetUpSuite(c)
91
func (s *LoggingCleanupSuite) TearDownSuite(c *gc.C) {
92
s.LoggingSuite.TearDownSuite(c)
93
s.CleanupSuite.TearDownSuite(c)
96
func (s *LoggingCleanupSuite) SetUpTest(c *gc.C) {
97
s.CleanupSuite.SetUpTest(c)
98
s.LoggingSuite.SetUpTest(c)
101
func (s *LoggingCleanupSuite) TearDownTest(c *gc.C) {
102
s.CleanupSuite.TearDownTest(c)
103
s.LoggingSuite.TearDownTest(c)