5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-25 19:01:35 +00:00

Use prefixed-formatter for better logging

This commit is contained in:
Wim 2018-02-21 00:20:25 +01:00
parent 6ea368c383
commit d1227b5fc9
17 changed files with 71 additions and 42 deletions

View File

@ -30,7 +30,7 @@ var flog *log.Entry
var protocol = "api" var protocol = "api"
func init() { func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"prefix": protocol})
} }
func New(cfg *config.BridgeConfig) *Api { func New(cfg *config.BridgeConfig) *Api {

View File

@ -36,6 +36,12 @@ type Bridge struct {
Joined map[string]bool Joined map[string]bool
} }
var flog *log.Entry
func init() {
flog = log.WithFields(log.Fields{"prefix": "bridge"})
}
func New(cfg *config.Config, bridge *config.Bridge, c chan config.Message) *Bridge { func New(cfg *config.Config, bridge *config.Bridge, c chan config.Message) *Bridge {
b := new(Bridge) b := new(Bridge)
b.Channels = make(map[string]config.ChannelInfo) b.Channels = make(map[string]config.ChannelInfo)
@ -100,7 +106,7 @@ func (b *Bridge) JoinChannels() error {
func (b *Bridge) joinChannels(channels map[string]config.ChannelInfo, exists map[string]bool) error { func (b *Bridge) joinChannels(channels map[string]config.ChannelInfo, exists map[string]bool) error {
for ID, channel := range channels { for ID, channel := range channels {
if !exists[ID] { if !exists[ID] {
log.Infof("%s: joining %s (ID: %s)", b.Account, channel.Name, ID) flog.Infof("%s: joining %s (ID: %s)", b.Account, channel.Name, ID)
err := b.JoinChannel(channel) err := b.JoinChannel(channel)
if err != nil { if err != nil {
return err return err

View File

@ -4,8 +4,8 @@ import (
"bytes" "bytes"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/bridge/helper" "github.com/42wim/matterbridge/bridge/helper"
log "github.com/sirupsen/logrus"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus"
"regexp" "regexp"
"strings" "strings"
"sync" "sync"
@ -29,7 +29,7 @@ var flog *log.Entry
var protocol = "discord" var protocol = "discord"
func init() { func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"prefix": protocol})
} }
func New(cfg *config.BridgeConfig) *bdiscord { func New(cfg *config.BridgeConfig) *bdiscord {

View File

@ -21,7 +21,7 @@ var flog *log.Entry
var protocol = "gitter" var protocol = "gitter"
func init() { func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"prefix": protocol})
} }
func New(cfg *config.BridgeConfig) *Bgitter { func New(cfg *config.BridgeConfig) *Bgitter {

View File

@ -6,11 +6,11 @@ import (
"fmt" "fmt"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/bridge/helper" "github.com/42wim/matterbridge/bridge/helper"
log "github.com/sirupsen/logrus"
"github.com/lrstanley/girc" "github.com/lrstanley/girc"
"github.com/paulrosania/go-charset/charset" "github.com/paulrosania/go-charset/charset"
_ "github.com/paulrosania/go-charset/data" _ "github.com/paulrosania/go-charset/data"
"github.com/saintfish/chardet" "github.com/saintfish/chardet"
log "github.com/sirupsen/logrus"
"io" "io"
"io/ioutil" "io/ioutil"
"net" "net"
@ -37,7 +37,7 @@ var flog *log.Entry
var protocol = "irc" var protocol = "irc"
func init() { func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"prefix": protocol})
} }
func New(cfg *config.BridgeConfig) *Birc { func New(cfg *config.BridgeConfig) *Birc {

View File

@ -25,7 +25,7 @@ var flog *log.Entry
var protocol = "matrix" var protocol = "matrix"
func init() { func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"prefix": protocol})
} }
func New(cfg *config.BridgeConfig) *Bmatrix { func New(cfg *config.BridgeConfig) *Bmatrix {

View File

@ -42,7 +42,7 @@ var flog *log.Entry
var protocol = "mattermost" var protocol = "mattermost"
func init() { func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"prefix": protocol})
} }
func New(cfg *config.BridgeConfig) *Bmattermost { func New(cfg *config.BridgeConfig) *Bmattermost {

View File

@ -22,7 +22,7 @@ var flog *log.Entry
var protocol = "rocketchat" var protocol = "rocketchat"
func init() { func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"prefix": protocol})
} }
func New(cfg *config.BridgeConfig) *Brocketchat { func New(cfg *config.BridgeConfig) *Brocketchat {

View File

@ -40,7 +40,7 @@ var flog *log.Entry
var protocol = "slack" var protocol = "slack"
func init() { func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"prefix": protocol})
} }
func New(cfg *config.BridgeConfig) *Bslack { func New(cfg *config.BridgeConfig) *Bslack {

View File

@ -20,7 +20,7 @@ var flog *log.Entry
var protocol = "sshchat" var protocol = "sshchat"
func init() { func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"prefix": protocol})
} }
func New(cfg *config.BridgeConfig) *Bsshchat { func New(cfg *config.BridgeConfig) *Bsshchat {

View File

@ -25,7 +25,7 @@ var flog *log.Entry
var protocol = "steam" var protocol = "steam"
func init() { func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"prefix": protocol})
} }
func New(cfg *config.BridgeConfig) *Bsteam { func New(cfg *config.BridgeConfig) *Bsteam {

View File

@ -21,7 +21,7 @@ var flog *log.Entry
var protocol = "telegram" var protocol = "telegram"
func init() { func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"prefix": protocol})
} }
func New(cfg *config.BridgeConfig) *Btelegram { func New(cfg *config.BridgeConfig) *Btelegram {

View File

@ -22,7 +22,7 @@ var flog *log.Entry
var protocol = "xmpp" var protocol = "xmpp"
func init() { func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"prefix": protocol})
} }
func New(cfg *config.BridgeConfig) *Bxmpp { func New(cfg *config.BridgeConfig) *Bxmpp {

View File

@ -34,6 +34,12 @@ type BrMsgID struct {
ChannelID string ChannelID string
} }
var flog *log.Entry
func init() {
flog = log.WithFields(log.Fields{"prefix": "gateway"})
}
func New(cfg config.Gateway, r *Router) *Gateway { func New(cfg config.Gateway, r *Router) *Gateway {
gw := &Gateway{Channels: make(map[string]*config.ChannelInfo), Message: r.Message, gw := &Gateway{Channels: make(map[string]*config.ChannelInfo), Message: r.Message,
Router: r, Bridges: make(map[string]*bridge.Bridge), Config: r.Config} Router: r, Bridges: make(map[string]*bridge.Bridge), Config: r.Config}
@ -78,10 +84,10 @@ func (gw *Gateway) reconnectBridge(br *bridge.Bridge) {
br.Disconnect() br.Disconnect()
time.Sleep(time.Second * 5) time.Sleep(time.Second * 5)
RECONNECT: RECONNECT:
log.Infof("Reconnecting %s", br.Account) flog.Infof("Reconnecting %s", br.Account)
err := br.Connect() err := br.Connect()
if err != nil { if err != nil {
log.Errorf("Reconnection failed: %s. Trying again in 60 seconds", err) flog.Errorf("Reconnection failed: %s. Trying again in 60 seconds", err)
time.Sleep(time.Second * 60) time.Sleep(time.Second * 60)
goto RECONNECT goto RECONNECT
} }
@ -145,7 +151,7 @@ func (gw *Gateway) getDestChannel(msg *config.Message, dest bridge.Bridge) []con
continue continue
} }
// do samechannelgateway logic // do samechannelgateway flogic
if channel.SameChannel[msg.Gateway] { if channel.SameChannel[msg.Gateway] {
if msg.Channel == channel.Name && msg.Account != dest.Account { if msg.Channel == channel.Name && msg.Account != dest.Account {
channels = append(channels, *channel) channels = append(channels, *channel)
@ -196,7 +202,7 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
// broadcast to every out channel (irc QUIT) // broadcast to every out channel (irc QUIT)
if msg.Channel == "" && msg.Event != config.EVENT_JOIN_LEAVE { if msg.Channel == "" && msg.Event != config.EVENT_JOIN_LEAVE {
log.Debug("empty channel") flog.Debug("empty channel")
return brMsgIDs return brMsgIDs
} }
originchannel := msg.Channel originchannel := msg.Channel
@ -214,7 +220,7 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
continue continue
} }
} }
log.Debugf("Sending %#v from %s (%s) to %s (%s)", msg, msg.Account, originchannel, dest.Account, channel.Name) flog.Debugf("Sending %#v from %s (%s) to %s (%s)", msg, msg.Account, originchannel, dest.Account, channel.Name)
msg.Channel = channel.Name msg.Channel = channel.Name
msg.Avatar = gw.modifyAvatar(origmsg, dest) msg.Avatar = gw.modifyAvatar(origmsg, dest)
msg.Username = gw.modifyUsername(origmsg, dest) msg.Username = gw.modifyUsername(origmsg, dest)
@ -258,12 +264,12 @@ func (gw *Gateway) ignoreMessage(msg *config.Message) bool {
len(msg.Extra[config.EVENT_FILE_FAILURE_SIZE]) > 0) { len(msg.Extra[config.EVENT_FILE_FAILURE_SIZE]) > 0) {
return false return false
} }
log.Debugf("ignoring empty message %#v from %s", msg, msg.Account) flog.Debugf("ignoring empty message %#v from %s", msg, msg.Account)
return true return true
} }
for _, entry := range strings.Fields(gw.Bridges[msg.Account].Config.IgnoreNicks) { for _, entry := range strings.Fields(gw.Bridges[msg.Account].Config.IgnoreNicks) {
if msg.Username == entry { if msg.Username == entry {
log.Debugf("ignoring %s from %s", msg.Username, msg.Account) flog.Debugf("ignoring %s from %s", msg.Username, msg.Account)
return true return true
} }
} }
@ -272,11 +278,11 @@ func (gw *Gateway) ignoreMessage(msg *config.Message) bool {
if entry != "" { if entry != "" {
re, err := regexp.Compile(entry) re, err := regexp.Compile(entry)
if err != nil { if err != nil {
log.Errorf("incorrect regexp %s for %s", entry, msg.Account) flog.Errorf("incorrect regexp %s for %s", entry, msg.Account)
continue continue
} }
if re.MatchString(msg.Text) { if re.MatchString(msg.Text) {
log.Debugf("matching %s. ignoring %s from %s", entry, msg.Text, msg.Account) flog.Debugf("matching %s. ignoring %s from %s", entry, msg.Text, msg.Account)
return true return true
} }
} }
@ -303,7 +309,7 @@ func (gw *Gateway) modifyUsername(msg config.Message, dest *bridge.Bridge) strin
// TODO move compile to bridge init somewhere // TODO move compile to bridge init somewhere
re, err := regexp.Compile(search) re, err := regexp.Compile(search)
if err != nil { if err != nil {
log.Errorf("regexp in %s failed: %s", msg.Account, err) flog.Errorf("regexp in %s failed: %s", msg.Account, err)
break break
} }
msg.Username = re.ReplaceAllString(msg.Username, replace) msg.Username = re.ReplaceAllString(msg.Username, replace)
@ -351,7 +357,7 @@ func (gw *Gateway) modifyMessage(msg *config.Message) {
// TODO move compile to bridge init somewhere // TODO move compile to bridge init somewhere
re, err := regexp.Compile(search) re, err := regexp.Compile(search)
if err != nil { if err != nil {
log.Errorf("regexp in %s failed: %s", msg.Account, err) flog.Errorf("regexp in %s failed: %s", msg.Account, err)
break break
} }
msg.Text = re.ReplaceAllString(msg.Text, replace) msg.Text = re.ReplaceAllString(msg.Text, replace)
@ -383,10 +389,10 @@ func (gw *Gateway) handleFiles(msg *config.Message) {
req.Header.Set("Content-Type", "binary/octet-stream") req.Header.Set("Content-Type", "binary/octet-stream")
_, err := client.Do(req) _, err := client.Do(req)
if err != nil { if err != nil {
log.Errorf("mediaserver upload failed: %#v", err) flog.Errorf("mediaserver upload failed: %#v", err)
continue continue
} }
log.Debugf("mediaserver download URL = %s", durl) flog.Debugf("mediaserver download URL = %s", durl)
// we uploaded the file successfully. Add the SHA // we uploaded the file successfully. Add the SHA
extra.SHA = sha1sum extra.SHA = sha1sum
msg.Extra["file"][i] = extra msg.Extra["file"][i] = extra

View File

@ -5,7 +5,7 @@ import (
"github.com/42wim/matterbridge/bridge" "github.com/42wim/matterbridge/bridge"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/gateway/samechannel" "github.com/42wim/matterbridge/gateway/samechannel"
log "github.com/sirupsen/logrus" //log "github.com/sirupsen/logrus"
// "github.com/davecgh/go-spew/spew" // "github.com/davecgh/go-spew/spew"
"time" "time"
) )
@ -42,13 +42,13 @@ func NewRouter(cfg *config.Config) (*Router, error) {
func (r *Router) Start() error { func (r *Router) Start() error {
m := make(map[string]*bridge.Bridge) m := make(map[string]*bridge.Bridge)
for _, gw := range r.Gateways { for _, gw := range r.Gateways {
log.Infof("Parsing gateway %s", gw.Name) flog.Infof("Parsing gateway %s", gw.Name)
for _, br := range gw.Bridges { for _, br := range gw.Bridges {
m[br.Account] = br m[br.Account] = br
} }
} }
for _, br := range m { for _, br := range m {
log.Infof("Starting bridge: %s ", br.Account) flog.Infof("Starting bridge: %s ", br.Account)
err := br.Connect() err := br.Connect()
if err != nil { if err != nil {
return fmt.Errorf("Bridge %s failed to start: %v", br.Account, err) return fmt.Errorf("Bridge %s failed to start: %v", br.Account, err)

View File

@ -18,7 +18,8 @@ var (
) )
func main() { func main() {
log.SetFormatter(&prefixed.TextFormatter{FullTimestamp: true}) log.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 10, DisableColors: true, FullTimestamp: true})
flog := log.WithFields(log.Fields{"prefix": "main"})
flagConfig := flag.String("conf", "matterbridge.toml", "config file") flagConfig := flag.String("conf", "matterbridge.toml", "config file")
flagDebug := flag.Bool("debug", false, "enable debug") flagDebug := flag.Bool("debug", false, "enable debug")
flagVersion := flag.Bool("version", false, "show version") flagVersion := flag.Bool("version", false, "show version")
@ -33,24 +34,24 @@ func main() {
return return
} }
if *flagDebug || os.Getenv("DEBUG") == "1" { if *flagDebug || os.Getenv("DEBUG") == "1" {
log.SetFormatter(&prefixed.TextFormatter{FullTimestamp: false}) log.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 10, DisableColors: true, FullTimestamp: false})
log.Info("Enabling debug") flog.Info("Enabling debug")
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
} }
log.Printf("Running version %s %s", version, githash) flog.Printf("Running version %s %s", version, githash)
if strings.Contains(version, "-dev") { if strings.Contains(version, "-dev") {
log.Println("WARNING: THIS IS A DEVELOPMENT VERSION. Things may break.") flog.Println("WARNING: THIS IS A DEVELOPMENT VERSION. Things may break.")
} }
cfg := config.NewConfig(*flagConfig) cfg := config.NewConfig(*flagConfig)
cfg.General.Debug = *flagDebug cfg.General.Debug = *flagDebug
r, err := gateway.NewRouter(cfg) r, err := gateway.NewRouter(cfg)
if err != nil { if err != nil {
log.Fatalf("Starting gateway failed: %s", err) flog.Fatalf("Starting gateway failed: %s", err)
} }
err = r.Start() err = r.Start()
if err != nil { if err != nil {
log.Fatalf("Starting gateway failed: %s", err) flog.Fatalf("Starting gateway failed: %s", err)
} }
log.Printf("Gateway(s) started succesfully. Now relaying messages") flog.Printf("Gateway(s) started succesfully. Now relaying messages")
select {} select {}
} }

View File

@ -11,8 +11,8 @@ import (
"sync" "sync"
"time" "time"
"github.com/sirupsen/logrus"
"github.com/mgutz/ansi" "github.com/mgutz/ansi"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/terminal" "golang.org/x/crypto/ssh/terminal"
) )
@ -110,6 +110,11 @@ type TextFormatter struct {
// Its default value is zero, which means no padding will be applied for msg. // Its default value is zero, which means no padding will be applied for msg.
SpacePadding int SpacePadding int
// Pad prefix field with spaces on the right for display.
// The value for this parameter will be the size of padding.
// Its default value is zero, which means no padding will be applied for prefix.
PrefixPadding int
// Color scheme to use. // Color scheme to use.
colorScheme *compiledColorScheme colorScheme *compiledColorScheme
@ -253,14 +258,25 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys
prefix := "" prefix := ""
message := entry.Message message := entry.Message
adjustedPrefixPadding := f.PrefixPadding //compensate for ANSI color sequences
if prefixValue, ok := entry.Data["prefix"]; ok { if prefixValue, ok := entry.Data["prefix"]; ok {
rawPrefixLength := len(prefixValue.(string))
prefix = colorScheme.PrefixColor(" " + prefixValue.(string) + ":") prefix = colorScheme.PrefixColor(" " + prefixValue.(string) + ":")
adjustedPrefixPadding = f.PrefixPadding + (len(prefix) - rawPrefixLength - 1)
} else { } else {
prefixValue, trimmedMsg := extractPrefix(entry.Message) prefixValue, trimmedMsg := extractPrefix(entry.Message)
rawPrefixLength := len(prefixValue)
if len(prefixValue) > 0 { if len(prefixValue) > 0 {
prefix = colorScheme.PrefixColor(" " + prefixValue + ":") prefix = colorScheme.PrefixColor(" " + prefixValue + ":")
message = trimmedMsg message = trimmedMsg
} }
adjustedPrefixPadding = f.PrefixPadding + (len(prefix) - rawPrefixLength - 1)
}
prefixFormat := "%s"
if f.PrefixPadding != 0 {
prefixFormat = fmt.Sprintf("%%-%ds", adjustedPrefixPadding)
} }
messageFormat := "%s" messageFormat := "%s"
@ -269,7 +285,7 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys
} }
if f.DisableTimestamp { if f.DisableTimestamp {
fmt.Fprintf(b, "%s%s "+messageFormat, level, prefix, message) fmt.Fprintf(b, "%s"+prefixFormat+" "+messageFormat, level, prefix, message)
} else { } else {
var timestamp string var timestamp string
if !f.FullTimestamp { if !f.FullTimestamp {
@ -277,7 +293,7 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys
} else { } else {
timestamp = fmt.Sprintf("[%s]", entry.Time.Format(timestampFormat)) timestamp = fmt.Sprintf("[%s]", entry.Time.Format(timestampFormat))
} }
fmt.Fprintf(b, "%s %s%s "+messageFormat, colorScheme.TimestampColor(timestamp), level, prefix, message) fmt.Fprintf(b, "%s %s"+prefixFormat+" "+messageFormat, colorScheme.TimestampColor(timestamp), level, prefix, message)
} }
for _, k := range keys { for _, k := range keys {
if k != "prefix" { if k != "prefix" {