From 2ce7fe8bf31f963fcf899207615e0c0db6877561 Mon Sep 17 00:00:00 2001 From: Ashutosh Bhide <37860760+abhide-tibco@users.noreply.github.com> Date: Thu, 24 Oct 2024 17:23:11 +0530 Subject: [PATCH] Read the value of logger output path from environment variable if set (#293) * Read the value of logger output path from environment variable if set * Added lowercase check for logstream value --- support/log/logger.go | 1 + support/log/zap.go | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/support/log/logger.go b/support/log/logger.go index 1c5dfd2..2d20a65 100644 --- a/support/log/logger.go +++ b/support/log/logger.go @@ -32,6 +32,7 @@ const ( EnvKeyLogSeparator = "FLOGO_LOG_SEPARATOR" DefaultLogSeparator = "\t" + EnvLogConsoleStream = "FLOGO_LOG_CONSOLE_STREAM" ) type Level int diff --git a/support/log/zap.go b/support/log/zap.go index a2060ed..658761e 100644 --- a/support/log/zap.go +++ b/support/log/zap.go @@ -4,6 +4,8 @@ import ( "fmt" "go.uber.org/zap" "go.uber.org/zap/zapcore" + "os" + "strings" ) var traceLogger *zap.SugaredLogger @@ -158,8 +160,15 @@ func newZapLogger(logFormat Format, level Level) (*zap.Logger, *zap.AtomicLevel, cfg := zap.NewProductionConfig() cfg.DisableCaller = true - cfg.OutputPaths = []string{"stdout"} - + // change the default output paths for the logger if the env variable is set and has the supported values (stderr and stdout). + // Otherwise, the logger will use the default value of stderr. + logstream, ok := os.LookupEnv(EnvLogConsoleStream) + if ok { + if strings.ToLower(logstream) == "stdout" || strings.ToLower(logstream) == "stderr" { + cfg.OutputPaths = []string{strings.ToLower(logstream)} + } + } + eCfg := cfg.EncoderConfig eCfg.TimeKey = "timestamp" eCfg.EncodeTime = zapcore.ISO8601TimeEncoder