-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
54 lines (43 loc) · 799 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
"log"
"sync"
)
type logger interface {
newSubLogger(prefix string) logger
Printf(fmt string, v ...any)
}
type rootLogger struct {
lock *sync.Mutex
}
func newLogger() logger {
return &rootLogger{
lock: &sync.Mutex{},
}
}
func (l *rootLogger) newSubLogger(prefix string) logger {
return &subLogger{
parent: l,
prefix: prefix,
}
}
func (l *rootLogger) Printf(fmt string, v ...any) {
l.lock.Lock()
defer l.lock.Unlock()
log.Printf(fmt, v...)
}
type subLogger struct {
parent logger
prefix string
}
func (s *subLogger) newSubLogger(prefix string) logger {
return &subLogger{
parent: s,
prefix: prefix,
}
}
func (s *subLogger) Printf(format string, v ...any) {
str := fmt.Sprintf(format, v...)
s.parent.Printf("%s: %s", s.prefix, str)
}