fix: should print max information by default
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.
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...Use your smartphone camera to open QR code link. |
To edit notification comments on pull requests, go to your Netlify project configuration.
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=falseinos.Args(edge cases, ordering). - Any tests or CI expecting prior no-op behavior.
- Ensure no unintended stderr noise in common workflows.
- Correct detection of
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.
Comment @coderabbitai help to get the list of available commands and usage tips.
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.
I agree with @strowk. The logs from testcontainers are noise while tests pass, but useful when tests fail.
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(),
)
}
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.Tcommon funcLogf(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? π