-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlogger.go
69 lines (55 loc) · 1.47 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) The EfficientGo Authors.
// Licensed under the Apache License 2.0.
package e2e
import (
"fmt"
"io"
"strings"
"time"
)
// Logger is the fundamental interface for all log operations. Log creates a
// log event from keyvals, a variadic sequence of alternating keys and values.
// Implementations must be safe for concurrent use by multiple goroutines. In
// particular, any implementation of Logger that appends to keyvals or
// modifies or retains any of its elements must make a copy first.
// This is 1:1 copy of "github.com/go-kit/kit/log" interface.
type Logger interface {
Log(keyvals ...interface{}) error
}
var _ Logger = &SimpleLogger{}
type SimpleLogger struct {
w io.Writer
}
func NewLogger(w io.Writer) *SimpleLogger {
return &SimpleLogger{
w: w,
}
}
func (l *SimpleLogger) Log(keyvals ...interface{}) error {
b := strings.Builder{}
b.WriteString(time.Now().Format("15:04:05"))
for _, v := range keyvals {
b.WriteString(" " + fmt.Sprintf("%v", v))
}
b.WriteString("\n")
_, err := l.w.Write([]byte(b.String()))
return err
}
type LinePrefixLogger struct {
prefix string
logger Logger
}
func (w *LinePrefixLogger) Write(p []byte) (n int, err error) {
for _, line := range strings.Split(string(p), "\n") {
// Skip empty lines.
line = strings.TrimSpace(line)
if line == "" {
continue
}
// Write the prefix + line to the wrapped writer.
if err := w.logger.Log(w.prefix + line); err != nil {
return 0, err
}
}
return len(p), nil
}