Merge lp:~noise/ubuntu-push/log-line-nums into lp:ubuntu-push/automatic

Proposed by Bret Barker
Status: Merged
Merged at revision: 363
Proposed branch: lp:~noise/ubuntu-push/log-line-nums
Merge into: lp:ubuntu-push/automatic
Diff against target: 84 lines (+23/-7)
2 files modified
logger/logger.go (+12/-7)
logger/logger_test.go (+11/-0)
To merge this branch: bzr merge lp:~noise/ubuntu-push/log-line-nums
Reviewer Review Type Date Requested Status
Samuele Pedroni Needs Fixing
Review via email: mp+247651@code.launchpad.net

Commit message

log line nums, enabled when logLevel = debug

To post a comment you must log in.
Revision history for this message
Samuele Pedroni (pedronis) wrote :

it breaks the acceptance tests (make acceptance) that do some log parsing, the parsing should be teached to ignore the file name

smaller note:

 buf.Reset()
 logger = NewSimpleLogger(buf, "error")

could be used to simplify the second match in the test

review: Needs Fixing

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'logger/logger.go'
2--- logger/logger.go 2014-04-11 23:17:40 +0000
3+++ logger/logger.go 2015-01-26 21:01:48 +0000
4@@ -50,7 +50,8 @@
5 }
6
7 const (
8- lError = iota
9+ calldepthBase = 3
10+ lError = iota
11 lInfo
12 lDebug
13 )
14@@ -81,8 +82,12 @@
15 // level. The level can be, in order: "error", "info", "debug". It takes an
16 // io.Writer.
17 func NewSimpleLogger(w io.Writer, level string) Logger {
18+ flags := log.Ldate | log.Ltime | log.Lmicroseconds
19+ if levelToNLevel[level] == lDebug {
20+ flags = flags | log.Lshortfile
21+ }
22 return NewSimpleLoggerFromMinimalLogger(
23- log.New(w, "", log.Ldate|log.Ltime|log.Lmicroseconds),
24+ log.New(w, "", flags),
25 level,
26 )
27 }
28@@ -92,13 +97,13 @@
29 }
30
31 func (lg *simpleLogger) Errorf(format string, v ...interface{}) {
32- lg.outputFunc(2, fmt.Sprintf("ERROR "+format, v...))
33+ lg.outputFunc(calldepthBase, fmt.Sprintf("ERROR "+format, v...))
34 }
35
36 var osExit = os.Exit // for testing
37
38 func (lg *simpleLogger) Fatalf(format string, v ...interface{}) {
39- lg.outputFunc(2, fmt.Sprintf("ERROR "+format, v...))
40+ lg.outputFunc(calldepthBase, fmt.Sprintf("ERROR "+format, v...))
41 osExit(1)
42 }
43
44@@ -107,18 +112,18 @@
45 stack := make([]byte, 8*1024) // Stack writes less but doesn't fail
46 stackWritten := runtime.Stack(stack, false)
47 stack = stack[:stackWritten]
48- lg.outputFunc(2, fmt.Sprintf("ERROR(PANIC) %s:\n%s", msg, stack))
49+ lg.outputFunc(calldepthBase, fmt.Sprintf("ERROR(PANIC) %s:\n%s", msg, stack))
50 }
51
52 func (lg *simpleLogger) Infof(format string, v ...interface{}) {
53 if lg.nlevel >= lInfo {
54- lg.outputFunc(2, fmt.Sprintf("INFO "+format, v...))
55+ lg.outputFunc(calldepthBase, fmt.Sprintf("INFO "+format, v...))
56 }
57 }
58
59 func (lg *simpleLogger) Debugf(format string, v ...interface{}) {
60 if lg.nlevel >= lDebug {
61- lg.outputFunc(2, fmt.Sprintf("DEBUG "+format, v...))
62+ lg.outputFunc(calldepthBase, fmt.Sprintf("DEBUG "+format, v...))
63 }
64 }
65
66
67=== modified file 'logger/logger_test.go'
68--- logger/logger_test.go 2014-04-11 19:05:35 +0000
69+++ logger/logger_test.go 2015-01-26 21:01:48 +0000
70@@ -163,3 +163,14 @@
71 checkError(`{"lvl": 1}`, "lvl:.*type string")
72 checkError(`{"lvl": "foo"}`, "lvl: not a log level: foo")
73 }
74+
75+func (s *loggerSuite) TestLogLineNo(c *C) {
76+ buf := &bytes.Buffer{}
77+ logger := NewSimpleLogger(buf, "debug")
78+ logger.Output(1, "foobaz")
79+ c.Check(buf.String(), Matches, ".* .* logger_test.go:[0-9]+: foobaz\n")
80+
81+ logger = NewSimpleLogger(buf, "error")
82+ logger.Output(1, "foobaz")
83+ c.Check(buf.String(), Matches, ".* .* logger_test.go:[0-9]+: foobaz\n"+".* .* foobaz\n")
84+}

Subscribers

People subscribed via source and target branches