-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogging.go
52 lines (44 loc) · 1.08 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
50
51
52
package main
import (
"flag"
"os"
"strings"
"github.com/Sirupsen/logrus"
)
var log = logrus.New()
func init() {
logLevel := flag.String("log-level", "debug", "Log level")
logFile := flag.String("log-file", "", "Log file")
flag.Parse()
*logLevel = strings.ToLower(*logLevel)
switch *logLevel {
default:
log.Fatal("Bad input for 'log-level' flag")
case "debug":
log.Level = logrus.DebugLevel
case "info":
log.Level = logrus.InfoLevel
case "warn", "warning":
log.Level = logrus.WarnLevel
case "error":
log.Level = logrus.ErrorLevel
case "panic":
log.Level = logrus.PanicLevel
case "fatal":
log.Level = logrus.FatalLevel
}
// logrus.SetFormatter(&logrus.TextFormatter{ForceColors: true})
if *logFile != "" {
file, err := os.OpenFile(*logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err == nil {
log.Out = file
} else {
log.WithFields(logrus.Fields{
"file": *logFile,
}).Info("Failed to log to file, using default stderr")
}
} else {
log.Info("No logfile provided, using default stderr")
}
log.Infof("Logging on %v level", log.Level)
}