Skip to content

proposal: testing: allow custom log headers #59311

Closed as not planned
Closed as not planned

Description

@briantkennedy

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.

https://pkg.go.dev/github.com/rs/zerolog#pkg-variables

Activity

  1. added this to the Proposal milestone on Mar 29, 2023
  2. seankhliao commented on Mar 29, 2023

    @seankhliao
    Member

    Duplicate of #28544

  3. briantkennedy commented on Mar 29, 2023

    @briantkennedy
    Author

    @seankhliao #28544 appears to be resolved by t.Helper() which isn't quite the same as what I'm asking for here. I'd like to be able to emit a timestamp to make long running tests easier to reason about. Emitting a test name would be helpful for parallel testcases where it's easy to be confused about exactly which testcase is running. Are you certain this is a duplicate?

  4. seankhliao commented on Mar 29, 2023

    @seankhliao
    Member

    see #36656 for timestamps

  5. locked and limited conversation to collaborators on Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions