feat(logger): Add an utils func to add common env variables to logger field
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:
parent
5a0beb9e2c
commit
e6b75e5348
27
log/utils.go
Normal file
27
log/utils.go
Normal 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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user