Skip to content

Commit 7594970

Browse files
authored
feat: add docker container log streaming (#314)
* add docker container log streaming * add tokio feature * log level management
1 parent 9f143d8 commit 7594970

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

.github/workflows/test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,5 @@ jobs:
9898
--success-output never \
9999
--verbose
100100
env:
101-
RUST_LOG: debug
101+
RUST_LOG: trace
102102
RUST_BACKTRACE: full

tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ alloy-network = { workspace = true }
1313
alloy-provider = { workspace = true, features = ["reqwest", "ws"] }
1414
alloy-rpc-types-eth = { workspace = true }
1515
scroll-alloy-network = { workspace = true }
16-
tokio = { workspace = true, features = ["rt", "time"] }
16+
tokio = { workspace = true, features = ["rt", "time", "process"] }
1717
eyre = { workspace = true }
1818
getrandom = { workspace = true }
1919
tracing = { workspace = true }

tests/docker-compose.test.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
services:
22
l1-node:
33
image: ghcr.io/foundry-rs/foundry:latest
4-
container_name: l1-node
54
entrypoint: [ "bash", "/launch_l1.bash" ]
65
ports:
76
- "8544:8545"
@@ -18,7 +17,6 @@ services:
1817
build:
1918
context: ../
2019
dockerfile: Dockerfile.test
21-
container_name: rollup-node-sequencer
2220
entrypoint: ["bash", "/launch_rollup_node_sequencer.bash"]
2321
environment:
2422
- RUST_LOG=info
@@ -37,7 +35,6 @@ services:
3735
build:
3836
context: ../
3937
dockerfile: Dockerfile.test
40-
container_name: rollup-node-follower
4138
entrypoint: ["bash", "/launch_rollup_node_follower.bash"]
4239
environment:
4340
- RUST_LOG=info
@@ -55,7 +52,6 @@ services:
5552
l2geth-sequencer:
5653
image: scrolltech/l2geth:scroll-v5.9.4
5754
platform: linux/amd64
58-
container_name: l2geth-sequencer
5955
entrypoint: ["bash", "/launch_l2geth.bash"]
6056
ports:
6157
- "8547:8545" # JSON-RPC
@@ -71,7 +67,6 @@ services:
7167
l2geth-follower:
7268
image: scrolltech/l2geth:scroll-v5.9.4
7369
platform: linux/amd64
74-
container_name: l2geth-follower
7570
entrypoint: ["bash", "/launch_l2geth.bash"]
7671
ports:
7772
- "8548:8545" # JSON-RPC

tests/src/docker_compose.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ use std::{
55
process::Command,
66
time::{Duration, SystemTime, UNIX_EPOCH},
77
};
8+
use tokio::{
9+
io::{AsyncBufReadExt, BufReader},
10+
process::Command as TokioCommand,
11+
};
812

913
/// A wrapper that combines a provider with its human-readable name for testing
1014
pub struct NamedProvider {
@@ -53,6 +57,9 @@ impl DockerComposeEnv {
5357
// Start the environment
5458
let env = Self::start_environment(&compose_file, &project_name)?;
5559

60+
// Start streaming logs in the background
61+
let _ = Self::stream_container_logs(&compose_file, &project_name).await;
62+
5663
// Wait for all services to be ready
5764
tracing::info!("⏳ Waiting for services to be ready...");
5865
Self::wait_for_l2_node_ready(RN_SEQUENCER_RPC_URL, 30).await?;
@@ -208,6 +215,45 @@ impl DockerComposeEnv {
208215
);
209216
}
210217

218+
/// Stream logs from all containers in the background to the `docker-compose` target aat trace
219+
/// level.
220+
async fn stream_container_logs(compose_file: &str, project_name: &str) -> eyre::Result<()> {
221+
let mut child = TokioCommand::new("docker")
222+
.args([
223+
"compose",
224+
"-f",
225+
compose_file,
226+
"-p",
227+
project_name,
228+
"logs",
229+
"-f", // follow mode
230+
"--no-color", // avoid ANSI mess
231+
])
232+
.stdout(std::process::Stdio::piped())
233+
.stderr(std::process::Stdio::piped())
234+
.spawn()?;
235+
236+
if let Some(stdout) = child.stdout.take() {
237+
let mut reader = BufReader::new(stdout).lines();
238+
tokio::spawn(async move {
239+
while let Ok(Some(line)) = reader.next_line().await {
240+
tracing::trace!(target: "docker-compose", "{}", line);
241+
}
242+
});
243+
}
244+
245+
if let Some(stderr) = child.stderr.take() {
246+
let mut reader = BufReader::new(stderr).lines();
247+
tokio::spawn(async move {
248+
while let Ok(Some(line)) = reader.next_line().await {
249+
tracing::trace!(target: "docker-compose", "{}", line);
250+
}
251+
});
252+
}
253+
254+
Ok(())
255+
}
256+
211257
/// Show logs for all containers
212258
fn show_all_container_logs(compose_file: &str, project_name: &str) {
213259
tracing::info!("🔍 Getting all container logs...");

0 commit comments

Comments
 (0)