feat(log): Working on log library
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Matthieu 'JP' DERASSE
2022-11-26 20:45:32 +00:00
parent 25ce5323a4
commit 5591da9c47
11 changed files with 952 additions and 2 deletions

32
log/hooks/gelf/config.go Normal file
View File

@ -0,0 +1,32 @@
package gelf
import (
"strconv"
"github.com/asaskevich/govalidator"
"github.com/juju/errors"
)
// ConfigStruct is the configuration for GELF Provider.
type ConfigStruct struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
ExtrasFields map[string]interface{} `yaml:"extra_fields"`
}
// IsValid will check that the Gelf configuration is valid.
func (c *ConfigStruct) IsValid() error {
if c.Host == "" {
return errors.NotValidf("Host is empty in GELF configuration")
} else if !govalidator.IsDNSName(c.Host) && !govalidator.IsIP(c.Host) {
return errors.NotValidf("Host is invalid in GELF configuration. Should be a DNS name or an IP")
}
if c.Port == 0 {
return errors.NotValidf("Port is empty in GELF configuration")
} else if !govalidator.IsPort(strconv.Itoa(c.Port)) {
return errors.NotValidf("Port is invalid in GELF configuration")
}
return nil
}

28
log/hooks/gelf/hook.go Normal file
View File

@ -0,0 +1,28 @@
package gelf
import (
"fmt"
"github.com/juju/errors"
"github.com/sirupsen/logrus"
graylog "github.com/gemnasium/logrus-graylog-hook/v3"
)
// NewHook will create a Gelf Hook for logrus.
func NewHook(c *ConfigStruct) (logrus.Hook, error) {
err := c.IsValid()
if err != nil {
return nil, errors.Trace(err)
}
hook := graylog.NewAsyncGraylogHook(
fmt.Sprintf("%s:%d", c.Host, c.Port),
c.ExtrasFields,
)
defer hook.Flush()
return hook, nil
}