feat(log): Working on log library
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
32
log/hooks/gelf/config.go
Normal file
32
log/hooks/gelf/config.go
Normal 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
28
log/hooks/gelf/hook.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user