Skip to content

Add LogConsumers.lines helper to consume container logs line by line - #1661

Open
rkkims wants to merge 10 commits into
testcontainers:mainfrom
rkkims:log-consumers-lines
Open

rkkims wants to merge 10 commits into
testcontainers:mainfrom
rkkims:log-consumers-lines

Conversation

@rkkims

@rkkims rkkims commented Nov 19, 2026

Copy link
Copy Markdown

Summary

Implements line-by-line log consumer helper #1544, e.g. withLogConsumer(LogConsumers.lines(line => console.log(line))). The choice of a helper is intentional to avoid adding a new method to the Container class. Existing code, including withLogConsumer, is unchanged.
The existing byline dependency was not used: by default, byline drops empty lines. It has an option keepEmptyLines to keep empty lines, but it adds a fake empty line at the end of the stream. Java's withLogConsumer(Consumer<OutputFrame>) and Go's LogConsumer.Accept(Log) receive one log entry per call instead of a raw stream. LogConsumers.lines gives Node a similar experience without changing withLogConsumer.

Verification

Notes

  • toConsole: noConsole setting in biome prevents the console output. biome-ignore is not allowed by AGENTS.md. Would you accept a noConsole exception for log-consumers.ts, or would you prefer it left out?
  • AI disclosure: I wrote the code and this PR myself. I got help from Claude to understand the codebase and review my code and PR draft.

Suggested labels: enhancement, minor
Refs #1544

@cristianrgreco cristianrgreco left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @rkkims, this looks good.

On toConsole: I'd leave it out. LogConsumers.lines(console.log), or LogConsumers.lines((line) => console.log(`[redis] ${line}`)) for a prefix, already covers it in one line. That isn't worth a noConsole exception in library code.

Nits inline.

import { LogConsumers } from "./log-consumers";

describe("LogConsumers.lines", () => {
it("should call fn once per line when one chunk holds two lines", async () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

These five tests differ only in what they write and what they expect. An it.each table of { chunks, expected } would make each case one line and keep the setup in one place.


await using _container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withLogConsumer(LogConsumers.lines((line) => received.push(line)))
.withEntrypoint(["/bin/sh", "-c", `echo a; echo b; echo c; sleep infinity`])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This template literal has nothing to interpolate, so a plain string will do.

@cristianrgreco cristianrgreco added enhancement New feature or request minor Backward compatible functionality labels Nov 20, 2026
@rkkims

rkkims commented Nov 20, 2026

Copy link
Copy Markdown
Author

Thanks for the review. Okay, I'll leave toConsole out. Both nits are done.

This branch has not been deployed

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

Labels

enhancement New feature or request minor Backward compatible functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants