Skip to content

Commit 7181a41

Browse files
committed
Add logger level
1 parent 946fc63 commit 7181a41

File tree

5 files changed

+83
-56
lines changed

5 files changed

+83
-56
lines changed

Cargo.lock

Lines changed: 52 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
[package]
22
name = "wait"
3-
version = "2.7.3"
3+
version = "2.8.0"
44
authors = ["ufoscout <[email protected]>"]
55
edition = "2018"
66

77
[dependencies]
8-
port_check = "0.1.5"
8+
port_check = "0.1"
9+
log = "0.4"
10+
env_logger = { version = "0.8", default-features = false }
911

1012
[dev-dependencies]
1113
atomic-counter = "1.0"

src/lib.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use log::*;
12
use std::time::Duration;
23

34
pub mod env_reader;
@@ -19,55 +20,53 @@ pub fn wait(
1920
config: &Config,
2021
on_timeout: &mut dyn FnMut(),
2122
) {
22-
println!("{}", LINE_SEPARATOR);
23-
println!(" docker-compose-wait {}", env!("CARGO_PKG_VERSION"));
24-
println!("---------------------------");
25-
println!("Starting with configuration:");
26-
println!(" - Hosts to be waiting for: [{}]", config.hosts);
27-
println!(
23+
info!("{}", LINE_SEPARATOR);
24+
info!(" docker-compose-wait {}", env!("CARGO_PKG_VERSION"));
25+
info!("---------------------------");
26+
debug!("Starting with configuration:");
27+
debug!(" - Hosts to be waiting for: [{}]", config.hosts);
28+
debug!(
2829
" - Timeout before failure: {} seconds ",
2930
config.global_timeout
3031
);
31-
println!(
32+
debug!(
3233
" - TCP connection timeout before retry: {} seconds ",
3334
config.tcp_connection_timeout
3435
);
35-
println!(
36+
debug!(
3637
" - Sleeping time before checking for hosts availability: {} seconds",
3738
config.wait_before
3839
);
39-
println!(
40+
debug!(
4041
" - Sleeping time once all hosts are available: {} seconds",
4142
config.wait_after
4243
);
43-
println!(
44+
debug!(
4445
" - Sleeping time between retries: {} seconds",
4546
config.wait_sleep_interval
4647
);
47-
println!("{}", LINE_SEPARATOR);
48+
debug!("{}", LINE_SEPARATOR);
4849

4950
if config.wait_before > 0 {
50-
println!(
51+
info!(
5152
"Waiting {} seconds before checking for hosts availability",
5253
config.wait_before
5354
);
54-
println!("{}", LINE_SEPARATOR);
55+
info!("{}", LINE_SEPARATOR);
5556
sleep.sleep(config.wait_before);
5657
}
5758

5859
if !config.hosts.trim().is_empty() {
5960
sleep.reset();
6061
for host in config.hosts.trim().split(',') {
61-
println!("Checking availability of {}", host);
62+
info!("Checking availability of {}", host);
6263
while !port_check::is_port_reachable_with_timeout(
63-
&host
64-
.trim()
65-
.to_string(),
64+
&host.trim().to_string(),
6665
Duration::from_secs(config.tcp_connection_timeout),
6766
) {
68-
println!("Host {} not yet available...", host);
67+
info!("Host {} not yet available...", host);
6968
if sleep.elapsed(config.global_timeout) {
70-
println!(
69+
error!(
7170
"Timeout! After {} seconds some hosts are still not reachable",
7271
config.global_timeout
7372
);
@@ -76,22 +75,22 @@ pub fn wait(
7675
}
7776
sleep.sleep(config.wait_sleep_interval);
7877
}
79-
println!("Host {} is now available!", host);
80-
println!("{}", LINE_SEPARATOR);
78+
info!("Host {} is now available!", host);
79+
info!("{}", LINE_SEPARATOR);
8180
}
8281
}
8382

8483
if config.wait_after > 0 {
85-
println!(
84+
info!(
8685
"Waiting {} seconds after hosts availability",
8786
config.wait_after
8887
);
89-
println!("{}", LINE_SEPARATOR);
88+
info!("{}", LINE_SEPARATOR);
9089
sleep.sleep(config.wait_after);
9190
}
9291

93-
println!("docker-compose-wait - Everything's fine, the application can now start!");
94-
println!("{}", LINE_SEPARATOR);
92+
info!("docker-compose-wait - Everything's fine, the application can now start!");
93+
info!("{}", LINE_SEPARATOR);
9594
}
9695

9796
pub fn config_from_env() -> Config {

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
fn main() {
2+
env_logger::init_from_env(env_logger::Env::default().filter_or("WAIT_LOGGER_LEVEL", "debug"));
3+
24
let mut sleep = wait::sleeper::new();
35
wait::wait(&mut sleep, &wait::config_from_env(), &mut on_timeout);
46
}

test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
export WAIT_HOSTS=localhost:8025,localhost:8081,localhost:1433
3+
export WAIT_HOSTS=localhost:4748
44
export WAIT_HOSTS_TIMEOUT=10
55
export WAIT_BEFORE_HOSTS=1
66
export WAIT_AFTER_HOSTS=2

0 commit comments

Comments
 (0)