From 8f9c02253cc3e73b520c12a98a7b00893d9364bf Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 10 Jan 2025 14:04:44 +0300 Subject: [PATCH] [#188] logging: Allow to print date in log messages `timeonly`: ``` INFO[14:05:21] kek endpoint=1 ``` `datetime`: ``` INFO[2025-01-10 14:03:58] kek endpoint=1 ``` `none` ``` INFO kek endpoint=1 ``` Signed-off-by: Evgenii Stratonikov --- internal/logging/logging.go | 21 +++++++++++++++++++-- scenarios/run_scenarios.md | 1 + 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/internal/logging/logging.go b/internal/logging/logging.go index 41632d3..c3c3827 100644 --- a/internal/logging/logging.go +++ b/internal/logging/logging.go @@ -1,6 +1,8 @@ package logging import ( + "fmt" + "strings" "time" "github.com/dop251/goja" @@ -57,14 +59,29 @@ func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { return &Logging{vu: vu} } + tsFormat, disableTs := time.TimeOnly, false + if val, ok := vu.InitEnv().LookupEnv("DATE_FORMAT"); ok { + switch strings.ToLower(val) { + case "timeonly": + case "datetime": + tsFormat = time.DateTime + case "none": + disableTs = true + default: + panic(fmt.Sprintf("invalid value for DATE_FORMAT: %s (should be `timeonly`, `datetime` or `none`)", val)) + } + } + format := lg.Formatter switch f := format.(type) { case *logrus.TextFormatter: f.ForceColors = true f.FullTimestamp = true - f.TimestampFormat = time.TimeOnly + f.TimestampFormat = tsFormat + f.DisableTimestamp = disableTs case *logrus.JSONFormatter: - f.TimestampFormat = time.TimeOnly + f.TimestampFormat = tsFormat + f.DisableTimestamp = disableTs } return &Logging{vu: vu} diff --git a/scenarios/run_scenarios.md b/scenarios/run_scenarios.md index 091adc9..144793c 100644 --- a/scenarios/run_scenarios.md +++ b/scenarios/run_scenarios.md @@ -21,6 +21,7 @@ Scenarios `grpc.js`, `local.js`, `http.js` and `s3.js` support the following opt * `PAYLOAD_TYPE` - type of an object payload ("random" or "text", default: "random"). * `STREAMING` - if set, the payload is generated on the fly and is not read into memory fully. * `METRIC_TAGS` - custom metrics tags (format `tag1:value1;tag2:value2`). + * `DATE_FORMAT` - custom datetime format: `timeonly` (default), `datetime` or `none`. Additionally, the profiling extension can be enabled to generate CPU and memory profiles which can be inspected with `go tool pprof file.prof`: ```shell