2022-11-26 20:45:32 +00:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/juju/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"git.dev.m-and-m.ovh/mderasse/gocommon/log/hooks/file"
|
|
|
|
"git.dev.m-and-m.ovh/mderasse/gocommon/log/hooks/gelf"
|
|
|
|
)
|
|
|
|
|
|
|
|
// InitLog will try to initialize logger by trying to retrieve config from multiple source.
|
2022-12-10 14:09:00 +00:00
|
|
|
func InitLog() (*logrus.Logger, error) {
|
2022-11-26 20:45:32 +00:00
|
|
|
|
|
|
|
// loading configuration
|
|
|
|
c, err := loadConfig()
|
|
|
|
if err != nil {
|
2022-12-10 14:09:00 +00:00
|
|
|
return nil, errors.Trace(err)
|
2022-11-26 20:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return initFromSource(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitLogFromCustomVaultSecret will initialize logger with a vault secret.
|
2022-12-10 14:09:00 +00:00
|
|
|
func InitLogFromCustomVaultSecret(secret string) (*logrus.Logger, error) {
|
2022-11-26 20:45:32 +00:00
|
|
|
|
|
|
|
c, err := loadConfigFromVault(secret)
|
|
|
|
if err != nil {
|
2022-12-10 14:09:00 +00:00
|
|
|
return nil, errors.Trace(err)
|
2022-11-26 20:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return initFromSource(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitLogFromCustomFile will initialize logger with a config file.
|
2022-12-10 14:09:00 +00:00
|
|
|
func InitLogFromCustomFile(path string) (*logrus.Logger, error) {
|
2022-11-26 20:45:32 +00:00
|
|
|
|
|
|
|
c, err := loadConfigFromFile(path)
|
|
|
|
if err != nil {
|
2022-12-10 14:09:00 +00:00
|
|
|
return nil, errors.Trace(err)
|
2022-11-26 20:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return initFromSource(c)
|
|
|
|
}
|
|
|
|
|
2022-12-10 14:09:00 +00:00
|
|
|
func initFromSource(c *ConfigStruct) (*logrus.Logger, error) {
|
2022-11-26 20:45:32 +00:00
|
|
|
err := c.applyEnv()
|
|
|
|
if err != nil {
|
2022-12-10 14:09:00 +00:00
|
|
|
return nil, errors.Trace(err)
|
2022-11-26 20:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.applyDefault()
|
|
|
|
|
|
|
|
return InitLogFromCustomConfig(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitLogFromCustomConfig will initialize logger from a gaven config.
|
2022-12-10 14:09:00 +00:00
|
|
|
func InitLogFromCustomConfig(c *ConfigStruct) (*logrus.Logger, error) {
|
2022-11-26 20:45:32 +00:00
|
|
|
|
|
|
|
err := c.IsValid()
|
|
|
|
if err != nil {
|
2022-12-10 14:09:00 +00:00
|
|
|
return nil, errors.Trace(err)
|
2022-11-26 20:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
level, err := logrus.ParseLevel(*c.Level)
|
|
|
|
if err != nil {
|
2022-12-10 14:09:00 +00:00
|
|
|
return nil, err
|
2022-11-26 20:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// init logger
|
|
|
|
log := logrus.New()
|
|
|
|
log.SetLevel(level)
|
|
|
|
|
|
|
|
if *c.EnableStdOut {
|
|
|
|
log.SetOutput(os.Stdout)
|
|
|
|
} else {
|
|
|
|
log.SetOutput(io.Discard)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch *c.Provider {
|
|
|
|
case ProviderName_FILE:
|
|
|
|
hook, err := file.NewHook(c.FileConfig)
|
|
|
|
if err != nil {
|
2022-12-10 14:09:00 +00:00
|
|
|
return nil, errors.Trace(err)
|
2022-11-26 20:45:32 +00:00
|
|
|
}
|
|
|
|
log.AddHook(hook)
|
|
|
|
case ProviderName_GELF:
|
|
|
|
hook, err := gelf.NewHook(c.GelfConfig)
|
|
|
|
if err != nil {
|
2022-12-10 14:09:00 +00:00
|
|
|
return nil, errors.Trace(err)
|
2022-11-26 20:45:32 +00:00
|
|
|
}
|
|
|
|
log.AddHook(hook)
|
|
|
|
case ProviderName_NONE:
|
2022-12-10 14:09:00 +00:00
|
|
|
fallthrough
|
2022-11-26 20:45:32 +00:00
|
|
|
default:
|
2022-12-10 14:09:00 +00:00
|
|
|
return nil, errors.BadRequestf("Provider is not handled.")
|
2022-11-26 20:45:32 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 14:09:00 +00:00
|
|
|
return log, nil
|
2022-11-26 20:45:32 +00:00
|
|
|
}
|