testcontainers-go icon indicating copy to clipboard operation
testcontainers-go copied to clipboard

fix: should print max information by default

Open strowk opened this issue 1 month ago β€’ 6 comments

When tests do not fail, what they printed rarely matters. However, when they do fail, it is important to see maximum information. This change switches default behavior back as was in 0.31, when failures could be quickly triaged without adding -v

label: fix, qol

What does this PR do?

Simply switches default logging behavior to more convenient one in realworld scenarios.

Why is it important?

Test output matters when we need to read it, we read it when tests fail, otherwise we might not even care if they run (if it is some pipeline).

Related issues

  • Closes #2887

How to test this PR

https://github.com/testcontainers/testcontainers-go/issues/2887#issuecomment-3469359308

Ideally maybe it should buffer logs and print them out only when failure happened. This could affect memory usage though, so IMO defaulting to complete output would be preferred behavior in majority of the cases.

strowk avatar Oct 30 '25 18:10 strowk

Deploy Preview for testcontainers-go ready!

Name Link
Latest commit fba533ff23797b787a68bc8996a418827e419f95
Latest deploy log https://app.netlify.com/projects/testcontainers-go/deploys/690fa174bf76ce000861d614
Deploy Preview https://deploy-preview-3459--testcontainers-go.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

netlify[bot] avatar Oct 30 '25 18:10 netlify[bot]

Summary by CodeRabbit

  • Bug Fixes
    • Fixed default logging to properly write to stderr instead of being disabled.
    • Updated logging behavior in test environments to respect explicit disable flags.

Walkthrough

Default logger changed from a no-op to a real stderr logger; a new exported NewNoopLogger() was added; Init() now only disables logging when the explicit -test.v=false flag is present and comments were updated accordingly.

Changes

Cohort / File(s) Summary
Logger initialization & API
log/logger.go
Replaced defaultLogger noop with log.New(os.Stderr, "", log.LstdFlags); added func NewNoopLogger() Logger; updated Init() to detect explicit -test.v=false to disable logging and revised comments.

Sequence Diagram(s)

sequenceDiagram
    participant App
    participant LoggerPkg as log/logger.go
    participant Stderr

    App->>LoggerPkg: Init()
    LoggerPkg->>LoggerPkg: inspect os.Args for "-test.v=false"
    alt explicit "-test.v=false"
        LoggerPkg->>LoggerPkg: set defaultLogger = NewNoopLogger()
    else
        LoggerPkg->>Stderr: create real logger (stderr, LstdFlags)
        LoggerPkg->>LoggerPkg: set defaultLogger = real logger
    end
    LoggerPkg-->>App: Init complete

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Single-file change but affects logging behavior at runtime and in tests.
  • Pay attention to:
    • Correct detection of -test.v=false in os.Args (edge cases, ordering).
    • Any tests or CI expecting prior no-op behavior.
    • Ensure no unintended stderr noise in common workflows.

Poem

🐰 I swapped my hush for stderr light,
I added a quiet hat for night.
If tests say β€œno” with flag in view,
I hop back silentβ€”just for you. πŸ₯•

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
βœ… Passed checks (4 passed)
Check name Status Explanation
Title check βœ… Passed The title accurately describes the main change: switching default logging behavior to print more information by default, which directly addresses the regression issue.
Description check βœ… Passed The description clearly explains the motivation for the change, relates it to prior behavior in v0.31, and references the linked issue #2887 about missing container logs.
Linked Issues check βœ… Passed The code changes directly address the linked issue #2887 by restoring the default logger to write to stderr instead of using a noop logger, which enables container logs to be printed on failures.
Out of Scope Changes check βœ… Passed All changes are directly related to the stated objective of restoring default logging behavior. The helper function NewNoopLogger() is a supporting addition for cases where noop logging is explicitly needed.
✨ Finishing touches
  • [ ] πŸ“ Generate docstrings
πŸ§ͺ Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

πŸ“œ Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between e73e10e90830c0510a795d672c5a6a4cc31bb168 and fba533ff23797b787a68bc8996a418827e419f95.

πŸ“’ Files selected for processing (1)
  • log/logger.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • log/logger.go
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Redirect rules - testcontainers-go
  • GitHub Check: Header rules - testcontainers-go
  • GitHub Check: Pages changed - testcontainers-go

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Oct 30 '25 18:10 coderabbitai[bot]

Hi @strowk I asked in #1984 about feedback, although I understand what you described in the issue and PR. This is key to me:

Test output matters when we need to read it, we read it when tests fail,

I'm fine in merging the changes proposed in this PR, I just want some alignment in the community about it.

mdelapenya avatar Nov 03 '25 13:11 mdelapenya

I agree with @strowk. The logs from testcontainers are noise while tests pass, but useful when tests fail.

braaar avatar Nov 10 '25 13:11 braaar

I agree with @strowk. The logs from testcontainers are noise while tests pass, but useful when tests fail.

It would be helpful to have an easy way to set the testing.T common func Logf(format string, args ...any) as a logger for the test containers, this way they would only print if the test failed.

type testLogger interface {
	Logf(format string, args ...any)
}

type testLoggerWrapper struct {
	testLogger
}

func (w testLoggerWrapper) Printf(format string, v ...any) {
	w.Logf(format, v...)
}

func WithTestLogger(t testLogger)LoggerOption {
	return WithLogger(testLoggerWrapper{t})
}

using with

func runNewPostgres(t *testing.T) (*testcontainersPostgres.PostgresContainer, error) {
	return testcontainersPostgres.Run(
		t.Context(),
		postgresImage,
		testcontainers.WithTestLogger(t),
		testcontainersPostgres.WithDatabase(PostgresDB),
		testcontainersPostgres.WithUsername(PostgresUser),
		testcontainersPostgres.WithPassword(PostgresPassword),
		testcontainersPostgres.BasicWaitStrategies(),
	)
}

ar-sematell avatar Nov 10 '25 13:11 ar-sematell

I agree with @strowk. The logs from testcontainers are noise while tests pass, but useful when tests fail.

It would be helpful to have an easy way to set the testing.T common func Logf(format string, args ...any) as a logger for the test containers, this way they would only print if the test failed.

@ar-sematell I think this could be a nice addition. Could you send this in a different PR? πŸ™

mdelapenya avatar Nov 19 '25 11:11 mdelapenya