Skip to content

Commit

Permalink
Merge remote-tracking branch 'hloeung/logging-with-module-and-prefix'…
Browse files Browse the repository at this point in the history
… into logging-with-module-and-prefix
  • Loading branch information
hloeung committed Mar 16, 2023
2 parents 0e4278a + 5f68f5a commit dd25d9f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
20 changes: 14 additions & 6 deletions bridge/mattermost/mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (
"github.com/42wim/matterircd/bridge"
"github.com/davecgh/go-spew/spew"
lru "github.com/hashicorp/golang-lru"
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"
)

Expand All @@ -29,6 +30,8 @@ type Mattermost struct {
msglruCache *lru.Cache
}

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,
Expand All @@ -38,16 +41,21 @@ func New(v *viper.Viper, cred bridge.Credentials, eventChan chan *bridge.Event,
cache, _ := lru.New(300)
m.msglruCache = cache

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 {
Expand Down Expand Up @@ -1016,7 +1024,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
}

Expand Down
16 changes: 12 additions & 4 deletions bridge/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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,
Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions bridge/slack/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strings"
"time"

logger "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"github.com/slack-go/slack"
)

Expand Down Expand Up @@ -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")
}
}

Expand Down
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions mm-go-irckit/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (u *User) Encode(msgs ...*irc.Message) (err error) {
continue
}

logger.Debugf("-> %s", msg)
logger.Debugf("-> \"%s\"", msg)

err := u.Conn.Encode(msg)
if err != nil {
Expand All @@ -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{}) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit dd25d9f

Please sign in to comment.