-
-
Notifications
You must be signed in to change notification settings - Fork 732
/
logger.go
87 lines (72 loc) · 1.79 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//go:build linux
// +build linux
package sensor
import (
"fmt"
"os"
log "github.com/sirupsen/logrus"
"github.com/slimtoolkit/slim/pkg/util/fsutil"
)
func configureLogger(
enableDebug bool,
levelName string,
format string,
logFile string,
) error {
if err := setLogLevel(enableDebug, levelName); err != nil {
return fmt.Errorf("failed to set log-level: %v", err)
}
if err := setLogFormat(format); err != nil {
return fmt.Errorf("failed to set log format: %v", err)
}
if len(logFile) > 0 {
// This touch is not ideal - need to understand how to merge this logic with artifacts.PrepareEnv().
if err := fsutil.Touch(logFile); err != nil {
return fmt.Errorf("failed to set log output destination to %q, touch failed with: %v", logFile, err)
}
f, err := os.OpenFile(logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return fmt.Errorf("failed to set log output destination to %q: %w", logFile, err)
}
log.SetOutput(f)
}
return nil
}
func setLogFormat(format string) error {
switch format {
case "text":
log.SetFormatter(&log.TextFormatter{DisableColors: true})
case "json":
log.SetFormatter(&log.JSONFormatter{})
default:
return fmt.Errorf("unknown log-format %q", format)
}
return nil
}
func setLogLevel(enableDebug bool, levelName string) error {
if enableDebug {
log.SetLevel(log.DebugLevel)
return nil
}
var logLevel log.Level
switch levelName {
case "trace":
logLevel = log.TraceLevel
case "debug":
logLevel = log.DebugLevel
case "info":
logLevel = log.InfoLevel
case "warn":
logLevel = log.WarnLevel
case "error":
logLevel = log.ErrorLevel
case "fatal":
logLevel = log.FatalLevel
case "panic":
logLevel = log.PanicLevel
default:
return fmt.Errorf("unknown log-level %q", levelName)
}
log.SetLevel(logLevel)
return nil
}