Skip to content

Conversation

@strowk
Copy link

@strowk strowk commented Oct 30, 2025

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

How to test this PR

#2887 (comment)

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 strowk requested a review from a team as a code owner October 30, 2025 18:38
@netlify
Copy link

netlify bot commented Oct 30, 2025

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit fba533f
🔍 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.

@coderabbitai
Copy link

coderabbitai bot commented Oct 30, 2025

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
Loading

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 e73e10e and fba533f.

📒 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.

@strowk strowk force-pushed the fix_default_logger branch 2 times, most recently from 0140132 to c66c912 Compare October 30, 2025 18:40
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
@strowk strowk force-pushed the fix_default_logger branch from c66c912 to 320b05e Compare October 30, 2025 18:41
@strowk strowk changed the title should print max information by default fix: should print max information by default Oct 30, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9895e2e and 320b05e.

📒 Files selected for processing (1)
  • log/logger.go (1 hunks)
🔇 Additional comments (2)
log/logger.go (2)

22-23: LGTM! Default logging behavior restored.

This change successfully restores the default logging to stderr, which addresses the regression described in #2887. The use of log.LstdFlags provides helpful timestamp context for log entries.


28-34: Fix typo in comment and confirm manual verification with issue #2887.

The logic is correct and safe—the flag check only runs during test initialization and only affects test mode. However:

  1. Fix the typo on line 28: "Disble" → "Disable"
  2. Verify this resolves the regression by testing with the instructions at #2887 (comment)

The change is well-scoped (init-time only, test-mode only) and won't affect other scenarios; 16+ existing usages of log.Default() across the codebase remain compatible.

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
log/logger.go (1)

28-28: Consider removing redundant comment.

The comment on line 28 repeats the information already stated in line 26. Since we're inside the testing.Testing() block, the context is already clear.

-		// Disable logging if explicitly disabled via -test.v=false
 		for _, arg := range os.Args {
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 320b05e and e73e10e.

📒 Files selected for processing (1)
  • log/logger.go (1 hunks)
🔇 Additional comments (2)
log/logger.go (2)

22-23: LGTM! Restores verbose logging by default.

The change from a no-op logger to an active stderr logger aligns perfectly with the PR objective to restore v0.31 behavior and addresses issue #2887 where container logs were missing on failures.


29-34: LGTM! Flag parsing logic is correct.

The implementation correctly checks for -test.v=false format, which is the only valid way to explicitly set the boolean test.v flag to false in Go. The logic appropriately disables logging only when this flag is explicitly present.

@mdelapenya mdelapenya self-assigned this Nov 3, 2025
@mdelapenya mdelapenya added the bug An issue with the library label Nov 3, 2025
@mdelapenya
Copy link
Member

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.

for _, arg := range os.Args {
if strings.EqualFold(arg, "-test.v=true") || strings.EqualFold(arg, "-v") {
defaultLogger = log.New(os.Stderr, "", log.LstdFlags)
if strings.EqualFold(arg, "-test.v=false") {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Create a public constructor for the noopLogger, or remove it and use log.New(io.Discard, "", 0)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@strowk what are your thoughts on this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easy enough - fba533f

@braaar
Copy link

braaar commented Nov 10, 2025

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

@ar-sematell
Copy link

ar-sematell commented Nov 10, 2025

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(),
	)
}

@mdelapenya
Copy link
Member

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? 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug An issue with the library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Regression]: Missing container logs when container exited

4 participants