Skip to content

Commit

Permalink
Add init func to set env variable fields and their values into logs (#…
Browse files Browse the repository at this point in the history
…481)

* set env variables from host's env into logs

* address review suggestion

* nit:align the code after resloving merge conflict.
  • Loading branch information
SupriyaKasten authored and mergify[bot] committed Jan 9, 2020
1 parent cae7f57 commit 910cc2e
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"os"
"strings"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -72,6 +73,23 @@ func SetOutput(sink OutputSink) error {
}
}

var envVarFields field.Fields

// initEnvVarFields populates envVarFields with values from the host's environment.
func initEnvVarFields() {
envVars := []string{
"HOSTNAME",
"CLUSTER_NAME",
"SERVICE_NAME",
"VERSION",
}
for _, e := range envVars {
if ev, ok := os.LookupEnv(e); ok {
envVarFields = field.Add(envVarFields, strings.ToLower(e), ev)
}
}
}

// OutputFormat sets the output data format.
type OutputFormat uint8

Expand All @@ -98,6 +116,7 @@ func SetFormatter(format OutputFormat) {

func init() {
SetFormatter(TextFormat)
initEnvVarFields()
}

func Info() Logger {
Expand Down Expand Up @@ -134,6 +153,10 @@ func WithError(err error) Logger {
func (l *logger) Print(msg string, fields ...field.M) {
logFields := make(logrus.Fields)

for _, f := range envVarFields.Fields() {
logFields[f.Key()] = f.Value()
}

frame := caller.GetFrame(3)
logFields["Function"] = frame.Function
logFields["File"] = frame.File
Expand Down

0 comments on commit 910cc2e

Please sign in to comment.