-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
Problem
The Go testing library is the standard unit testing mechanism for the Go universe. The framework's logging mechanism works well for unit tests due to the short, functional nature of the environment, however, it's been adopted in end to end test environments. Long running tests typically have large volumes of logs and can run for tens of minutes which can make it quite difficult to correlate events between systems and reason about logs when parallel testing is involved.
Proposal
Add a mechanism for users to optionally configure a logging header for tests which contains timestamp, test name and other information which may be useful for human log readers.
Alternatives Considered
Require the caller to provide the header
If users need to pass the log header, this will require a fairly large effort to migrate all the callsites in a codebase. This also won't guarantee consistency across all log callsites which will be frustrating for users.
Create a testing like package specifically for large end to end tests
The Go community is very familiar with the existing testing package, it's likely not worth the fragmentation to create another testing package to allow users to add timestamps to logs.
Prior art
Go log package
https://pkg.go.dev/log allows the user to configure a pre-defined logging header through a log.SetPrefix() call. A mechanism like this would be sufficient for resolving the proposal and maintain backward compatibility.
Uber Zap Logger
Zap provides a mechanism to customize logging by setting configuration struct members. Of note, setting TimeKey configures the timestamp formatting (or omits it if emtpy)
https://pkg.go.dev/go.uber.org/zap@v1.24.0/zapcore#EncoderConfig
Zerolog
Zerolog allows the user to configure the timestamp in the log header by setting a global variable in the zerolog pacakage.