add global log to file

This commit is contained in:
Martin/Geno 2018-04-13 17:13:09 +02:00
parent 5e5b54bab9
commit a858fa649a
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
4 changed files with 68 additions and 1 deletions

View File

@ -16,6 +16,7 @@ type NotifyConfig struct {
JID string `toml:"jid"`
Password string `toml:"password"`
} `toml:"xmpp"`
File string `toml:"file"`
}
type ReceiveConfig struct {

View File

@ -1,5 +1,6 @@
package all
import (
_ "dev.sum7.eu/genofire/logmania/notify/file"
_ "dev.sum7.eu/genofire/logmania/notify/xmpp"
)

65
notify/file/main.go Normal file
View File

@ -0,0 +1,65 @@
package xmpp
import (
"os"
log "github.com/sirupsen/logrus"
"dev.sum7.eu/genofire/logmania/bot"
"dev.sum7.eu/genofire/logmania/database"
"dev.sum7.eu/genofire/logmania/lib"
"dev.sum7.eu/genofire/logmania/notify"
)
const (
proto = "file:"
)
var logger = log.WithField("notify", proto)
type Notifier struct {
notify.Notifier
formatter log.Formatter
file *os.File
path string
}
func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifier {
logger.Info("startup")
if config.File == "" {
return nil
}
file, err := os.OpenFile(config.File, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
logger.Errorf("could not open file: %s", err.Error())
return nil
}
return &Notifier{
formatter: &log.JSONFormatter{},
file: file,
path: config.File,
}
}
func (n *Notifier) Send(e *log.Entry) error {
text, err := n.formatter.Format(e)
if err != nil {
return err
}
_, err = n.file.Write(text)
if err != nil {
logger.Warnf("could not write to logfile: %s - try to reopen it", err.Error())
file, err := os.OpenFile(n.path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
}
n.file = file
_, err = n.file.Write(text)
}
return err
}
func init() {
notify.AddNotifier(Init)
}

View File

@ -28,7 +28,7 @@ type Notifier struct {
client *xmpp_client.Client
channels map[string]bool
db *database.DB
formatter *log.TextFormatter
formatter log.Formatter
}
func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifier {