forked from juju/testing
/
log.go
75 lines (59 loc) · 1.65 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2012-2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package testing
import (
"fmt"
"time"
"github.com/juju/loggo"
gc "launchpad.net/gocheck"
)
// LoggingSuite redirects the juju logger to the test logger
// when embedded in a gocheck suite type.
type LoggingSuite struct{}
type gocheckWriter struct {
c *gc.C
}
func (w *gocheckWriter) Write(level loggo.Level, module, filename string, line int, timestamp time.Time, message string) {
// Magic calldepth value...
// TODO (frankban) Document why we are using this magic value.
w.c.Output(3, fmt.Sprintf("%s %s %s", level, module, message))
}
func (s *LoggingSuite) SetUpSuite(c *gc.C) {
s.setUp(c)
}
func (s *LoggingSuite) TearDownSuite(c *gc.C) {
loggo.ResetLoggers()
loggo.ResetWriters()
}
func (s *LoggingSuite) SetUpTest(c *gc.C) {
s.setUp(c)
}
func (s *LoggingSuite) TearDownTest(c *gc.C) {
}
func (s *LoggingSuite) setUp(c *gc.C) {
loggo.ResetWriters()
loggo.ReplaceDefaultWriter(&gocheckWriter{c})
loggo.ResetLoggers()
}
// LoggingCleanupSuite is defined for backward compatibility.
// Do not use this suite in new tests.
type LoggingCleanupSuite struct {
LoggingSuite
CleanupSuite
}
func (s *LoggingCleanupSuite) SetUpSuite(c *gc.C) {
s.CleanupSuite.SetUpSuite(c)
s.LoggingSuite.SetUpSuite(c)
}
func (s *LoggingCleanupSuite) TearDownSuite(c *gc.C) {
s.LoggingSuite.TearDownSuite(c)
s.CleanupSuite.TearDownSuite(c)
}
func (s *LoggingCleanupSuite) SetUpTest(c *gc.C) {
s.CleanupSuite.SetUpTest(c)
s.LoggingSuite.SetUpTest(c)
}
func (s *LoggingCleanupSuite) TearDownTest(c *gc.C) {
s.CleanupSuite.TearDownTest(c)
s.LoggingSuite.TearDownTest(c)
}