From be0468eb3160827eac879561c90ed1046f1feff6 Mon Sep 17 00:00:00 2001 From: Haw Loeung Date: Sun, 20 Nov 2022 11:15:38 +1100 Subject: [PATCH] Improve on logging and make consistent with matterclient --- bridge/mattermost/mattermost.go | 20 ++++++++++++++------ bridge/slack/slack.go | 16 ++++++++++++---- bridge/slack/utils.go | 6 +++--- main.go | 8 ++++++-- mm-go-irckit/user.go | 6 +++--- 5 files changed, 38 insertions(+), 18 deletions(-) diff --git a/bridge/mattermost/mattermost.go b/bridge/mattermost/mattermost.go index 1c6c1442..8d607042 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -10,10 +10,11 @@ import ( "github.com/42wim/matterircd/bridge" "github.com/davecgh/go-spew/spew" + prefixed "github.com/matterbridge/logrus-prefixed-formatter" "github.com/matterbridge/matterclient" "github.com/mattermost/mattermost-server/v6/model" "github.com/mitchellh/mapstructure" - logger "github.com/sirupsen/logrus" + "github.com/sirupsen/logrus" "github.com/spf13/viper" ) @@ -26,6 +27,8 @@ type Mattermost struct { connected bool } +var logger *logrus.Entry + func New(v *viper.Viper, cred bridge.Credentials, eventChan chan *bridge.Event, onWsConnect func()) (bridge.Bridger, *matterclient.Client, error) { m := &Mattermost{ credentials: cred, @@ -33,16 +36,21 @@ func New(v *viper.Viper, cred bridge.Credentials, eventChan chan *bridge.Event, v: v, } - logger.SetFormatter(&logger.TextFormatter{FullTimestamp: true}) + ourlog := logrus.New() + ourlog.SetFormatter(&prefixed.TextFormatter{ + PrefixPadding: 18, + FullTimestamp: true, + }) + logger = ourlog.WithFields(logrus.Fields{"prefix": "bridge/mattermost"}) if v.GetBool("debug") { - logger.SetLevel(logger.DebugLevel) + ourlog.SetLevel(logrus.DebugLevel) } if v.GetBool("trace") { - logger.SetLevel(logger.TraceLevel) + ourlog.SetLevel(logrus.TraceLevel) } - fmt.Println("loggerlevel:", logger.GetLevel()) + fmt.Println("loggerlevel:", ourlog.GetLevel()) mc, err := m.loginToMattermost(onWsConnect) if err != nil { @@ -984,7 +992,7 @@ func (m *Mattermost) handleWsActionPost(rmsg *model.WebSocketEvent) { m.handleFileEvent(channelType, ghost, &data, rmsg) - logger.Debugf("handleWsActionPost() user %s sent %s", m.mc.GetUser(data.UserId).Username, data.Message) + logger.Debugf("handleWsActionPost() user %s sent %#v", m.mc.GetUser(data.UserId).Username, data.Message) logger.Debugf("%#v", data) //nolint:govet } diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index 463fcccc..315c5aa7 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -9,7 +9,8 @@ import ( "github.com/42wim/matterircd/bridge" "github.com/davecgh/go-spew/spew" - logger "github.com/sirupsen/logrus" + prefixed "github.com/matterbridge/logrus-prefixed-formatter" + "github.com/sirupsen/logrus" "github.com/slack-go/slack" "github.com/spf13/viper" ) @@ -29,6 +30,8 @@ type Slack struct { v *viper.Viper } +var logger *logrus.Entry + func New(v *viper.Viper, cred bridge.Credentials, eventChan chan *bridge.Event, onConnect func()) (bridge.Bridger, error) { s := &Slack{ credentials: cred, @@ -39,13 +42,18 @@ func New(v *viper.Viper, cred bridge.Credentials, eventChan chan *bridge.Event, var err error - logger.SetFormatter(&logger.TextFormatter{FullTimestamp: true}) + ourlog := logrus.New() + ourlog.SetFormatter(&prefixed.TextFormatter{ + PrefixPadding: 13, + FullTimestamp: true, + }) + logger = ourlog.WithFields(logrus.Fields{"prefix": "bridge/slack"}) if v.GetBool("debug") { - logger.SetLevel(logger.DebugLevel) + ourlog.SetLevel(logrus.DebugLevel) } if v.GetBool("trace") { - logger.SetLevel(logger.TraceLevel) + ourlog.SetLevel(logrus.TraceLevel) } s.sc, err = s.loginToSlack() diff --git a/bridge/slack/utils.go b/bridge/slack/utils.go index 26d0e280..a2cdb96c 100644 --- a/bridge/slack/utils.go +++ b/bridge/slack/utils.go @@ -12,7 +12,7 @@ import ( "strings" "time" - logger "github.com/sirupsen/logrus" + "github.com/sirupsen/logrus" "github.com/slack-go/slack" ) @@ -220,11 +220,11 @@ type httpClient struct { // taken from https://github.com/insomniacslk/irc-slack/blob/master/pkg/ircslack/irc_server.go func (hc httpClient) Do(req *http.Request) (*http.Response, error) { if hc.cookie != "" { - logger.Debug("Setting auth cookie") + logrus.Debug("Setting auth cookie") if strings.ToLower(req.URL.Scheme) == "https" { req.Header.Add("Cookie", hc.cookie) } else { - logger.Warning("Cookie is set but connection is not HTTPS, skipping") + logrus.Warning("Cookie is set but connection is not HTTPS, skipping") } } diff --git a/main.go b/main.go index ac49db19..3aba2226 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( "github.com/42wim/matterircd/config" irckit "github.com/42wim/matterircd/mm-go-irckit" "github.com/google/gops/agent" + prefixed "github.com/matterbridge/logrus-prefixed-formatter" "github.com/sirupsen/logrus" "github.com/spf13/pflag" "github.com/spf13/viper" @@ -31,8 +32,11 @@ var ( func main() { ourlog := logrus.New() - ourlog.Formatter = &logrus.TextFormatter{FullTimestamp: true} - logger = ourlog.WithFields(logrus.Fields{"module": "matterircd"}) + ourlog.Formatter = &prefixed.TextFormatter{ + PrefixPadding: 11, + FullTimestamp: true, + } + logger = ourlog.WithFields(logrus.Fields{"prefix": "matterircd"}) config.Logger = logger // config related. instantiate a new config.Config to store flags diff --git a/mm-go-irckit/user.go b/mm-go-irckit/user.go index d9dbb3cf..dcc96eec 100644 --- a/mm-go-irckit/user.go +++ b/mm-go-irckit/user.go @@ -168,7 +168,7 @@ func (u *User) Decode() { if bufferTimeout < 100 { bufferTimeout = 100 } - logger.Debugf("using paste buffer timeout: %#v\n", bufferTimeout) + logger.Debugf("using paste buffer timeout: %#v", bufferTimeout) t := timer.NewTimer(time.Duration(bufferTimeout) * time.Millisecond) t.Stop() go func(buffer chan *irc.Message, stop chan struct{}) { @@ -204,7 +204,7 @@ func (u *User) Decode() { if u.BufferedMsg != nil { // trim last newline u.BufferedMsg.Trailing = strings.TrimSpace(u.BufferedMsg.Trailing) - logger.Debugf("flushing buffer: %#v\n", u.BufferedMsg) + logger.Debugf("flushing buffer: %#v", u.BufferedMsg) u.DecodeCh <- u.BufferedMsg // clear buffer u.BufferedMsg = nil @@ -240,7 +240,7 @@ func (u *User) Decode() { } // PRIVMSG can be buffered if msg.Command == "PRIVMSG" { - logger.Debugf("B: %#v\n", dmsg) + logger.Debugf("B: %#v", dmsg) buffer <- msg } else { logger.Debug(dmsg)