Skip to content

Commit d325c22

Browse files
authored
Merge pull request #107 from howbazaar/update-loggo
Update to use the latest loggo. Minor changes to deal with changes in the loggo functions. (Review request: http://reviews.vapour.ws/r/5350/)
2 parents c20a6ce + 290880c commit d325c22

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

checkers/log.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (s SimpleMessages) GoString() string {
3131
return fmt.Sprintf("SimpleMessages{\n%s\n}", strings.Join(out, "\n"))
3232
}
3333

34-
func logToSimpleMessages(log []loggo.TestLogValues) SimpleMessages {
34+
func logToSimpleMessages(log []loggo.Entry) SimpleMessages {
3535
out := make(SimpleMessages, len(log))
3636
for i, val := range log {
3737
out[i].Level = val.Level
@@ -47,10 +47,10 @@ type logMatches struct {
4747
func (checker *logMatches) Check(params []interface{}, _ []string) (result bool, error string) {
4848
var obtained SimpleMessages
4949
switch params[0].(type) {
50-
case []loggo.TestLogValues:
51-
obtained = logToSimpleMessages(params[0].([]loggo.TestLogValues))
50+
case []loggo.Entry:
51+
obtained = logToSimpleMessages(params[0].([]loggo.Entry))
5252
default:
53-
return false, "Obtained value must be of type []loggo.TestLogValues or SimpleMessage"
53+
return false, "Obtained value must be of type []loggo.Entry or SimpleMessage"
5454
}
5555

5656
var expected SimpleMessages

checkers/log_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type LogMatchesSuite struct{}
4343
var _ = gc.Suite(&LogMatchesSuite{})
4444

4545
func (s *LogMatchesSuite) TestMatchSimpleMessage(c *gc.C) {
46-
log := []loggo.TestLogValues{
46+
log := []loggo.Entry{
4747
{Level: loggo.INFO, Message: "foo bar"},
4848
{Level: loggo.INFO, Message: "12345"},
4949
}
@@ -68,7 +68,7 @@ func (s *LogMatchesSuite) TestMatchSimpleMessage(c *gc.C) {
6868
}
6969

7070
func (s *LogMatchesSuite) TestMatchSimpleMessages(c *gc.C) {
71-
log := []loggo.TestLogValues{
71+
log := []loggo.Entry{
7272
{Level: loggo.INFO, Message: "foo bar"},
7373
{Level: loggo.INFO, Message: "12345"},
7474
}
@@ -93,7 +93,7 @@ func (s *LogMatchesSuite) TestMatchSimpleMessages(c *gc.C) {
9393
}
9494

9595
func (s *LogMatchesSuite) TestMatchStrings(c *gc.C) {
96-
log := []loggo.TestLogValues{
96+
log := []loggo.Entry{
9797
{Level: loggo.INFO, Message: "foo bar"},
9898
{Level: loggo.INFO, Message: "12345"},
9999
}
@@ -103,7 +103,7 @@ func (s *LogMatchesSuite) TestMatchStrings(c *gc.C) {
103103
}
104104

105105
func (s *LogMatchesSuite) TestMatchInexact(c *gc.C) {
106-
log := []loggo.TestLogValues{
106+
log := []loggo.Entry{
107107
{Level: loggo.INFO, Message: "foo bar"},
108108
{Level: loggo.INFO, Message: "baz"},
109109
{Level: loggo.DEBUG, Message: "12345"},
@@ -169,11 +169,11 @@ func (s *LogMatchesSuite) TestLogMatchesOnlyAcceptsSliceTestLogValues(c *gc.C) {
169169
expected := jc.SimpleMessages{}
170170
result, err := jc.LogMatches.Check([]interface{}{obtained, expected}, nil)
171171
c.Assert(result, gc.Equals, false)
172-
c.Assert(err, gc.Equals, "Obtained value must be of type []loggo.TestLogValues or SimpleMessage")
172+
c.Assert(err, gc.Equals, "Obtained value must be of type []loggo.Entry or SimpleMessage")
173173
}
174174

175175
func (s *LogMatchesSuite) TestLogMatchesOnlyAcceptsStringOrSimpleMessages(c *gc.C) {
176-
obtained := []loggo.TestLogValues{
176+
obtained := []loggo.Entry{
177177
{Level: loggo.INFO, Message: "foo bar"},
178178
{Level: loggo.INFO, Message: "baz"},
179179
{Level: loggo.DEBUG, Message: "12345"},
@@ -185,7 +185,7 @@ func (s *LogMatchesSuite) TestLogMatchesOnlyAcceptsStringOrSimpleMessages(c *gc.
185185
}
186186

187187
func (s *LogMatchesSuite) TestLogMatchesFailsOnInvalidRegex(c *gc.C) {
188-
var obtained interface{} = []loggo.TestLogValues{{Level: loggo.INFO, Message: "foo bar"}}
188+
var obtained interface{} = []loggo.Entry{{Level: loggo.INFO, Message: "foo bar"}}
189189
var expected interface{} = []string{"[]foo"}
190190

191191
result, err := jc.LogMatches.Check([]interface{}{obtained, expected}, nil /* unused */)

log.go

+21-11
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
package testing
55

66
import (
7+
"flag"
78
"fmt"
89
"os"
9-
"time"
10+
"path/filepath"
1011

1112
"github.com/juju/loggo"
1213
gc "gopkg.in/check.v1"
1314
)
1415

16+
var logLocation = flag.Bool("loggo.location", false, "Also log the location of the loggo call")
17+
1518
// LoggingSuite redirects the juju logger to the test logger
1619
// when embedded in a gocheck suite type.
1720
type LoggingSuite struct{}
@@ -27,19 +30,27 @@ var logConfig = func() string {
2730
return "DEBUG"
2831
}()
2932

30-
func (w *gocheckWriter) Write(level loggo.Level, module, filename string, line int, timestamp time.Time, message string) {
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+
}
3141
// Magic calldepth value...
32-
// TODO (frankban) Document why we are using this magic value.
33-
w.c.Output(3, fmt.Sprintf("%s %s %s", level, module, message))
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)
3446
}
3547

3648
func (s *LoggingSuite) SetUpSuite(c *gc.C) {
3749
s.setUp(c)
3850
}
3951

4052
func (s *LoggingSuite) TearDownSuite(c *gc.C) {
41-
loggo.ResetLoggers()
42-
loggo.ResetWriters()
53+
loggo.ResetLogging()
4354
}
4455

4556
func (s *LoggingSuite) SetUpTest(c *gc.C) {
@@ -51,17 +62,16 @@ func (s *LoggingSuite) TearDownTest(c *gc.C) {
5162

5263
type discardWriter struct{}
5364

54-
func (discardWriter) Write(level loggo.Level, name, filename string, line int, timestamp time.Time, message string) {
65+
func (discardWriter) Write(entry loggo.Entry) {
5566
}
5667

5768
func (s *LoggingSuite) setUp(c *gc.C) {
58-
loggo.ResetWriters()
69+
loggo.ResetLogging()
5970
// Don't use the default writer for the test logging, which
6071
// means we can still get logging output from tests that
6172
// replace the default writer.
62-
loggo.ReplaceDefaultWriter(discardWriter{})
63-
loggo.RegisterWriter("loggingsuite", &gocheckWriter{c}, loggo.TRACE)
64-
loggo.ResetLoggers()
73+
loggo.RegisterWriter(loggo.DefaultWriterName, discardWriter{})
74+
loggo.RegisterWriter("loggingsuite", &gocheckWriter{c})
6575
err := loggo.ConfigureLoggers(logConfig)
6676
c.Assert(err, gc.IsNil)
6777
}

log_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (*logSuite) TestLog(c *gc.C) {
4040

4141
c.Assert(c.GetTestLog(), gc.Matches,
4242
".*DEBUG test message 1\n"+
43-
".*TRACE juju message 3\n",
43+
".*TRACE juju message 3\n$",
4444
)
4545
c.Assert(logger.EffectiveLogLevel(), gc.Equals, loggo.WARNING)
4646
c.Assert(jujuLogger.EffectiveLogLevel(), gc.Equals, loggo.WARNING)

0 commit comments

Comments
 (0)