diff --git a/commonctx/logger.go b/commonctx/logger.go index 1454671..0787435 100644 --- a/commonctx/logger.go +++ b/commonctx/logger.go @@ -8,10 +8,10 @@ import ( const LoggerKey = "mainLogger" -func AddMainLogger(ctx context.Context, logger *logrus.Logger) context.Context { +func AddMainLogger(ctx context.Context, logger *logrus.Entry) context.Context { return context.WithValue(ctx, LoggerKey, logger) } -func GetLogger(ctx context.Context) *logrus.Logger { - return ctx.Value(LoggerKey).(*logrus.Logger) +func GetLogger(ctx context.Context) *logrus.Entry { + return ctx.Value(LoggerKey).(*logrus.Entry) } diff --git a/log/config.go b/log/config.go index 044270c..1759f21 100644 --- a/log/config.go +++ b/log/config.go @@ -26,11 +26,12 @@ const defaultSecretName = "log" // ConfigStruct represent the configuration of our logger system. type ConfigStruct struct { - Level *string `yaml:"level"` - EnableStdOut *bool `yaml:"ensable_std_out"` - Providers []ProviderName `yaml:"providers"` - FileConfig *file.ConfigStruct `yaml:"file_config"` - GelfConfig *gelf.ConfigStruct `yaml:"gelf_config"` + EnableStdOut *bool `yaml:"ensable_std_out"` + ExtrasFields map[string]interface{} `yaml:"extra_fields"` + FileConfig *file.ConfigStruct `yaml:"file_config"` + GelfConfig *gelf.ConfigStruct `yaml:"gelf_config"` + Level *string `yaml:"level"` + Providers []ProviderName `yaml:"providers"` } func newDefaultConfig() *ConfigStruct { @@ -111,6 +112,18 @@ func (c *ConfigStruct) applyEnv() error { c.Providers = providers } + // Extra Fields + if v := os.Getenv(fmt.Sprintf("%s%s", envPrefix, "EXTRA_FIELDS")); v != "" { + extraFieldsPart := strings.Split(v, ",") + for _, efp := range extraFieldsPart { + extraFieldKV := strings.SplitN(efp, ":", 1) + if len(extraFieldKV) != 2 { + return errors.NotValidf(fmt.Sprintf("Invalid extra_field %s in environment variable. Should be a key1:value1,key2:value2 format", efp)) + } + c.ExtrasFields[extraFieldKV[0]] = extraFieldKV[1] + } + } + return nil } diff --git a/log/log.go b/log/log.go index bf8af93..c332c10 100644 --- a/log/log.go +++ b/log/log.go @@ -12,7 +12,7 @@ import ( ) // Init will try to initialize logger by trying to retrieve config from multiple source. -func Init() (*logrus.Logger, error) { +func Init() (*logrus.Entry, error) { // loading configuration c, err := loadConfig() @@ -24,7 +24,7 @@ func Init() (*logrus.Logger, error) { } // InitFromCustomVaultSecret will initialize logger with a vault secret. -func InitFromCustomVaultSecret(secret string) (*logrus.Logger, error) { +func InitFromCustomVaultSecret(secret string) (*logrus.Entry, error) { c, err := loadConfigFromVault(secret) if err != nil { @@ -35,7 +35,7 @@ func InitFromCustomVaultSecret(secret string) (*logrus.Logger, error) { } // InitFromCustomFile will initialize logger with a config file. -func InitFromCustomFile(path string) (*logrus.Logger, error) { +func InitFromCustomFile(path string) (*logrus.Entry, error) { c, err := loadConfigFromFile(path) if err != nil { @@ -45,7 +45,7 @@ func InitFromCustomFile(path string) (*logrus.Logger, error) { return initFromSource(c) } -func initFromSource(c *ConfigStruct) (*logrus.Logger, error) { +func initFromSource(c *ConfigStruct) (*logrus.Entry, error) { err := c.applyEnv() if err != nil { return nil, errors.Trace(err) @@ -57,7 +57,7 @@ func initFromSource(c *ConfigStruct) (*logrus.Logger, error) { } // InitFromCustomConfig will initialize logger from a gaven config. -func InitFromCustomConfig(c *ConfigStruct) (*logrus.Logger, error) { +func InitFromCustomConfig(c *ConfigStruct) (*logrus.Entry, error) { err := c.IsValid() if err != nil { @@ -100,5 +100,9 @@ func InitFromCustomConfig(c *ConfigStruct) (*logrus.Logger, error) { } } - return log, nil + return c.applyExtraFields(logrus.NewEntry(log)), nil +} + +func (c *ConfigStruct) applyExtraFields(l *logrus.Entry) *logrus.Entry { + return l.WithFields(c.ExtrasFields) }