-
Notifications
You must be signed in to change notification settings - Fork 401
/
simplelog.go
71 lines (63 loc) · 1.23 KB
/
simplelog.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
70
71
// Package simplelog contains a very simple interface for logging strings at either Debug, Info, or Error levels
package simplelog
import (
"context"
"oss.terrastruct.com/d2/lib/log"
"oss.terrastruct.com/util-go/cmdlog"
)
type Logger interface {
Debug(string)
Info(string)
Error(string)
}
type logger struct {
logDebug *func(string)
logInfo *func(string)
logError *func(string)
}
func (l logger) Debug(s string) {
if l.logDebug != nil {
(*l.logDebug)(s)
}
}
func (l logger) Info(s string) {
if l.logInfo != nil {
(*l.logInfo)(s)
}
}
func (l logger) Error(s string) {
if l.logError != nil {
(*l.logError)(s)
}
}
func Make(logDebug, logInfo, logError *func(string)) Logger {
return logger{
logDebug: logDebug,
logInfo: logInfo,
logError: logError,
}
}
func FromLibLog(ctx context.Context) Logger {
lDebug := func(s string) {
log.Debug(ctx, s)
}
lInfo := func(s string) {
log.Info(ctx, s)
}
lError := func(s string) {
log.Error(ctx, s)
}
return Make(&lDebug, &lInfo, &lError)
}
func FromCmdLog(cl *cmdlog.Logger) Logger {
lDebug := func(s string) {
cl.Debug.Print(s)
}
lInfo := func(s string) {
cl.Info.Print(s)
}
lError := func(s string) {
cl.Error.Print(s)
}
return Make(&lDebug, &lInfo, &lError)
}