Skip to content

Commit

Permalink
fix: [#893] enable color in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jun 14, 2024
1 parent 4832235 commit a88082a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/bootstrap/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn config_level_or_default(log_level: &Option<LogLevel>) -> LevelFilter {
}

fn tracing_stdout_init(filter: LevelFilter, style: &TraceStyle) {
let builder = tracing_subscriber::fmt().with_max_level(filter).with_ansi(false);
let builder = tracing_subscriber::fmt().with_max_level(filter).with_ansi(true);

let () = match style {
TraceStyle::Default => builder.init(),
Expand Down
57 changes: 48 additions & 9 deletions src/console/ci/e2e/logs_parser.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
//! Utilities to parse Torrust Tracker logs.
use serde::{Deserialize, Serialize};

const UDP_TRACKER_PATTERN: &str = "UDP TRACKER: Started on: udp://";
const HTTP_TRACKER_PATTERN: &str = "HTTP TRACKER: Started on: ";
const HEALTH_CHECK_PATTERN: &str = "HEALTH CHECK API: Started on: ";
const INFO_LOG_LEVEL: &str = "INFO";

const UDP_TRACKER_TARGET: &str = "UDP TRACKER";
const UDP_TRACKER_SOCKET_ADDR_START_PATTERN: &str = "Started on: udp://";

const HTTP_TRACKER_TARGET: &str = "HTTP TRACKER";
const HTTP_TRACKER_URL_START_PATTERN: &str = "Started on: ";

const HEALTH_CHECK_TARGET: &str = "HEALTH CHECK API";
const HEALTH_CHECK_URL_START_PATTERN: &str = "Started on: ";

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct RunningServices {
Expand Down Expand Up @@ -59,11 +66,11 @@ impl RunningServices {
let mut health_checks: Vec<String> = Vec::new();

for line in logs.lines() {
if let Some(address) = Self::extract_address_if_matches(line, UDP_TRACKER_PATTERN) {
if let Some(address) = Self::extract_udp_tracker_url(line) {
udp_trackers.push(address);
} else if let Some(address) = Self::extract_address_if_matches(line, HTTP_TRACKER_PATTERN) {
} else if let Some(address) = Self::extract_http_tracker_url(line) {
http_trackers.push(address);
} else if let Some(address) = Self::extract_address_if_matches(line, HEALTH_CHECK_PATTERN) {
} else if let Some(address) = Self::extract_health_check_api_url(line) {
health_checks.push(format!("{address}/health_check"));
}
}
Expand All @@ -75,9 +82,32 @@ impl RunningServices {
}
}

fn extract_address_if_matches(line: &str, pattern: &str) -> Option<String> {
line.find(pattern)
.map(|start| Self::replace_wildcard_ip_with_localhost(line[start + pattern.len()..].trim()))
fn extract_udp_tracker_url(line: &str) -> Option<String> {
if !line.contains(INFO_LOG_LEVEL) || !line.contains(UDP_TRACKER_TARGET) {
return None;
};

line.find(UDP_TRACKER_SOCKET_ADDR_START_PATTERN).map(|start| {
Self::replace_wildcard_ip_with_localhost(line[start + UDP_TRACKER_SOCKET_ADDR_START_PATTERN.len()..].trim())
})
}

fn extract_http_tracker_url(line: &str) -> Option<String> {
if !line.contains(INFO_LOG_LEVEL) || !line.contains(HTTP_TRACKER_TARGET) {
return None;
};

line.find(HTTP_TRACKER_URL_START_PATTERN)
.map(|start| Self::replace_wildcard_ip_with_localhost(line[start + HTTP_TRACKER_URL_START_PATTERN.len()..].trim()))
}

fn extract_health_check_api_url(line: &str) -> Option<String> {
if !line.contains(INFO_LOG_LEVEL) || !line.contains(HEALTH_CHECK_TARGET) {
return None;
};

line.find(HEALTH_CHECK_URL_START_PATTERN)
.map(|start| Self::replace_wildcard_ip_with_localhost(line[start + HEALTH_CHECK_URL_START_PATTERN.len()..].trim()))
}

fn replace_wildcard_ip_with_localhost(address: &str) -> String {
Expand Down Expand Up @@ -127,6 +157,15 @@ mod tests {
assert_eq!(running_services.health_checks, vec!["http://127.0.0.1:1313/health_check"]);
}

#[test]
fn it_should_support_colored_output() {
let logs = "\x1b[2m2024-06-14T14:40:13.028824Z\x1b[0m \x1b[33mINFO\x1b[0m \x1b[2mUDP TRACKER\x1b[0m: \x1b[37mStarted on: udp://0.0.0.0:6969\x1b[0m";

let running_services = RunningServices::parse_from_logs(logs);

assert_eq!(running_services.udp_trackers, vec!["127.0.0.1:6969"]);
}

#[test]
fn it_should_ignore_logs_with_no_matching_lines() {
let logs = "[Other Service][INFO] Started on: 0.0.0.0:7070";
Expand Down
2 changes: 1 addition & 1 deletion src/console/ci/e2e/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn run() -> anyhow::Result<()> {
}

fn tracing_stdout_init(filter: LevelFilter) {
tracing_subscriber::fmt().with_max_level(filter).with_ansi(false).init();
tracing_subscriber::fmt().with_max_level(filter).init();
info!("Logging initialized.");
}

Expand Down
2 changes: 1 addition & 1 deletion src/console/clients/checker/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub async fn run() -> Result<Vec<CheckResult>> {
}

fn tracing_stdout_init(filter: LevelFilter) {
tracing_subscriber::fmt().with_max_level(filter).with_ansi(false).init();
tracing_subscriber::fmt().with_max_level(filter).init();
info!("logging initialized.");
}

Expand Down
2 changes: 1 addition & 1 deletion src/console/clients/udp/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub async fn run() -> anyhow::Result<()> {
}

fn tracing_stdout_init(filter: LevelFilter) {
tracing_subscriber::fmt().with_max_level(filter).with_ansi(false).init();
tracing_subscriber::fmt().with_max_level(filter).init();
info!("logging initialized.");
}

Expand Down

0 comments on commit a88082a

Please sign in to comment.