-
Notifications
You must be signed in to change notification settings - Fork 17
/
log.go
134 lines (110 loc) · 2.86 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package cagent
import (
"fmt"
"os"
"path/filepath"
"github.com/sirupsen/logrus"
)
type LogLevel string
const (
LogLevelDebug LogLevel = "debug"
LogLevelInfo LogLevel = "info"
LogLevelError LogLevel = "error"
)
func (lvl LogLevel) IsValid() bool {
switch lvl {
case LogLevelDebug:
fallthrough
case LogLevelInfo:
fallthrough
case LogLevelError:
return true
default:
return false
}
}
func (lvl LogLevel) LogrusLevel() logrus.Level {
switch lvl {
case LogLevelDebug:
return logrus.DebugLevel
case LogLevelError:
return logrus.ErrorLevel
default:
return logrus.InfoLevel
}
}
type logrusFileHook struct {
file *os.File
flag int
chmod os.FileMode
formatter *logrus.TextFormatter
}
func addLogFileHook(file string, flag int, chmod os.FileMode) error {
dir := filepath.Dir(file)
err := os.MkdirAll(dir, 0755)
if err != nil {
logrus.WithError(err).Errorf("Failed to create the logs dir: '%s'", dir)
}
plainFormatter := &logrus.TextFormatter{FullTimestamp: true, DisableColors: true}
logFile, err := os.OpenFile(file, flag, chmod)
if err != nil {
return fmt.Errorf("Unable to write log file: %s", err.Error())
}
hook := &logrusFileHook{logFile, flag, chmod, plainFormatter}
logrus.AddHook(hook)
return nil
}
// Fire event
func (hook *logrusFileHook) Fire(entry *logrus.Entry) error {
plainformat, err := hook.formatter.Format(entry)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "unable to format log entry %v, %v", entry, err)
return err
}
line := string(plainformat)
_, err = hook.file.WriteString(line)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "unable to write file on filehook(entry.String) %v", err)
return err
}
return nil
}
func (hook *logrusFileHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
logrus.DebugLevel,
}
}
// Sets Log level and corresponding logrus level
func (ca *Cagent) SetLogLevel(lvl LogLevel) {
ca.Config.LogLevel = lvl
logrus.SetLevel(lvl.LogrusLevel())
}
func (ca *Cagent) configureLogger() {
tfmt := logrus.TextFormatter{FullTimestamp: true, DisableColors: true}
logrus.SetFormatter(&tfmt)
ca.SetLogLevel(ca.Config.LogLevel)
if ca.Config.LogFile != "" {
err := addLogFileHook(ca.Config.LogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
logrus.Error("Can't write logs to file: ", err.Error())
}
}
// If a logfile is specified, syslog must be disabled and logs are written to that file and nowhere else.
if ca.Config.LogSyslog != "" {
err := addSyslogHook(ca.Config.LogSyslog)
if err != nil {
logrus.Error("Can't set up syslog: ", err.Error())
}
}
// sets standard logging to /dev/null
devNull, err := os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
logrus.Error("err", err)
}
logrus.SetOutput(devNull)
}