Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 10e3ed137507d389ea5e782c9628df29139f38aa Author: z3bra <contact@z3bra.org> Date: Sat Jul 18 15:46:19 2020 +0000 Add an option to log into a file rather than stdout (#1168) Use Logfile option in the `[general]` section diff --git a/bridge/config/config.go b/bridge/config/config.go index d98c942..6e99066 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -36 +37 @@ package config  import (   "bytes"   "io/ioutil" + "os"   "path/filepath"   "strings"   "sync" @@ -936 +947 @@ type Protocol struct {   JoinDelay string // all protocols   Label string // all protocols   Login string // mattermost, matrix + LogFile string // general   MediaDownloadBlackList []string   MediaDownloadPath string // Basically MediaServerUpload, but instead of uploading it, just write it to a file on the same server.   MediaDownloadSize int // all protocols @@ -2476 +24915 @@ func NewConfig(rootLogger *logrus.Logger, cfgfile string) Config {     cfgtype := detectConfigType(cfgfile)   mycfg := newConfigFromString(logger, input, cfgtype) + if mycfg.cv.General.LogFile != "" { + logfile, err := os.OpenFile(mycfg.cv.General.LogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600) + if err == nil { + logger.Info("Opening log file ", mycfg.cv.General.LogFile) + rootLogger.Out = logfile + } else { + logger.Warn("Failed to open ", mycfg.cv.General.LogFile) + } + }   if mycfg.cv.General.MediaDownloadSize == 0 {   mycfg.cv.General.MediaDownloadSize = 1000000   } diff --git a/matterbridge.go b/matterbridge.go index 7b04182..63fd12f 100644 --- a/matterbridge.go +++ b/matterbridge.go @@ -516 +5115 @@ func main() {   cfg := config.NewConfig(rootLogger, *flagConfig)   cfg.BridgeValues().General.Debug = *flagDebug   + // if logging to a file, ensure it is closed when the program terminates + // nolint:errcheck + defer func() { + if f, ok := rootLogger.Out.(*os.File); ok { + f.Sync() + f.Close() + } + }() +   r, err := gateway.NewRouter(rootLogger, cfg, bridgemap.FullMap)   if err != nil {   logger.Fatalf("Starting gateway failed: %s", err) diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index 975078c..156e1f2 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -16046 +160414 @@ MediaDownloadBlacklist=[".html$",".htm$"]  #OPTIONAL (default false)  IgnoreFailureOnStart=false   +#LogFile defines the location of a file to write logs into, rather +#than stdout. +#Logging will still happen on stdout if the file cannot be open for +#writing, or if the value is empty. Note that the log won't roll, so +#you might want to use logrotate(8) with this feature. +#OPTIONAL (default empty) +LogFile=/var/log/matterbridge.log +  ###################################################################  #Tengo configuration  ###################################################################