2023-01-10 22:36:42 +00:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2023-01-29 15:56:09 +00:00
|
|
|
// list of common environment variables.
|
2023-01-10 22:36:42 +00:00
|
|
|
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
|
|
|
|
}
|