feat(logger): Add an utils func to add common env variables to logger field
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Matthieu 'JP' DERASSE 2023-01-10 22:36:42 +00:00
parent 5a0beb9e2c
commit e6b75e5348
Signed by: mderasse
GPG Key ID: 55141C777B16A705

27
log/utils.go Normal file
View File

@ -0,0 +1,27 @@
package log
import (
"fmt"
"os"
"github.com/sirupsen/logrus"
)
// list of common environment variables
var commonEnvs = []string{"APP_NAME", "APP_VERSION"}
// AddCommonFieldsFromEnv will analyze "common" environment variable and will add those
// as fields on a logrus.Entry.
func AddCommonFieldsFromEnv(l *logrus.Entry) (*logrus.Entry, error) {
if l == nil {
return nil, fmt.Errorf("missing logrus entry")
}
for _, envKey := range commonEnvs {
if v := os.Getenv(envKey); v != "" {
l = logrus.WithField(envKey, v)
}
}
return l, nil
}