[#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 <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2025-01-10 14:04:44 +03:00
parent 829777bd53
commit 8f9c02253c
Signed by: fyrchik
SSH key fingerprint: SHA256:m/TTwCzjnRkXgnzEx9X92ccxy1CcVeinOgDb3NPWWmg
2 changed files with 20 additions and 2 deletions

View file

@ -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}

View file

@ -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