diff options
author | Wim <wim@42.be> | 2019-02-23 22:51:27 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-23 22:51:27 +0100 |
commit | bf21604d425b4feb1b95e4e94643e7a658eeea90 (patch) | |
tree | 376741188b9646ef66526b36e24d3577f07a678f /matterbridge.go | |
parent | 1bb39eba8717f62336cc98c5bb7cfbef194f3626 (diff) | |
download | matterbridge-msglm-bf21604d425b4feb1b95e4e94643e7a658eeea90.tar.gz matterbridge-msglm-bf21604d425b4feb1b95e4e94643e7a658eeea90.tar.bz2 matterbridge-msglm-bf21604d425b4feb1b95e4e94643e7a658eeea90.zip |
Make all loggers derive from non-default instance (#728)
Diffstat (limited to 'matterbridge.go')
-rw-r--r-- | matterbridge.go | 73 |
1 files changed, 48 insertions, 25 deletions
diff --git a/matterbridge.go b/matterbridge.go index 0dac8af5..6c6b11fe 100644 --- a/matterbridge.go +++ b/matterbridge.go @@ -17,46 +17,69 @@ import ( var ( version = "1.14.0-dev" githash string + + flagConfig = flag.String("conf", "matterbridge.toml", "config file") + flagDebug = flag.Bool("debug", false, "enable debug") + flagVersion = flag.Bool("version", false, "show version") + flagGops = flag.Bool("gops", false, "enable gops agent") ) func main() { - logrus.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true, FullTimestamp: true}) - flog := logrus.WithFields(logrus.Fields{"prefix": "main"}) - flagConfig := flag.String("conf", "matterbridge.toml", "config file") - flagDebug := flag.Bool("debug", false, "enable debug") - flagVersion := flag.Bool("version", false, "show version") - flagGops := flag.Bool("gops", false, "enable gops agent") flag.Parse() + if *flagVersion { + fmt.Printf("version: %s %s\n", version, githash) + return + } + + rootLogger := setupLogger() + logger := rootLogger.WithFields(logrus.Fields{"prefix": "main"}) + if *flagGops { if err := agent.Listen(agent.Options{}); err != nil { - flog.Errorf("failed to start gops agent: %#v", err) + logger.Errorf("Failed to start gops agent: %#v", err) } else { defer agent.Close() } } - if *flagVersion { - fmt.Printf("version: %s %s\n", version, githash) - return - } - if *flagDebug || os.Getenv("DEBUG") == "1" { - logrus.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true, FullTimestamp: false, ForceFormatting: true}) - flog.Info("Enabling debug") - logrus.SetLevel(logrus.DebugLevel) - } - flog.Printf("Running version %s %s", version, githash) + + logger.Printf("Running version %s %s", version, githash) if strings.Contains(version, "-dev") { - flog.Println("WARNING: THIS IS A DEVELOPMENT VERSION. Things may break.") + logger.Println("WARNING: THIS IS A DEVELOPMENT VERSION. Things may break.") } - cfg := config.NewConfig(*flagConfig) + + cfg := config.NewConfig(rootLogger, *flagConfig) cfg.BridgeValues().General.Debug = *flagDebug - r, err := gateway.NewRouter(cfg, bridgemap.FullMap) + + r, err := gateway.NewRouter(rootLogger, cfg, bridgemap.FullMap) if err != nil { - flog.Fatalf("Starting gateway failed: %s", err) + logger.Fatalf("Starting gateway failed: %s", err) } - err = r.Start() - if err != nil { - flog.Fatalf("Starting gateway failed: %s", err) + if err = r.Start(); err != nil { + logger.Fatalf("Starting gateway failed: %s", err) } - flog.Printf("Gateway(s) started succesfully. Now relaying messages") + logger.Printf("Gateway(s) started succesfully. Now relaying messages") select {} } + +func setupLogger() *logrus.Logger { + logger := &logrus.Logger{ + Out: os.Stdout, + Formatter: &prefixed.TextFormatter{ + PrefixPadding: 13, + DisableColors: true, + FullTimestamp: true, + }, + Level: logrus.InfoLevel, + } + if *flagDebug || os.Getenv("DEBUG") == "1" { + logger.Formatter = &prefixed.TextFormatter{ + PrefixPadding: 13, + DisableColors: true, + FullTimestamp: false, + ForceFormatting: true, + } + logger.Level = logrus.DebugLevel + logger.WithFields(logrus.Fields{"prefix": "main"}).Info("Enabling debug logging.") + } + return logger +} |