Merge lp:~jamesodhunt/snappy/log-filename+lineno-onlyh-for-loglevel-error-and-above into lp:~snappy-dev/snappy/snappy-moved-to-github

Proposed by James Hunt on 2015-03-27
Status: Merged
Approved by: Michael Vogt on 2015-03-30
Approved revision: 270
Merged at revision: 285
Proposed branch: lp:~jamesodhunt/snappy/log-filename+lineno-onlyh-for-loglevel-error-and-above
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Diff against target: 116 lines (+54/-19)
2 files modified
logger/logger.go (+5/-0)
logger/logger_test.go (+49/-19)
To merge this branch: bzr merge lp:~jamesodhunt/snappy/log-filename+lineno-onlyh-for-loglevel-error-and-above
Reviewer Review Type Date Requested Status
John Lenton 2015-03-27 Approve on 2015-03-30
Review via email: mp+254429@code.launchpad.net

Commit Message

Don't include filename+line number unless logging levels ERROR and above.

Description of the Change

Logging the filename+line number for every log entry is overkill unless the level is ERROR or higher:

* logger/logger.go: Format(): Don't include filename+line number unless
  logging levels ERROR and above.
* logger/logger_test.go:
  - Assert new log format behaviour.
  - TestWrite(): Now tests all log levels.
  - TestFormat(): Now tests all log levels.

To post a comment you must log in.
John Lenton (chipaca) wrote :

Nice.

Shame loggo doesn't give you a way to not compute line numbers when not needed (as it's relatively expensive).

review: Approve

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 2015-03-19 10:29:09 +0000
3+++ logger/logger.go 2015-03-27 16:54:17 +0000
4@@ -94,6 +94,11 @@
5 // Format handles how each log entry should appear.
6 // Note that the timestamp field is not used as syslog adds that for us.
7 func (l *LogWriter) Format(level loggo.Level, module, filename string, line int, timestamp time.Time, message string) string {
8+ if level < loggo.ERROR {
9+ // keep it relatively succinct for low priority messages
10+ return fmt.Sprintf("%s:%s:%s", level, module, message)
11+ }
12+
13 return fmt.Sprintf("%s:%s:%s:%d:%s", level, module, filename, line, message)
14 }
15
16
17=== modified file 'logger/logger_test.go'
18--- logger/logger_test.go 2015-03-19 11:08:04 +0000
19+++ logger/logger_test.go 2015-03-27 16:54:17 +0000
20@@ -27,6 +27,8 @@
21
22 var mockWriter *MockLogWriter
23
24+var loggoLevels = []string{"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"}
25+
26 func mockGetSyslog(priority syslog.Priority, tag string) (w logWriterInterface, err error) {
27 mockWriter = &MockLogWriter{}
28 return mockWriter, nil
29@@ -122,18 +124,41 @@
30 t := time.Now()
31 strTime := fmt.Sprintf("%s", t)
32
33- w.Write(loggo.DEBUG, "module", "filename", 1234, t, "a message")
34- lines := readLines()
35- c.Assert(len(lines), Equals, 1)
36-
37- c.Assert(sliceContainsString(lines, "module"), Equals, true)
38- c.Assert(sliceContainsString(lines, "filename"), Equals, true)
39- c.Assert(sliceContainsString(lines, "1234"), Equals, true)
40-
41- // We discard the timestamp as syslog adds that itself
42- c.Assert(sliceContainsString(lines, strTime), Equals, false)
43-
44- c.Assert(sliceContainsString(lines, "a message"), Equals, true)
45+ for _, l := range loggoLevels {
46+ level := stringToLogLevel(l)
47+
48+ w.Write(level, "module", "filename", 1234, t, "a message")
49+ lines := readLines()
50+
51+ if level < loggo.ERROR {
52+ c.Assert(len(lines), Equals, 1)
53+ } else {
54+ c.Assert(len(lines) > 1, Equals, true)
55+
56+ c.Assert(sliceContainsString(lines, "filename"), Equals, true)
57+ c.Assert(sliceContainsString(lines, "1234"), Equals, true)
58+ }
59+
60+ c.Assert(sliceContainsString(lines, "module"), Equals, true)
61+
62+ // We discard the timestamp as syslog adds that itself
63+ c.Assert(sliceContainsString(lines, strTime), Equals, false)
64+
65+ c.Assert(sliceContainsString(lines, "a message"), Equals, true)
66+ }
67+
68+}
69+
70+// Convert a loggo log level string representation into a real log
71+// level.
72+func stringToLogLevel(name string) loggo.Level {
73+ level, ok := loggo.ParseLevel(name)
74+
75+ if !ok {
76+ panic(fmt.Sprintf("unknown loggo level string: %q", name))
77+ }
78+
79+ return level
80 }
81
82 func (ts *LoggerTestSuite) TestFormat(c *C) {
83@@ -142,11 +167,17 @@
84 c.Assert(w, Not(IsNil))
85 c.Assert(w.systemLog, Not(IsNil))
86
87- out := w.Format(loggo.ERROR, "module", "filename", 1234, time.Now(), "a message")
88-
89- expected := fmt.Sprintf("%s:%s:%s:%d:%s", "ERROR", "module", "filename", 1234, "a message")
90-
91- c.Assert(out, Equals, expected)
92+ for _, l := range loggoLevels {
93+ level := stringToLogLevel(l)
94+
95+ if level < loggo.ERROR {
96+ out := w.Format(level, "module", "filename", 1234, time.Now(), "a message")
97+ c.Assert(out, Equals, fmt.Sprintf("%s:%s:%s", l, "module", "a message"))
98+ } else {
99+ out := w.Format(level, "module", "filename", 1234, time.Now(), "a message")
100+ c.Assert(out, Equals, fmt.Sprintf("%s:%s:%s:%d:%s", l, "module", "filename", 1234, "a message"))
101+ }
102+ }
103 }
104
105 func (ts *LoggerTestSuite) TestLogStackTrace(c *C) {
106@@ -223,9 +254,8 @@
107
108 func (ts *LoggerTestSuite) TestLogLevels(c *C) {
109 msg := "an error message"
110- levels := []string{"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"}
111
112- for _, level := range levels {
113+ for _, level := range loggoLevels {
114 ts.checkLogLevel(c, level, msg)
115 }
116 }

Subscribers

People subscribed via source and target branches