logmania/output/file/main.go

108 lines
2.3 KiB
Go
Raw Permalink Normal View History

2018-05-18 14:04:54 +02:00
package file
import (
"os"
"path"
"regexp"
2019-06-20 09:25:43 +02:00
"github.com/bdlm/log"
2018-09-05 01:53:23 +02:00
"github.com/mitchellh/mapstructure"
2018-05-18 14:04:54 +02:00
2019-08-08 15:51:55 +02:00
"dev.sum7.eu/sum7/logmania/bot"
"dev.sum7.eu/sum7/logmania/database"
"dev.sum7.eu/sum7/logmania/output"
2018-05-18 14:04:54 +02:00
)
const (
proto = "file"
)
2018-09-05 01:53:23 +02:00
var logger = log.WithField("output", proto)
2018-05-18 14:04:54 +02:00
2018-09-05 01:53:23 +02:00
type Output struct {
output.Output
defaults []*database.Notify
2018-05-18 14:04:54 +02:00
files map[string]*os.File
formatter log.Formatter
path string
}
2018-09-05 01:53:23 +02:00
type OutputConfig struct {
Directory string `mapstructure:"directory"`
Default string `mapstructure:"default"`
}
func Init(configInterface interface{}, db *database.DB, bot *bot.Bot) output.Output {
var config OutputConfig
if err := mapstructure.Decode(configInterface, &config); err != nil {
logger.Warnf("not able to decode data: %s", err)
return nil
}
if config.Directory == "" {
2018-05-18 14:04:54 +02:00
return nil
}
2018-09-05 01:53:23 +02:00
logger.WithField("directory", config.Directory).Info("startup")
var defaults []*database.Notify
2018-09-05 01:53:23 +02:00
if config.Default != "" {
defaults = append(defaults, &database.Notify{
2018-09-11 20:20:58 +02:00
Protocol: proto,
To: config.Default,
RegexIn: make(map[string]*regexp.Regexp),
MaxPrioIn: log.DebugLevel,
})
}
2018-05-18 14:04:54 +02:00
2018-09-05 01:53:23 +02:00
return &Output{
2019-06-20 14:47:53 +02:00
defaults: defaults,
files: make(map[string]*os.File),
formatter: &log.JSONFormatter{
DisableCaller: true,
},
path: config.Directory,
2018-05-18 14:04:54 +02:00
}
}
2018-09-05 01:53:23 +02:00
func (out *Output) Default() []*database.Notify {
return out.defaults
}
2018-09-05 01:53:23 +02:00
func (out *Output) getFile(name string) *os.File {
if file, ok := out.files[name]; ok {
2018-05-18 14:04:54 +02:00
return file
}
if m, err := regexp.MatchString(`^[0-9A-Za-z_-]*$`, name); err != nil || !m {
logger.Errorf("not allowed to use '%s:%s'", proto, name)
return nil
}
2018-09-05 01:53:23 +02:00
filename := path.Join(out.path, name+".json")
2018-05-18 14:04:54 +02:00
file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
logger.Errorf("could not open file: %s", err.Error())
return nil
}
2018-09-05 01:53:23 +02:00
out.files[name] = file
2018-05-18 14:04:54 +02:00
return file
}
2018-09-05 01:53:23 +02:00
func (out *Output) Send(e *log.Entry, to *database.Notify) bool {
2018-05-18 14:04:54 +02:00
if to.Protocol != proto {
return false
}
2018-09-05 01:53:23 +02:00
byteText, err := out.formatter.Format(e)
2018-05-18 14:04:54 +02:00
if err != nil {
return false
}
text := to.RunReplace(string(byteText))
2018-09-05 01:53:23 +02:00
file := out.getFile(to.To)
2018-05-18 14:04:54 +02:00
if file == nil {
return false
}
_, err = file.WriteString(text)
return err == nil
}
func init() {
2018-09-05 01:53:23 +02:00
output.Add(proto, Init)
2018-05-18 14:04:54 +02:00
}