-
Notifications
You must be signed in to change notification settings - Fork 1
/
logging.go
49 lines (41 loc) · 1 KB
/
logging.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
package conf
import (
"bufio"
"os"
"strings"
"github.com/Sirupsen/logrus"
)
// LoggingConfig specifies all the parameters needed for logging
type LoggingConfig struct {
Level string
File string
}
// ConfigureLogging will take the logging configuration and also adds
// a few default parameters
func ConfigureLogging(config *LoggingConfig) (*logrus.Entry, error) {
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
// use a file if you want
if config.File != "" {
f, errOpen := os.OpenFile(config.File, os.O_RDWR|os.O_APPEND, 0660)
if errOpen != nil {
return nil, errOpen
}
logrus.SetOutput(bufio.NewWriter(f))
}
if config.Level != "" {
level, err := logrus.ParseLevel(strings.ToUpper(config.Level))
if err != nil {
return nil, err
}
logrus.SetLevel(level)
}
// always use the fulltimestamp
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
DisableTimestamp: false,
})
return logrus.StandardLogger().WithField("hostname", hostname), nil
}