-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogging.go
50 lines (40 loc) · 995 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
49
50
package jiraworklog
import (
"io"
"os"
"github.com/sirupsen/logrus"
)
type LoggerOptions struct {
Application string
LogFile string
Level string
}
func NewLogger(options LoggerOptions) *logrus.Entry {
if options.Level == "" {
options.Level = "warn"
}
level, err := logrus.ParseLevel(options.Level)
if err != nil {
panic(err)
}
log := logrus.New()
log.Level = level
log.Formatter = &logrus.TextFormatter{
//ForceColors: true,
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
//DisableColors: true,
}
// &logrus.JSONFormatter{}
if options.LogFile != "" {
log.Out = os.Stdout
file, err := os.OpenFile(options.LogFile, os.O_CREATE|os.O_WRONLY, 0666)
if err == nil {
log.Out = io.MultiWriter(file, os.Stdout)
} else {
log.Info("Failed to log to file, using default stderr")
}
}
logger := log.WithFields(logrus.Fields{"app": options.Application})
return logger
}