-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.go
44 lines (34 loc) · 1.11 KB
/
printer.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
package log
import (
"context"
)
// NewPrinter creates new logger that prints to provided function
// Example: NewPrinter("stdout", fmt.Printf)
func NewPrinter(name string, printf func(format string, a ...any) (n int, err error)) Logger {
return printer{name, printf}
}
type printer struct {
name string
printf func(format string, a ...any) (n int, err error)
}
func (p printer) Name() string {
return p.name
}
func (p printer) Debugf(c context.Context, format string, args ...interface{}) {
p.log("DEBUG", format, args...)
}
func (p printer) Infof(c context.Context, format string, args ...interface{}) {
p.log("INFO", format, args...)
}
func (p printer) Warningf(c context.Context, format string, args ...interface{}) {
p.log("WARNING", format, args...)
}
func (p printer) Errorf(c context.Context, format string, args ...interface{}) {
p.log("ERROR", format, args...)
}
func (p printer) Criticalf(c context.Context, format string, args ...interface{}) {
p.log("CRITICAL", format, args...)
}
func (p printer) log(level, format string, args ...interface{}) {
_, _ = p.printf(level+": "+format+"\n", args...)
}