-
Notifications
You must be signed in to change notification settings - Fork 43
/
logger.go
47 lines (38 loc) · 956 Bytes
/
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
package bot
import (
"os"
"github.com/innogames/slack-bot/bot/config"
"github.com/innogames/slack-bot/client"
"github.com/nlopes/slack"
"github.com/rifflock/lfshook"
log "github.com/sirupsen/logrus"
)
// GetLogger provides logger instance for the given config
func GetLogger(cfg config.Config) *log.Logger {
level, _ := log.ParseLevel(cfg.Logger.Level)
log.SetOutput(os.Stdout)
log.SetLevel(level)
logger := log.New()
if cfg.Logger.File != "" {
logger.AddHook(lfshook.NewHook(
cfg.Logger.File,
&log.TextFormatter{},
))
}
return logger
}
func (b bot) getLogger(event slack.MessageEvent) *log.Entry {
_, username := client.GetUser(event.User)
channel := ""
if event.Channel[0] == 'D' {
channel = "@" + username
} else {
_, channel = client.GetChannel(event.Channel)
}
if event.SubType == TypeInternal {
channel += " (internal)"
}
return b.logger.
WithField("channel", channel).
WithField("user", username)
}