-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathlog.go
47 lines (40 loc) · 968 Bytes
/
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
package log
import (
"fmt"
"io"
"strings"
"github.com/coder/coder/v2/codersdk"
)
type Func func(l Level, msg string, args ...any)
type Level string
// Below constants are the same as their codersdk equivalents.
const (
LevelTrace = Level(codersdk.LogLevelTrace)
LevelDebug = Level(codersdk.LogLevelDebug)
LevelInfo = Level(codersdk.LogLevelInfo)
LevelWarn = Level(codersdk.LogLevelWarn)
LevelError = Level(codersdk.LogLevelError)
)
// New logs to the provided io.Writer.
func New(w io.Writer, verbose bool) Func {
return func(l Level, msg string, args ...any) {
if !verbose {
switch l {
case LevelDebug, LevelTrace:
return
}
}
_, _ = fmt.Fprintf(w, msg, args...)
if !strings.HasSuffix(msg, "\n") {
_, _ = fmt.Fprintf(w, "\n")
}
}
}
// Wrap wraps the provided LogFuncs into a single Func.
func Wrap(fs ...Func) Func {
return func(l Level, msg string, args ...any) {
for _, f := range fs {
f(l, msg, args...)
}
}
}