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
=== modified file 'logger/logger.go'
--- logger/logger.go 2014-04-11 23:17:40 +0000
+++ logger/logger.go 2015-01-26 21:01:48 +0000
@@ -50,7 +50,8 @@
50}50}
5151
52const (52const (
53 lError = iota53 calldepthBase = 3
54 lError = iota
54 lInfo55 lInfo
55 lDebug56 lDebug
56)57)
@@ -81,8 +82,12 @@
81// level. The level can be, in order: "error", "info", "debug". It takes an82// level. The level can be, in order: "error", "info", "debug". It takes an
82// io.Writer.83// io.Writer.
83func NewSimpleLogger(w io.Writer, level string) Logger {84func NewSimpleLogger(w io.Writer, level string) Logger {
85 flags := log.Ldate | log.Ltime | log.Lmicroseconds
86 if levelToNLevel[level] == lDebug {
87 flags = flags | log.Lshortfile
88 }
84 return NewSimpleLoggerFromMinimalLogger(89 return NewSimpleLoggerFromMinimalLogger(
85 log.New(w, "", log.Ldate|log.Ltime|log.Lmicroseconds),90 log.New(w, "", flags),
86 level,91 level,
87 )92 )
88}93}
@@ -92,13 +97,13 @@
92}97}
9398
94func (lg *simpleLogger) Errorf(format string, v ...interface{}) {99func (lg *simpleLogger) Errorf(format string, v ...interface{}) {
95 lg.outputFunc(2, fmt.Sprintf("ERROR "+format, v...))100 lg.outputFunc(calldepthBase, fmt.Sprintf("ERROR "+format, v...))
96}101}
97102
98var osExit = os.Exit // for testing103var osExit = os.Exit // for testing
99104
100func (lg *simpleLogger) Fatalf(format string, v ...interface{}) {105func (lg *simpleLogger) Fatalf(format string, v ...interface{}) {
101 lg.outputFunc(2, fmt.Sprintf("ERROR "+format, v...))106 lg.outputFunc(calldepthBase, fmt.Sprintf("ERROR "+format, v...))
102 osExit(1)107 osExit(1)
103}108}
104109
@@ -107,18 +112,18 @@
107 stack := make([]byte, 8*1024) // Stack writes less but doesn't fail112 stack := make([]byte, 8*1024) // Stack writes less but doesn't fail
108 stackWritten := runtime.Stack(stack, false)113 stackWritten := runtime.Stack(stack, false)
109 stack = stack[:stackWritten]114 stack = stack[:stackWritten]
110 lg.outputFunc(2, fmt.Sprintf("ERROR(PANIC) %s:\n%s", msg, stack))115 lg.outputFunc(calldepthBase, fmt.Sprintf("ERROR(PANIC) %s:\n%s", msg, stack))
111}116}
112117
113func (lg *simpleLogger) Infof(format string, v ...interface{}) {118func (lg *simpleLogger) Infof(format string, v ...interface{}) {
114 if lg.nlevel >= lInfo {119 if lg.nlevel >= lInfo {
115 lg.outputFunc(2, fmt.Sprintf("INFO "+format, v...))120 lg.outputFunc(calldepthBase, fmt.Sprintf("INFO "+format, v...))
116 }121 }
117}122}
118123
119func (lg *simpleLogger) Debugf(format string, v ...interface{}) {124func (lg *simpleLogger) Debugf(format string, v ...interface{}) {
120 if lg.nlevel >= lDebug {125 if lg.nlevel >= lDebug {
121 lg.outputFunc(2, fmt.Sprintf("DEBUG "+format, v...))126 lg.outputFunc(calldepthBase, fmt.Sprintf("DEBUG "+format, v...))
122 }127 }
123}128}
124129
125130
=== modified file 'logger/logger_test.go'
--- logger/logger_test.go 2014-04-11 19:05:35 +0000
+++ logger/logger_test.go 2015-01-26 21:01:48 +0000
@@ -163,3 +163,14 @@
163 checkError(`{"lvl": 1}`, "lvl:.*type string")163 checkError(`{"lvl": 1}`, "lvl:.*type string")
164 checkError(`{"lvl": "foo"}`, "lvl: not a log level: foo")164 checkError(`{"lvl": "foo"}`, "lvl: not a log level: foo")
165}165}
166
167func (s *loggerSuite) TestLogLineNo(c *C) {
168 buf := &bytes.Buffer{}
169 logger := NewSimpleLogger(buf, "debug")
170 logger.Output(1, "foobaz")
171 c.Check(buf.String(), Matches, ".* .* logger_test.go:[0-9]+: foobaz\n")
172
173 logger = NewSimpleLogger(buf, "error")
174 logger.Output(1, "foobaz")
175 c.Check(buf.String(), Matches, ".* .* logger_test.go:[0-9]+: foobaz\n"+".* .* foobaz\n")
176}

Subscribers

People subscribed via source and target branches