-
Notifications
You must be signed in to change notification settings - Fork 1
/
logging.go
46 lines (38 loc) · 957 Bytes
/
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
package logging
import (
"bufio"
"os"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// Configure sets up the logger
func Configure(logLevel string, logFile string) error {
hostname, err := os.Hostname()
if err != nil {
return errors.Wrap(err, "getting hostname")
}
logrus.WithField("hostname", hostname)
// use a file if you want
if logFile != "" {
f, errOpen := os.OpenFile(logFile, os.O_RDWR|os.O_APPEND, 0660)
if errOpen != nil {
return errors.Wrapf(errOpen, "opening log file %s", logFile)
}
logrus.SetOutput(bufio.NewWriter(f))
}
if logLevel != "" {
level, err := logrus.ParseLevel(strings.ToUpper(logLevel))
if err != nil {
return errors.Wrapf(err, "setting log level to %s", level)
}
logrus.SetLevel(level)
}
// always use the fulltimestamp
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})
// Set log output to stderr
logrus.SetOutput(os.Stderr)
return nil
}