-
Notifications
You must be signed in to change notification settings - Fork 16
/
message_log.go
87 lines (68 loc) · 1.58 KB
/
message_log.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package logger
import (
"flag"
"github.com/sirupsen/logrus"
)
var (
DefaultMessageLogger = NewMessageLogService(&Config{
BasePrefix: "core",
DefaultLevel: "info",
})
)
// Used to log message that need send to remote such as Elastic Search
type messageLogger struct {
*stdLogger
// log error message
log Logger
// flags
logPath string
}
func NewMessageLogService(config *Config) *messageLogger {
if config == nil {
config = &Config{}
}
appLog := NewAppLogService(config)
appLog.logger.Formatter = &logrus.JSONFormatter{}
newLog := logrus.New()
newLog.Formatter = logrus.Formatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "15:04:05",
})
log := &logger{logrus.NewEntry(newLog)}
return &messageLogger{
stdLogger: appLog,
log: log,
}
}
// Implement Runnable interface
func (m *messageLogger) Name() string { return "file-logger" }
func (m *messageLogger) InitFlags() {
flag.StringVar(&m.logPath, "logfile", "", "file to write log to. Default write to console")
m.stdLogger.InitFlags()
}
func (m *messageLogger) Configure() error {
if m.logPath == "" {
lv := mustParseLevel(m.stdLogger.cfg.DefaultLevel)
m.stdLogger.logger.SetLevel(lv)
return nil
}
out, err := newReloadFile(m.logPath)
if err != nil {
m.log.Fatal("Fail to open log file: ", err.Error())
}
m.logger.Out = out
return nil
}
func (m *messageLogger) Run() error {
return m.Configure()
}
func (m *messageLogger) Stop() <-chan bool {
c := make(chan bool)
go func() {
if file, ok := m.logger.Out.(*reloadFile); ok {
file.Close()
}
c <- true
}()
return c
}