-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.go
58 lines (49 loc) · 1.15 KB
/
logger.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
/*
Copyright 2019 Adevinta
*/
package stream
import (
"os"
"github.com/sirupsen/logrus"
)
// LoggerConfig defines required Vulcan Logger configuration.
type LoggerConfig struct {
LogFile string
LogLevel string
}
// NewLogger provides a logrus FieldLogger.
func NewLogger(lc LoggerConfig) (logrus.FieldLogger, *os.File, error) {
logger := logrus.New().WithFields(logrus.Fields{
"app": "VULCAN-STREAM",
})
logger.Logger.Formatter = &logrus.TextFormatter{
FullTimestamp: true,
DisableColors: true,
}
var file *os.File
var err error
if lc.LogFile != "" {
file, err = os.OpenFile(lc.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
return nil, nil, err
}
logger.Logger.Out = file
}
switch lc.LogLevel {
case "DEBUG":
logger.Logger.Level = logrus.DebugLevel
case "INFO":
logger.Logger.Level = logrus.InfoLevel
case "WARN":
logger.Logger.Level = logrus.WarnLevel
case "ERROR":
logger.Logger.Level = logrus.ErrorLevel
case "FATAL":
logger.Logger.Level = logrus.FatalLevel
case "PANIC":
logger.Logger.Level = logrus.PanicLevel
default:
logger.Logger.Level = logrus.DebugLevel
}
return logger, file, nil
}