5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-09 23:40:27 +00:00

Use logrus imports instead of log (#662)

This commit is contained in:
Duco van Amstel 2018-12-26 15:16:09 +01:00 committed by Wim
parent 1b834c6858
commit da8e415ae1
10 changed files with 42 additions and 43 deletions

View File

@ -1,10 +1,10 @@
package bridge package bridge
import ( import (
"github.com/42wim/matterbridge/bridge/config"
log "github.com/sirupsen/logrus"
"strings" "strings"
"github.com/42wim/matterbridge/bridge/config"
"github.com/sirupsen/logrus"
) )
type Bridger interface { type Bridger interface {
@ -21,7 +21,7 @@ type Bridge struct {
Protocol string Protocol string
Channels map[string]config.ChannelInfo Channels map[string]config.ChannelInfo
Joined map[string]bool Joined map[string]bool
Log *log.Entry Log *logrus.Entry
Config config.Config Config config.Config
General *config.Protocol General *config.Protocol
} }
@ -29,7 +29,7 @@ type Bridge struct {
type Config struct { type Config struct {
// General *config.Protocol // General *config.Protocol
Remote chan config.Message Remote chan config.Message
Log *log.Entry Log *logrus.Entry
*Bridge *Bridge
} }

View File

@ -9,7 +9,7 @@ import (
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
prefixed "github.com/matterbridge/logrus-prefixed-formatter" prefixed "github.com/matterbridge/logrus-prefixed-formatter"
log "github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -197,12 +197,12 @@ type config struct {
} }
func NewConfig(cfgfile string) Config { func NewConfig(cfgfile string) Config {
log.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true, FullTimestamp: false}) logrus.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true, FullTimestamp: false})
flog := log.WithFields(log.Fields{"prefix": "config"}) flog := logrus.WithFields(logrus.Fields{"prefix": "config"})
viper.SetConfigFile(cfgfile) viper.SetConfigFile(cfgfile)
input, err := getFileContents(cfgfile) input, err := getFileContents(cfgfile)
if err != nil { if err != nil {
log.Fatal(err) logrus.Fatal(err)
} }
mycfg := newConfigFromString(input) mycfg := newConfigFromString(input)
if mycfg.cv.General.MediaDownloadSize == 0 { if mycfg.cv.General.MediaDownloadSize == 0 {
@ -218,7 +218,7 @@ func NewConfig(cfgfile string) Config {
func getFileContents(filename string) ([]byte, error) { func getFileContents(filename string) ([]byte, error) {
input, err := ioutil.ReadFile(filename) input, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
log.Fatal(err) logrus.Fatal(err)
return []byte(nil), err return []byte(nil), err
} }
return input, nil return input, nil
@ -236,13 +236,13 @@ func newConfigFromString(input []byte) *config {
viper.AutomaticEnv() viper.AutomaticEnv()
err := viper.ReadConfig(bytes.NewBuffer(input)) err := viper.ReadConfig(bytes.NewBuffer(input))
if err != nil { if err != nil {
log.Fatal(err) logrus.Fatal(err)
} }
cfg := &BridgeValues{} cfg := &BridgeValues{}
err = viper.Unmarshal(cfg) err = viper.Unmarshal(cfg)
if err != nil { if err != nil {
log.Fatal(err) logrus.Fatal(err)
} }
return &config{ return &config{
v: viper.GetViper(), v: viper.GetViper(),

View File

@ -11,7 +11,7 @@ import (
"unicode/utf8" "unicode/utf8"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
log "github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
func DownloadFile(url string) (*[]byte, error) { func DownloadFile(url string) (*[]byte, error) {
@ -97,7 +97,7 @@ func GetAvatar(av map[string]string, userid string, general *config.Protocol) st
return "" return ""
} }
func HandleDownloadSize(flog *log.Entry, msg *config.Message, name string, size int64, general *config.Protocol) error { func HandleDownloadSize(flog *logrus.Entry, msg *config.Message, name string, size int64, general *config.Protocol) error {
// check blacklist here // check blacklist here
for _, entry := range general.MediaDownloadBlackList { for _, entry := range general.MediaDownloadBlackList {
if entry != "" { if entry != "" {
@ -120,7 +120,7 @@ func HandleDownloadSize(flog *log.Entry, msg *config.Message, name string, size
return nil return nil
} }
func HandleDownloadData(flog *log.Entry, msg *config.Message, name, comment, url string, data *[]byte, general *config.Protocol) { func HandleDownloadData(flog *logrus.Entry, msg *config.Message, name, comment, url string, data *[]byte, general *config.Protocol) {
var avatar bool var avatar bool
flog.Debugf("Download OK %#v %#v", name, len(*data)) flog.Debugf("Download OK %#v %#v", name, len(*data))
if msg.Event == config.EventAvatarDownload { if msg.Event == config.EventAvatarDownload {

View File

@ -9,7 +9,7 @@ import (
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/bridge/helper" "github.com/42wim/matterbridge/bridge/helper"
"github.com/shazow/ssh-chat/sshd" "github.com/shazow/ssh-chat/sshd"
log "github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
type Bsshchat struct { type Bsshchat struct {
@ -131,7 +131,7 @@ func (b *Bsshchat) handleSSHChat() error {
res := strings.Split(stripPrompt(b.r.Text()), ":") res := strings.Split(stripPrompt(b.r.Text()), ":")
if res[0] == "-> Set theme" { if res[0] == "-> Set theme" {
wait = false wait = false
log.Debugf("mono found, allowing") logrus.Debugf("mono found, allowing")
continue continue
} }
if !wait { if !wait {

View File

@ -9,7 +9,7 @@ import (
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
"github.com/hashicorp/golang-lru" "github.com/hashicorp/golang-lru"
"github.com/peterhellberg/emojilib" "github.com/peterhellberg/emojilib"
log "github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
type Gateway struct { type Gateway struct {
@ -31,14 +31,14 @@ type BrMsgID struct {
ChannelID string ChannelID string
} }
var flog *log.Entry var flog *logrus.Entry
const ( const (
apiProtocol = "api" apiProtocol = "api"
) )
func New(cfg config.Gateway, r *Router) *Gateway { func New(cfg config.Gateway, r *Router) *Gateway {
flog = log.WithFields(log.Fields{"prefix": "gateway"}) flog = logrus.WithFields(logrus.Fields{"prefix": "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}
cache, _ := lru.New(5000) cache, _ := lru.New(5000)
@ -76,8 +76,8 @@ func (gw *Gateway) AddBridge(cfg *config.Bridge) error {
br.Config = gw.Router.Config br.Config = gw.Router.Config
br.General = &gw.BridgeValues().General br.General = &gw.BridgeValues().General
// set logging // set logging
br.Log = log.WithFields(log.Fields{"prefix": "bridge"}) br.Log = logrus.WithFields(logrus.Fields{"prefix": "bridge"})
brconfig := &bridge.Config{Remote: gw.Message, Log: log.WithFields(log.Fields{"prefix": br.Protocol}), Bridge: br} brconfig := &bridge.Config{Remote: gw.Message, Log: logrus.WithFields(logrus.Fields{"prefix": br.Protocol}), Bridge: br}
// add the actual bridger for this protocol to this bridge using the bridgeMap // add the actual bridger for this protocol to this bridge using the bridgeMap
br.Bridger = gw.Router.BridgeMap[br.Protocol](brconfig) br.Bridger = gw.Router.BridgeMap[br.Protocol](brconfig)
} }

View File

@ -3,12 +3,11 @@ package gateway
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"testing"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/gateway/bridgemap" "github.com/42wim/matterbridge/gateway/bridgemap"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing"
) )
var testconfig = []byte(` var testconfig = []byte(`

View File

@ -1,10 +1,10 @@
package samechannelgateway package samechannelgateway
import ( import (
"testing"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing"
) )
const testConfig = ` const testConfig = `

View File

@ -11,7 +11,7 @@ import (
"github.com/42wim/matterbridge/gateway/bridgemap" "github.com/42wim/matterbridge/gateway/bridgemap"
"github.com/google/gops/agent" "github.com/google/gops/agent"
prefixed "github.com/matterbridge/logrus-prefixed-formatter" prefixed "github.com/matterbridge/logrus-prefixed-formatter"
log "github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
var ( var (
@ -20,8 +20,8 @@ var (
) )
func main() { func main() {
log.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true, FullTimestamp: true}) logrus.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true, FullTimestamp: true})
flog := log.WithFields(log.Fields{"prefix": "main"}) flog := logrus.WithFields(logrus.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")
@ -39,9 +39,9 @@ func main() {
return return
} }
if *flagDebug || os.Getenv("DEBUG") == "1" { if *flagDebug || os.Getenv("DEBUG") == "1" {
log.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true, FullTimestamp: false, ForceFormatting: true}) logrus.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true, FullTimestamp: false, ForceFormatting: true})
flog.Info("Enabling debug") flog.Info("Enabling debug")
log.SetLevel(log.DebugLevel) logrus.SetLevel(logrus.DebugLevel)
} }
flog.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") {

View File

@ -5,7 +5,7 @@ import (
"strings" "strings"
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
log "github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
// GetChannels returns all channels we're members off // GetChannels returns all channels we're members off
@ -192,7 +192,7 @@ func (m *MMClient) UpdateChannelHeader(channelId string, header string) { //noli
m.log.Debugf("updating channelheader %#v, %#v", channelId, header) m.log.Debugf("updating channelheader %#v, %#v", channelId, header)
_, resp := m.Client.UpdateChannel(channel) _, resp := m.Client.UpdateChannel(channel)
if resp.Error != nil { if resp.Error != nil {
log.Error(resp.Error) logrus.Error(resp.Error)
} }
} }

View File

@ -12,7 +12,7 @@ import (
"github.com/jpillora/backoff" "github.com/jpillora/backoff"
prefixed "github.com/matterbridge/logrus-prefixed-formatter" prefixed "github.com/matterbridge/logrus-prefixed-formatter"
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
log "github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
type Credentials struct { type Credentials struct {
@ -55,7 +55,7 @@ type MMClient struct {
User *model.User User *model.User
Users map[string]*model.User Users map[string]*model.User
MessageChan chan *Message MessageChan chan *Message
log *log.Entry log *logrus.Entry
WsClient *websocket.Conn WsClient *websocket.Conn
WsQuit bool WsQuit bool
WsAway bool WsAway bool
@ -70,23 +70,23 @@ type MMClient struct {
func New(login, pass, team, server string) *MMClient { func New(login, pass, team, server string) *MMClient {
cred := &Credentials{Login: login, Pass: pass, Team: team, Server: server} cred := &Credentials{Login: login, Pass: pass, Team: team, Server: server}
mmclient := &MMClient{Credentials: cred, MessageChan: make(chan *Message, 100), Users: make(map[string]*model.User)} mmclient := &MMClient{Credentials: cred, MessageChan: make(chan *Message, 100), Users: make(map[string]*model.User)}
log.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true}) logrus.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true})
mmclient.log = log.WithFields(log.Fields{"prefix": "matterclient"}) mmclient.log = logrus.WithFields(logrus.Fields{"prefix": "matterclient"})
mmclient.lruCache, _ = lru.New(500) mmclient.lruCache, _ = lru.New(500)
return mmclient return mmclient
} }
func (m *MMClient) SetDebugLog() { func (m *MMClient) SetDebugLog() {
log.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true, FullTimestamp: false, ForceFormatting: true}) logrus.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true, FullTimestamp: false, ForceFormatting: true})
} }
func (m *MMClient) SetLogLevel(level string) { func (m *MMClient) SetLogLevel(level string) {
l, err := log.ParseLevel(level) l, err := logrus.ParseLevel(level)
if err != nil { if err != nil {
log.SetLevel(log.InfoLevel) logrus.SetLevel(logrus.InfoLevel)
return return
} }
log.SetLevel(l) logrus.SetLevel(l)
} }
func (m *MMClient) Login() error { func (m *MMClient) Login() error {
@ -209,7 +209,7 @@ func (m *MMClient) StatusLoop() {
} }
if m.WsConnected { if m.WsConnected {
if err := m.checkAlive(); err != nil { if err := m.checkAlive(); err != nil {
log.Errorf("Connection is not alive: %#v", err) logrus.Errorf("Connection is not alive: %#v", err)
} }
select { select {
case <-m.WsPingChan: case <-m.WsPingChan:
@ -222,7 +222,7 @@ func (m *MMClient) StatusLoop() {
m.WsQuit = false m.WsQuit = false
err := m.Login() err := m.Login()
if err != nil { if err != nil {
log.Errorf("Login failed: %#v", err) logrus.Errorf("Login failed: %#v", err)
break break
} }
if m.OnWsConnect != nil { if m.OnWsConnect != nil {