~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/testing/log.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2012-2014 Canonical Ltd.
 
2
// Licensed under the LGPLv3, see LICENCE file for details.
 
3
 
 
4
package testing
 
5
 
 
6
import (
 
7
        "flag"
 
8
        "fmt"
 
9
        "os"
 
10
        "path/filepath"
 
11
 
 
12
        "github.com/juju/loggo"
 
13
        gc "gopkg.in/check.v1"
 
14
)
 
15
 
 
16
var logLocation = flag.Bool("loggo.location", false, "Also log the location of the loggo call")
 
17
 
 
18
// LoggingSuite redirects the juju logger to the test logger
 
19
// when embedded in a gocheck suite type.
 
20
type LoggingSuite struct{}
 
21
 
 
22
type gocheckWriter struct {
 
23
        c *gc.C
 
24
}
 
25
 
 
26
var logConfig = func() string {
 
27
        if cfg := os.Getenv("TEST_LOGGING_CONFIG"); cfg != "" {
 
28
                return cfg
 
29
        }
 
30
        return "DEBUG"
 
31
}()
 
32
 
 
33
func (w *gocheckWriter) Write(entry loggo.Entry) {
 
34
        filename := filepath.Base(entry.Filename)
 
35
        var message string
 
36
        if *logLocation {
 
37
                message = fmt.Sprintf("%s %s %s:%d %s", entry.Level, entry.Module, filename, entry.Line, entry.Message)
 
38
        } else {
 
39
                message = fmt.Sprintf("%s %s %s", entry.Level, entry.Module, entry.Message)
 
40
        }
 
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
 
44
        // used by gocheck.
 
45
        w.c.Output(3, message)
 
46
}
 
47
 
 
48
func (s *LoggingSuite) SetUpSuite(c *gc.C) {
 
49
        s.setUp(c)
 
50
}
 
51
 
 
52
func (s *LoggingSuite) TearDownSuite(c *gc.C) {
 
53
        loggo.ResetLogging()
 
54
}
 
55
 
 
56
func (s *LoggingSuite) SetUpTest(c *gc.C) {
 
57
        s.setUp(c)
 
58
}
 
59
 
 
60
func (s *LoggingSuite) TearDownTest(c *gc.C) {
 
61
}
 
62
 
 
63
type discardWriter struct{}
 
64
 
 
65
func (discardWriter) Write(entry loggo.Entry) {
 
66
}
 
67
 
 
68
func (s *LoggingSuite) setUp(c *gc.C) {
 
69
        loggo.ResetLogging()
 
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)
 
77
}
 
78
 
 
79
// LoggingCleanupSuite is defined for backward compatibility.
 
80
// Do not use this suite in new tests.
 
81
type LoggingCleanupSuite struct {
 
82
        LoggingSuite
 
83
        CleanupSuite
 
84
}
 
85
 
 
86
func (s *LoggingCleanupSuite) SetUpSuite(c *gc.C) {
 
87
        s.CleanupSuite.SetUpSuite(c)
 
88
        s.LoggingSuite.SetUpSuite(c)
 
89
}
 
90
 
 
91
func (s *LoggingCleanupSuite) TearDownSuite(c *gc.C) {
 
92
        s.LoggingSuite.TearDownSuite(c)
 
93
        s.CleanupSuite.TearDownSuite(c)
 
94
}
 
95
 
 
96
func (s *LoggingCleanupSuite) SetUpTest(c *gc.C) {
 
97
        s.CleanupSuite.SetUpTest(c)
 
98
        s.LoggingSuite.SetUpTest(c)
 
99
}
 
100
 
 
101
func (s *LoggingCleanupSuite) TearDownTest(c *gc.C) {
 
102
        s.CleanupSuite.TearDownTest(c)
 
103
        s.LoggingSuite.TearDownTest(c)
 
104
}