forked from dcrodman/archon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
48 lines (40 loc) · 994 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
47
48
package archon
import (
"fmt"
"io"
"os"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
// Log is the global, threadsafe logger that can be used by any server instance.
var Log *logrus.Logger
// InitLogger configures the global logger and should be called on startup.
func InitLogger() {
var w io.Writer
var err error
logFile := viper.GetString("log_file_path")
if logFile == "" {
w = os.Stdout
} else {
w, err = os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
fmt.Println("ERROR: Failed to open Log file " + logFile)
os.Exit(1)
}
}
logLvl, err := logrus.ParseLevel(viper.GetString("log_level"))
if err != nil {
fmt.Println("ERROR: Failed to parse Log level: " + err.Error())
os.Exit(1)
}
Log = &logrus.Logger{
Out: w,
Formatter: &logrus.TextFormatter{
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
DisableSorting: true,
},
Hooks: make(logrus.LevelHooks),
Level: logLvl,
}
}