-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlog.go
74 lines (63 loc) · 1.41 KB
/
log.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
72
73
74
package main
import (
"fmt"
"strings"
)
type LogLevel int
const (
LogLevelDebug LogLevel = -4
LogLevelInfo LogLevel = 0
LogLevelWarn LogLevel = 4
LogLevelError LogLevel = 8
LogLevelSilent LogLevel = 12
)
var logLevelName = map[LogLevel]string{
LogLevelDebug: "debug",
LogLevelInfo: "info",
LogLevelWarn: "warn",
LogLevelError: "error",
LogLevelSilent: "silent",
}
func (level LogLevel) Matches(target LogLevel) bool {
return target >= level
}
func (level *LogLevel) Set(name string) error {
name = strings.ToLower(name)
for l, n := range logLevelName {
if n == name {
*level = l
return nil
}
}
return fmt.Errorf("unknown log level %q, defaulting to debug\n", name)
}
func (level LogLevel) String() string {
name, ok := logLevelName[level]
if !ok {
return fmt.Sprintf("LogLevel(%d)", level)
}
return name
}
func logln(at LogLevel, values ...any) {
if loglevel.Matches(at) {
fmt.Println(values...)
}
}
func logf(at LogLevel, format string, values ...any) {
if loglevel.Matches(at) {
fmt.Printf(format, values...)
}
}
type pipelineLog struct{}
func (pipelineLog) Info(args ...any) {
logln(LogLevelInfo, args...)
}
func (pipelineLog) Infof(format string, args ...any) {
logf(LogLevelInfo, format, args...)
}
func (pipelineLog) Error(args ...any) {
logln(LogLevelError, args...)
}
func (pipelineLog) Errorf(format string, args ...any) {
logf(LogLevelError, format, args...)
}