Skip to content

Commit b5d40e3

Browse files
authored
Align logs (#7256)
#7249
1 parent e77fb01 commit b5d40e3

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

common/logging/src/tracing_logging_layer.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ use tracing_subscriber::layer::Context;
1313
use tracing_subscriber::registry::LookupSpan;
1414
use tracing_subscriber::Layer;
1515

16+
const FIXED_MESSAGE_WIDTH: usize = 44;
17+
const ALIGNED_LEVEL_WIDTH: usize = 5;
18+
1619
pub struct LoggingLayer {
1720
pub non_blocking_writer: NonBlocking,
1821
_guard: WorkerGuard,
@@ -368,13 +371,18 @@ fn build_log_text<'a, S>(
368371
}
369372
}
370373

374+
let pad = if plain_level_str.len() < ALIGNED_LEVEL_WIDTH {
375+
" "
376+
} else {
377+
""
378+
};
379+
371380
let level_str = if use_color {
372-
color_level_str
381+
format!("{}{}", color_level_str, pad)
373382
} else {
374-
plain_level_str
383+
format!("{}{}", plain_level_str, pad)
375384
};
376385

377-
let fixed_message_width = 44;
378386
let message_len = visitor.message.len();
379387

380388
let message_content = if use_color {
@@ -383,7 +391,7 @@ fn build_log_text<'a, S>(
383391
visitor.message.clone()
384392
};
385393

386-
let padded_message = if message_len < fixed_message_width {
394+
let padded_message = if message_len < FIXED_MESSAGE_WIDTH {
387395
let extra_color_len = if use_color {
388396
bold_start.len() + bold_end.len()
389397
} else {
@@ -392,7 +400,7 @@ fn build_log_text<'a, S>(
392400
format!(
393401
"{:<width$}",
394402
message_content,
395-
width = fixed_message_width + extra_color_len
403+
width = FIXED_MESSAGE_WIDTH + extra_color_len
396404
)
397405
} else {
398406
message_content.clone()

0 commit comments

Comments
 (0)