Skip to content

Commit 4791ba3

Browse files
committed
TUN-3194: Don't render log output when level is not enabled
1 parent cf1c9a3 commit 4791ba3

File tree

6 files changed

+74
-43
lines changed

6 files changed

+74
-43
lines changed

logger/formatter.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import (
88
"github.com/acmacalister/skittles"
99
)
1010

11-
// Level of logging
11+
// Level of logging, lower number means more verbose logging, higher more terse
1212
type Level int
1313

1414
const (
15-
// InfoLevel is for standard log messages
16-
InfoLevel Level = iota
17-
1815
// DebugLevel is for messages that are intended for purposes debugging only
19-
DebugLevel
16+
DebugLevel Level = iota
17+
18+
// InfoLevel is for standard log messages
19+
InfoLevel
2020

21-
// ErrorLevel is for error message to indicte something has gone wrong
21+
// ErrorLevel is for error message to indicate something has gone wrong
2222
ErrorLevel
2323

2424
// FatalLevel is for error message that log and kill the program with an os.exit(1)

logger/manager.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import "sync"
77
var SharedWriteManager = NewWriteManager()
88

99
type writeData struct {
10-
writeFunc func([]byte)
11-
data []byte
10+
target LogOutput
11+
data []byte
1212
}
1313

1414
// WriteManager is a logging service that handles managing multiple writing streams
@@ -31,9 +31,9 @@ func NewWriteManager() OutputManager {
3131
}
3232

3333
// Append adds a message to the writer runloop
34-
func (m *WriteManager) Append(data []byte, callback func([]byte)) {
34+
func (m *WriteManager) Append(data []byte, target LogOutput) {
3535
m.wg.Add(1)
36-
m.writeChan <- writeData{data: data, writeFunc: callback}
36+
m.writeChan <- writeData{data: data, target: target}
3737
}
3838

3939
// Shutdown stops the sync manager service
@@ -49,7 +49,7 @@ func (m *WriteManager) run() {
4949
select {
5050
case event, ok := <-m.writeChan:
5151
if ok {
52-
event.writeFunc(event.data)
52+
event.target.WriteLogLine(event.data)
5353
m.wg.Done()
5454
}
5555
case <-m.shutdown:

logger/manager_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ import (
66
"github.com/stretchr/testify/assert"
77
)
88

9+
type outputFunc func(b []byte)
10+
11+
func (f outputFunc) WriteLogLine(data []byte) {
12+
f(data)
13+
}
14+
915
func TestWriteManger(t *testing.T) {
1016
testData := []byte(string("hello Austin, how are you doing?"))
1117
waitChan := make(chan []byte)
1218
m := NewWriteManager()
13-
m.Append(testData, func(b []byte) {
19+
m.Append(testData, outputFunc(func(b []byte) {
1420
waitChan <- b
15-
})
21+
}))
1622
resp := <-waitChan
1723
assert.Equal(t, testData, resp)
1824
}

logger/mock_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func NewMockWriteManager() OutputManager {
1010
}
1111

1212
// Append is a mock stub
13-
func (m *MockWriteManager) Append(data []byte, callback func([]byte)) {
13+
func (m *MockWriteManager) Append(data []byte, target LogOutput) {
1414
}
1515

1616
// Shutdown is a mock stub

logger/output.go

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ import (
1010
// provided for testing
1111
var osExit = os.Exit
1212

13+
type LogOutput interface {
14+
WriteLogLine([]byte)
15+
}
16+
1317
// OutputManager is used to sync data of Output
1418
type OutputManager interface {
15-
Append([]byte, func([]byte))
19+
Append([]byte, LogOutput)
1620
Shutdown()
1721
}
1822

@@ -35,38 +39,66 @@ type sourceGroup struct {
3539
levelsSupported []Level
3640
}
3741

42+
func (s *sourceGroup) WriteLogLine(data []byte) {
43+
_, _ = s.writer.Write(data)
44+
}
45+
46+
func (s *sourceGroup) supportsLevel(l Level) bool {
47+
for _, level := range s.levelsSupported {
48+
if l == level {
49+
return true
50+
}
51+
}
52+
return false
53+
}
54+
3855
// OutputWriter is the standard logging implementation
3956
type OutputWriter struct {
40-
groups []sourceGroup
57+
groups []*sourceGroup
4158
syncWriter OutputManager
59+
minLevel Level
4260
}
4361

4462
// NewOutputWriter create a new logger
4563
func NewOutputWriter(syncWriter OutputManager) *OutputWriter {
4664
return &OutputWriter{
4765
syncWriter: syncWriter,
48-
groups: make([]sourceGroup, 0),
66+
groups: nil,
67+
minLevel: FatalLevel,
4968
}
5069
}
5170

5271
// Add a writer and formatter to output to
5372
func (s *OutputWriter) Add(writer io.Writer, formatter Formatter, levels ...Level) {
54-
s.groups = append(s.groups, sourceGroup{writer: writer, formatter: formatter, levelsSupported: levels})
73+
s.groups = append(s.groups, &sourceGroup{writer: writer, formatter: formatter, levelsSupported: levels})
74+
75+
// track most verbose (lowest) level we need to output
76+
for _, level := range levels {
77+
if level < s.minLevel {
78+
s.minLevel = level
79+
}
80+
}
5581
}
5682

5783
// Error writes an error to the logging sources
5884
func (s *OutputWriter) Error(message string) {
59-
s.output(ErrorLevel, message)
85+
if s.minLevel <= ErrorLevel {
86+
s.output(ErrorLevel, message)
87+
}
6088
}
6189

6290
// Info writes an info string to the logging sources
6391
func (s *OutputWriter) Info(message string) {
64-
s.output(InfoLevel, message)
92+
if s.minLevel <= InfoLevel {
93+
s.output(InfoLevel, message)
94+
}
6595
}
6696

6797
// Debug writes a debug string to the logging sources
6898
func (s *OutputWriter) Debug(message string) {
69-
s.output(DebugLevel, message)
99+
if s.minLevel <= DebugLevel {
100+
s.output(DebugLevel, message)
101+
}
70102
}
71103

72104
// Fatal writes a error string to the logging sources and runs does an os.exit()
@@ -78,17 +110,23 @@ func (s *OutputWriter) Fatal(message string) {
78110

79111
// Errorf writes a formatted error to the logging sources
80112
func (s *OutputWriter) Errorf(format string, args ...interface{}) {
81-
s.output(ErrorLevel, fmt.Sprintf(format, args...))
113+
if s.minLevel <= ErrorLevel {
114+
s.output(ErrorLevel, fmt.Sprintf(format, args...))
115+
}
82116
}
83117

84118
// Infof writes a formatted info statement to the logging sources
85119
func (s *OutputWriter) Infof(format string, args ...interface{}) {
86-
s.output(InfoLevel, fmt.Sprintf(format, args...))
120+
if s.minLevel <= InfoLevel {
121+
s.output(InfoLevel, fmt.Sprintf(format, args...))
122+
}
87123
}
88124

89125
// Debugf writes a formatted debug statement to the logging sources
90126
func (s *OutputWriter) Debugf(format string, args ...interface{}) {
91-
s.output(DebugLevel, fmt.Sprintf(format, args...))
127+
if s.minLevel <= DebugLevel {
128+
s.output(DebugLevel, fmt.Sprintf(format, args...))
129+
}
92130
}
93131

94132
// Fatalf writes a writes a formatted error statement and runs does an os.exit()
@@ -100,29 +138,14 @@ func (s *OutputWriter) Fatalf(format string, args ...interface{}) {
100138

101139
// output does the actual write to the sync manager
102140
func (s *OutputWriter) output(l Level, content string) {
141+
now := time.Now()
103142
for _, group := range s.groups {
104-
if isSupported(group, l) {
105-
logLine := fmt.Sprintf("%s%s\n", group.formatter.Timestamp(l, time.Now()),
143+
if group.supportsLevel(l) {
144+
logLine := fmt.Sprintf("%s%s\n", group.formatter.Timestamp(l, now),
106145
group.formatter.Content(l, content))
107-
s.append(group, []byte(logLine))
108-
}
109-
}
110-
}
111-
112-
func (s *OutputWriter) append(group sourceGroup, logLine []byte) {
113-
s.syncWriter.Append(logLine, func(b []byte) {
114-
group.writer.Write(b)
115-
})
116-
}
117-
118-
// isSupported checks if the log level is supported
119-
func isSupported(group sourceGroup, l Level) bool {
120-
for _, level := range group.levelsSupported {
121-
if l == level {
122-
return true
146+
s.syncWriter.Append([]byte(logLine), group)
123147
}
124148
}
125-
return false
126149
}
127150

128151
// Write implements io.Writer to support SetOutput of the log package

logger/output_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ func TestOutputWrite(t *testing.T) {
5555
logger := NewOutputWriter(m)
5656
logger.Add(&testBuffer, f, InfoLevel)
5757

58+
logger.Debugf("debug message not logged here")
59+
5860
testData := "hello Bob Bork, how are you doing?"
5961
logger.Info(testData)
6062
testTime := f.Timestamp(InfoLevel, time.Now())

0 commit comments

Comments
 (0)