Skip to content

Commit

Permalink
feat: Add support to enable FullTimeStamp in logging (#15127)
Browse files Browse the repository at this point in the history
* Add support to enable FullTimeStamp in logging
Signed-off-by: skalinov <skalinov@tradingview.com>

* fix: Fix go linter file exist issue
Signed-off-by: skalinov <skalinov@tradingview.com>

* fix: Remove --skip-pkg-cache
Signed-off-by: skalinov <skalinov@tradingview.com>

* Update util/log/logrus_test.go

Use custom set env for prevent linter to be failed

Signed-off-by: pasha-codefresh <pavel@codefresh.io>

* Update common/common.go

Signed-off-by: Dan Garfield <dan@codefresh.io>

* Update util/log/logrus_test.go

Signed-off-by: pasha-codefresh <pavel@codefresh.io>

* Update util/log/logrus_test.go

remove os import

Signed-off-by: pasha-codefresh <pavel@codefresh.io>

* Update util/log/logrus_test.go

sort dependencies

Signed-off-by: pasha-codefresh <pavel@codefresh.io>

* fix formatting

Signed-off-by: pashakostohrys <pavel@codefresh.io>

---------

Signed-off-by: pasha-codefresh <pavel@codefresh.io>
Signed-off-by: Dan Garfield <dan@codefresh.io>
Signed-off-by: pashakostohrys <pavel@codefresh.io>
Co-authored-by: skalinov <skalinov@tradingview.com>
Co-authored-by: pasha-codefresh <pavel@codefresh.io>
Co-authored-by: Dan Garfield <dan@codefresh.io>
  • Loading branch information
4 people authored Mar 13, 2024
1 parent 3b8f673 commit f0b0307
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
2 changes: 2 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ const (
EnvLogFormat = "ARGOCD_LOG_FORMAT"
// EnvLogLevel log level that is defined by `--loglevel` option
EnvLogLevel = "ARGOCD_LOG_LEVEL"
// EnvLogFormatEnableFullTimestamp enables the FullTimestamp option in logs
EnvLogFormatEnableFullTimestamp = "ARGOCD_LOG_FORMAT_ENABLE_FULL_TIMESTAMP"
// EnvMaxCookieNumber max number of chunks a cookie can be broken into
EnvMaxCookieNumber = "ARGOCD_MAX_COOKIE_NUMBER"
// EnvPluginSockFilePath allows to override the pluginSockFilePath for repo server and cmp server
Expand Down
19 changes: 14 additions & 5 deletions util/log/logrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ func CreateFormatter(logFormat string) logrus.Formatter {
case JsonFormat:
formatType = &logrus.JSONFormatter{}
case TextFormat:
if os.Getenv("FORCE_LOG_COLORS") == "1" {
formatType = &logrus.TextFormatter{ForceColors: true}
} else {
formatType = &logrus.TextFormatter{}
formatType = &logrus.TextFormatter{
ForceColors: checkForceLogColors(),
FullTimestamp: checkEnableFullTimestamp(),
}
default:
formatType = &logrus.TextFormatter{}
formatType = &logrus.TextFormatter{
FullTimestamp: checkEnableFullTimestamp(),
}
}

return formatType
Expand All @@ -57,3 +58,11 @@ func createLogLevel() logrus.Level {
}
return level
}

func checkForceLogColors() bool {
return strings.ToLower(os.Getenv("FORCE_LOG_COLORS")) == "1"
}

func checkEnableFullTimestamp() bool {
return strings.ToLower(os.Getenv(common.EnvLogFormatEnableFullTimestamp)) == "1"
}
12 changes: 12 additions & 0 deletions util/log/logrus_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package log

import (
"fmt"
"testing"

"github.com/argoproj/argo-cd/v2/common"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
Expand All @@ -23,6 +25,16 @@ func TestCreateFormatter(t *testing.T) {
result := CreateFormatter("text")
assert.Equal(t, &logrus.TextFormatter{}, result)
})
t.Run(fmt.Sprintf("%s == 1", common.EnvLogFormatEnableFullTimestamp), func(t *testing.T) {
t.Setenv(common.EnvLogFormatEnableFullTimestamp, "1")
result := CreateFormatter("text")
assert.Equal(t, &logrus.TextFormatter{FullTimestamp: true}, result)
})
t.Run(fmt.Sprintf("%s != 1", common.EnvLogFormatEnableFullTimestamp), func(t *testing.T) {
t.Setenv(common.EnvLogFormatEnableFullTimestamp, "0")
result := CreateFormatter("text")
assert.Equal(t, &logrus.TextFormatter{}, result)
})
})
t.Run("log format is not json or text", func(t *testing.T) {
result := CreateFormatter("xml")
Expand Down

0 comments on commit f0b0307

Please sign in to comment.