Skip to content

Commit d490b39

Browse files
committed
add test and rework kv formatting
1 parent 9f6d0cc commit d490b39

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ func main() {
3333
## Implementation Details
3434

3535
This is a simple log adapter to log messages into a buffer.
36+
Useful for testing.

buflogr.go

+19-13
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
)
1111

1212
const (
13-
LevelError = "ERROR "
14-
LevelInfo = "INFO "
15-
LevelV = "V[%d] "
13+
LevelError = "ERROR"
14+
LevelInfo = "INFO"
15+
LevelV = "V[%d]"
1616
)
1717

1818
var (
@@ -70,14 +70,14 @@ func (l *bufLogger) Enabled(level int) bool {
7070
// Info implements logr.Logger.Info by writing the line to the internal buffer.
7171
func (l *bufLogger) Info(level int, msg string, kv ...interface{}) {
7272
if l.Enabled(level) {
73-
l.writeLine(l.levelString(level), msg, KVFormatter(kv...))
73+
l.writeLine(l.levelString(level), msg, kv...)
7474
}
7575
}
7676

7777
// Error implements logr.Logger.Error by prefixing the line with "ERROR" and
7878
// write it to the internal buffer.
7979
func (l *bufLogger) Error(err error, msg string, kv ...interface{}) {
80-
l.writeLine(LevelError, msg, KVFormatter(kv...))
80+
l.writeLine(LevelError, msg, kv...)
8181
}
8282

8383
// WithValues returns a new LogSink with additional key/value pairs.
@@ -107,21 +107,27 @@ func (l *bufLogger) WithName(name string) logr.LogSink {
107107
}
108108

109109
func defaultKVFormatter(kv ...interface{}) string {
110-
s := strings.Join(strings.Fields(fmt.Sprint(kv)), " ")
110+
s := strings.Join(strings.Fields(fmt.Sprint(kv...)), " ")
111111
s = strings.TrimPrefix(s, "[")
112112
s = strings.TrimSuffix(s, "]")
113113
return s
114114
}
115115

116-
func (l *bufLogger) writeLine(level, msg, line string) {
116+
func (l *bufLogger) writeLine(level, msg string, kv ...interface{}) {
117117
l.mu.Lock()
118118
defer l.mu.Unlock()
119-
l.buf.WriteString(level)
120-
l.buf.WriteString(fmt.Sprintf("%s ", l.name))
121-
l.buf.WriteString(fmt.Sprintf("%s ", msg))
122-
l.buf.WriteString(fmt.Sprintf("%s ", KVFormatter(l.values...)))
123-
l.buf.WriteString(fmt.Sprintf("%s", line))
124-
if !strings.HasSuffix(line, "\n") {
119+
120+
var line []string
121+
122+
fields := []string{level, l.name, msg, KVFormatter(l.values), KVFormatter(kv)}
123+
for _, f := range fields {
124+
if f != "" {
125+
line = append(line, f)
126+
}
127+
}
128+
129+
l.buf.WriteString(strings.Join(line, " "))
130+
if !strings.HasSuffix(line[len(line)-1], "\n") {
125131
l.buf.WriteRune('\n')
126132
}
127133
}

example/main.go example/example_test.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package buflogr_test
22

33
import (
44
"fmt"
@@ -23,24 +23,27 @@ func helper2(log logr.Logger, msg string) {
2323
log.WithCallDepth(2).Info(msg)
2424
}
2525

26-
func main() {
26+
func ExampleNew() {
2727
var log logr.Logger = buflogr.New()
28-
2928
log = log.WithName("MyName")
30-
example(log.WithValues("module", "example"))
31-
32-
log.Info("print the log")
33-
printBuffer(log)
34-
}
29+
log = log.WithValues("module", "example")
3530

36-
// example only depends on logr.
37-
func example(log logr.Logger) {
3831
log.Info("hello", "val1", 1, "val2", map[string]int{"k": 1})
3932
log.V(1).Info("you should see this")
40-
log.V(1).V(1).Info("you should NOT see this")
33+
log.V(1).V(1).Info("you will also see this")
4134
log.Error(nil, "uh oh", "trouble", true, "reasons", []float64{0.1, 0.11, 3.14})
4235
log.Error(e{"an error occurred"}, "goodbye", "code", -1)
4336
helper(log, "thru a helper")
37+
38+
printBuffer(log)
39+
40+
// Output:
41+
// INFO MyName hello module example val1 1 val2 map[k:1]
42+
// V[1] MyName you should see this module example
43+
// V[2] MyName you will also see this module example
44+
// ERROR MyName uh oh module example trouble true reasons [0.1 0.11 3.14]
45+
// ERROR MyName goodbye module example code -1
46+
// INFO MyName thru a helper module example
4447
}
4548

4649
// printBuffer breaks the abstraction to print the logged text in the buffer.

0 commit comments

Comments
 (0)