slackApp: nicen logs a bit

* Remove entry log lines
* Change topic format for pisg support
This commit is contained in:
Chris Sexton 2019-06-07 14:23:14 -04:00
parent 3d51d33da9
commit 7d159db6f1
1 changed files with 24 additions and 5 deletions

View File

@ -29,7 +29,7 @@ import (
) )
const DefaultRing = 5 const DefaultRing = 5
const defaultLogFormat = "[{{fixDate .Time \"2006-01-02 15:04:05\"}}] {{if .Action}}* {{.User.Name}}{{else}}<{{.User.Name}}>{{end}} {{.Body}}\n" const defaultLogFormat = "[{{fixDate .Time \"2006-01-02 15:04:05\"}}] {{if .TopicChange}}*** {{.User.Name}}{{else if .Action}}* {{.User.Name}}{{else}}<{{.User.Name}}>{{end}} {{.Body}}\n"
// 11:10AM DBG connectors/slackapp/slackApp.go:496 > Slack event dir=logs raw={"Action":false,"AdditionalData": // 11:10AM DBG connectors/slackapp/slackApp.go:496 > Slack event dir=logs raw={"Action":false,"AdditionalData":
// {"RAW_SLACK_TIMESTAMP":"1559920235.001100"},"Body":"aoeu","Channel":"C0S04SMRC","ChannelName":"test", // {"RAW_SLACK_TIMESTAMP":"1559920235.001100"},"Body":"aoeu","Channel":"C0S04SMRC","ChannelName":"test",
@ -187,15 +187,15 @@ func (s *SlackApp) msgReceivd(msg *slackevents.MessageEvent) {
isItMe := msg.BotID != "" && msg.BotID == s.myBotID isItMe := msg.BotID != "" && msg.BotID == s.myBotID
if !isItMe && msg.ThreadTimeStamp == "" { if !isItMe && msg.ThreadTimeStamp == "" {
m := s.buildMessage(msg) m := s.buildMessage(msg)
if err := s.log(m); err != nil {
log.Fatal().Err(err).Msg("Error logging message")
}
if m.Time.Before(s.lastRecieved) { if m.Time.Before(s.lastRecieved) {
log.Debug(). log.Debug().
Time("ts", m.Time). Time("ts", m.Time).
Interface("lastRecv", s.lastRecieved). Interface("lastRecv", s.lastRecieved).
Msg("Ignoring message") Msg("Ignoring message")
} else { } else {
if err := s.log(m); err != nil {
log.Fatal().Err(err).Msg("Error logging message")
}
s.lastRecieved = m.Time s.lastRecieved = m.Time
s.event(s, bot.Message, m) s.event(s, bot.Message, m)
} }
@ -511,6 +511,25 @@ func (s *SlackApp) Who(id string) []string {
// log writes to a <slackapp.log.dir>/<channel>.log // log writes to a <slackapp.log.dir>/<channel>.log
// Uses slackapp.log.format to write entries // Uses slackapp.log.format to write entries
func (s *SlackApp) log(raw msg.Message) error { func (s *SlackApp) log(raw msg.Message) error {
// Do some filtering and fixing up front
if raw.Body == "" {
return nil
}
data := struct {
msg.Message
TopicChange bool
}{
Message: raw,
}
if strings.Contains(raw.Body, "set the channel topic: ") {
topic := strings.SplitN(raw.Body, "set the channel topic: ", 2)
data.Body = "changed topic to " + topic[1]
data.TopicChange = true
}
dir := path.Join(s.config.Get("slackapp.log.dir", "logs"), raw.ChannelName) dir := path.Join(s.config.Get("slackapp.log.dir", "logs"), raw.ChannelName)
now := time.Now() now := time.Now()
fname := now.Format("20060102") + ".log" fname := now.Format("20060102") + ".log"
@ -536,7 +555,7 @@ func (s *SlackApp) log(raw msg.Message) error {
} }
defer f.Close() defer f.Close()
if err := s.logFormat.Execute(f, raw); err != nil { if err := s.logFormat.Execute(f, data); err != nil {
return err return err
} }