sacrebleu-dns/utils/utils.go

78 lines
2.3 KiB
Go
Raw Normal View History

2020-12-13 04:01:04 +01:00
package utils
import (
"strings"
"time"
"github.com/mattn/go-colorable"
2020-12-28 01:34:26 +01:00
"github.com/outout14/sacrebleu-api/api/types"
2020-12-13 04:01:04 +01:00
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/snowzach/rotatefilehook"
)
//CheckErr : If fatal error, log it and panic
2020-12-13 04:01:04 +01:00
func CheckErr(err error) {
if err != nil {
log.Fatalf("%s\n ", err.Error())
}
}
//DbgErr : If basic error, log it as classic error but don't panic and keep kalm
2020-12-13 19:43:31 +01:00
func DbgErr(err error) {
if err != nil {
log.Errorf("%s\n ", err.Error())
}
}
//InitLogger : Init the logrus logger with rotateFileHook.
2020-12-14 23:20:24 +01:00
//Conf struct passed to get informations about the logger (debug or not)
2020-12-13 04:01:04 +01:00
func InitLogger(conf *Conf) {
2020-12-14 23:20:24 +01:00
var logLevel = logrus.InfoLevel //By default the level is Info.
2020-12-13 04:01:04 +01:00
if conf.AppMode != "production" { //If the configuration contains anything different than "production"; the level is set to Debug
2020-12-13 04:01:04 +01:00
logLevel = logrus.DebugLevel
}
2020-12-14 23:20:24 +01:00
rotateFileHook, err := rotatefilehook.NewRotateFileHook(rotatefilehook.RotateFileConfig{ //Rotate file hook, By default 50Mb max and 28 days retention
2020-12-13 04:01:04 +01:00
Filename: conf.App.Logdir + "/console.log",
MaxSize: 50, // megabytes
MaxBackups: 3,
MaxAge: 28, //days
Level: logLevel,
Formatter: &logrus.TextFormatter{
DisableColors: true,
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
},
})
if err != nil {
logrus.Fatalf("Failed to initialize file rotate hook: %v", err)
}
2020-12-14 23:20:24 +01:00
logrus.SetLevel(logLevel) //Set the log level
logrus.SetOutput(colorable.NewColorableStdout()) //Force colors in the Stdout
2020-12-13 04:01:04 +01:00
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: false,
FullTimestamp: true,
TimestampFormat: time.RFC822,
})
2020-12-14 23:20:24 +01:00
if conf.App.Logfile { //If file logging is enabled
2020-12-13 04:01:04 +01:00
logrus.AddHook(rotateFileHook)
}
log.WithFields(log.Fields{"app_mode": conf.AppMode}).Info("Application mode")
2020-12-13 04:01:04 +01:00
log.WithFields(log.Fields{"logLevel": logLevel}).Debug("Log level")
}
2020-12-14 23:20:24 +01:00
//Check if a reverse wildcard correspond to a record using strings.Contains
//Return bool
2020-12-28 01:34:26 +01:00
func checkReverse6(entry types.Record, result types.Record) bool {
2020-12-13 04:01:04 +01:00
check := strings.Replace(entry.Fqdn, result.Fqdn[1:], "", 1)
logrus.WithFields(logrus.Fields{"entry": entry.Fqdn, "result": result.Fqdn[1:]}).Debug("REVERSE checkReverse6 :")
logrus.Debugf("REVERSE checkReverse6 : %s", check)
2020-12-23 02:30:32 +01:00
return !strings.Contains(check, IP6arpa)
2020-12-13 04:01:04 +01:00
}