-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
38 lines (32 loc) · 892 Bytes
/
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
// Package logger implements a logger that takes a writer and a func to prefix its output.
package logger
import (
"fmt"
"io"
"sync"
)
// A Logger is a mutex-protected logger.
type Logger struct {
m sync.Mutex
w io.Writer
f func() string
}
// New returns a new Logger.
func New(w io.Writer, f func() string) *Logger {
return &Logger{f: f, w: w}
}
// Printf prefixes the Logger's output with the string returned from the Logger's f func.
func (l *Logger) Printf(format string, a ...interface{}) {
l.m.Lock()
fmt.Fprintf(l.w, format, prepend(l.f(), a)...)
l.m.Unlock()
}
// Println prefixes the Logger's output with the string returned from the Loggers' f func.
func (l *Logger) Println(a ...interface{}) {
l.m.Lock()
fmt.Fprintln(l.w, prepend(l.f(), a)...)
l.m.Unlock()
}
func prepend(v interface{}, a []interface{}) []interface{} {
return append([]interface{}{v}, a...)
}