| 1 |
From 174859bc1b8cfc56e083f4105b67b080fb356716 Mon Sep 17 00:00:00 2001 |
| 2 |
From: NoisyCoil <[email protected]> |
| 3 |
Date: Sun, 27 Oct 2024 15:57:04 +0100 |
| 4 |
Subject: [PATCH] Port to env_logger 0.11 |
| 5 |
|
| 6 |
env_logger 0.11 removed the API around which this library is written and |
| 7 |
offloaded its functionality to the anstyle crate, which is re-exported as |
| 8 |
env_logger::fmt::style. anstyle's API is very different than v0.10's: |
| 9 |
anstyle just provides ANSI escape codes which must then be used in format |
| 10 |
strings. Opening escape codes are printed with "{}" and closing escape |
| 11 |
codes are printed with "{:#}". `fn colored_level` must be re-written |
| 12 |
from scratch (and nicely enough can be marked as const). |
| 13 |
|
| 14 |
Testing suggests the patch does not change the visual appearance of the |
| 15 |
logs. |
| 16 |
--- |
| 17 |
src/lib.rs | 71 +++++++++++++++++++++++++++++++++++++----------------- |
| 18 |
1 file changed, 49 insertions(+), 22 deletions(-) |
| 19 |
|
| 20 |
--- a/src/lib.rs |
| 21 |
+++ b/src/lib.rs |
| 22 |
@@ -45,11 +45,13 @@ |
| 23 |
use std::sync::atomic::{AtomicUsize, Ordering}; |
| 24 |
|
| 25 |
use env_logger::{ |
| 26 |
- fmt::{Color, Style, StyledValue}, |
| 27 |
+ fmt::style::{AnsiColor, Color, Style}, |
| 28 |
Builder, |
| 29 |
}; |
| 30 |
use log::Level; |
| 31 |
|
| 32 |
+const BSTYLE: Style = Style::new().bold(); |
| 33 |
+ |
| 34 |
/// Initializes the global logger with a pretty env logger. |
| 35 |
/// |
| 36 |
/// This should be called early in the execution of a Rust program, and the |
| 37 |
@@ -169,16 +171,24 @@ |
| 38 |
let target = record.target(); |
| 39 |
let max_width = max_target_width(target); |
| 40 |
|
| 41 |
- let mut style = f.style(); |
| 42 |
- let level = colored_level(&mut style, record.level()); |
| 43 |
+ let (lstyle, level) = colored_level(record.level()); |
| 44 |
|
| 45 |
- let mut style = f.style(); |
| 46 |
- let target = style.set_bold(true).value(Padded { |
| 47 |
+ let target = Padded { |
| 48 |
value: target, |
| 49 |
width: max_width, |
| 50 |
- }); |
| 51 |
+ }; |
| 52 |
|
| 53 |
- writeln!(f, " {} {} > {}", level, target, record.args(),) |
| 54 |
+ writeln!( |
| 55 |
+ f, |
| 56 |
+ " {}{}{:#} {}{}{:#} > {}", |
| 57 |
+ lstyle, |
| 58 |
+ level, |
| 59 |
+ lstyle, |
| 60 |
+ BSTYLE, |
| 61 |
+ target, |
| 62 |
+ BSTYLE, |
| 63 |
+ record.args(), |
| 64 |
+ ) |
| 65 |
}); |
| 66 |
|
| 67 |
builder |
| 68 |
@@ -197,18 +207,27 @@ |
| 69 |
let target = record.target(); |
| 70 |
let max_width = max_target_width(target); |
| 71 |
|
| 72 |
- let mut style = f.style(); |
| 73 |
- let level = colored_level(&mut style, record.level()); |
| 74 |
+ let (lstyle, level) = colored_level(record.level()); |
| 75 |
|
| 76 |
- let mut style = f.style(); |
| 77 |
- let target = style.set_bold(true).value(Padded { |
| 78 |
+ let target = Padded { |
| 79 |
value: target, |
| 80 |
width: max_width, |
| 81 |
- }); |
| 82 |
+ }; |
| 83 |
|
| 84 |
let time = f.timestamp_millis(); |
| 85 |
|
| 86 |
- writeln!(f, " {} {} {} > {}", time, level, target, record.args(),) |
| 87 |
+ writeln!( |
| 88 |
+ f, |
| 89 |
+ " {}{}{:#} {} {}{}{:#} > {}", |
| 90 |
+ lstyle, |
| 91 |
+ time, |
| 92 |
+ lstyle, |
| 93 |
+ level, |
| 94 |
+ BSTYLE, |
| 95 |
+ target, |
| 96 |
+ BSTYLE, |
| 97 |
+ record.args(), |
| 98 |
+ ) |
| 99 |
}); |
| 100 |
|
| 101 |
builder |
| 102 |
@@ -237,12 +256,20 @@ |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
-fn colored_level<'a>(style: &'a mut Style, level: Level) -> StyledValue<'a, &'static str> { |
| 107 |
- match level { |
| 108 |
- Level::Trace => style.set_color(Color::Magenta).value("TRACE"), |
| 109 |
- Level::Debug => style.set_color(Color::Blue).value("DEBUG"), |
| 110 |
- Level::Info => style.set_color(Color::Green).value("INFO "), |
| 111 |
- Level::Warn => style.set_color(Color::Yellow).value("WARN "), |
| 112 |
- Level::Error => style.set_color(Color::Red).value("ERROR"), |
| 113 |
- } |
| 114 |
+const fn colored_level(level: Level) -> (Style, &'static str) { |
| 115 |
+ let style = Style::new(); |
| 116 |
+ let (style, level) = match level { |
| 117 |
+ Level::Trace => ( |
| 118 |
+ style.fg_color(Some(Color::Ansi(AnsiColor::Magenta))), |
| 119 |
+ "TRACE", |
| 120 |
+ ), |
| 121 |
+ Level::Debug => (style.fg_color(Some(Color::Ansi(AnsiColor::Blue))), "DEBUG"), |
| 122 |
+ Level::Info => (style.fg_color(Some(Color::Ansi(AnsiColor::Green))), "INFO "), |
| 123 |
+ Level::Warn => ( |
| 124 |
+ style.fg_color(Some(Color::Ansi(AnsiColor::Yellow))), |
| 125 |
+ "WARN ", |
| 126 |
+ ), |
| 127 |
+ Level::Error => (style.fg_color(Some(Color::Ansi(AnsiColor::Red))), "ERROR"), |
| 128 |
+ }; |
| 129 |
+ (style, level) |
| 130 |
} |
| 131 |
--- a/Cargo.toml |
| 132 |
+++ b/Cargo.toml |
| 133 |
@@ -31,7 +31,7 @@ |
| 134 |
repository = "https://github.com/seanmonstar/pretty-env-logger" |
| 135 |
|
| 136 |
[dependencies.env_logger] |
| 137 |
-version = "0.10" |
| 138 |
+version = "0.11" |
| 139 |
|
| 140 |
[dependencies.log] |
| 141 |
version = "0.4" |