| 1 |
#![cfg_attr(test, deny(warnings))] |
| 2 |
#![deny(missing_docs)] |
| 3 |
#![doc(html_root_url = "https://docs.rs/pretty_env_logger/0.5.0")] |
| 4 |
|
| 5 |
//! A logger configured via an environment variable which writes to standard |
| 6 |
//! error with nice colored output for log levels. |
| 7 |
//! |
| 8 |
//! ## Example |
| 9 |
//! |
| 10 |
//! ``` |
| 11 |
//! extern crate pretty_env_logger; |
| 12 |
//! #[macro_use] extern crate log; |
| 13 |
//! |
| 14 |
//! fn main() { |
| 15 |
//! pretty_env_logger::init(); |
| 16 |
//! |
| 17 |
//! trace!("a trace example"); |
| 18 |
//! debug!("deboogging"); |
| 19 |
//! info!("such information"); |
| 20 |
//! warn!("o_O"); |
| 21 |
//! error!("boom"); |
| 22 |
//! } |
| 23 |
//! ``` |
| 24 |
//! |
| 25 |
//! Run the program with the environment variable `RUST_LOG=trace`. |
| 26 |
//! |
| 27 |
//! ## Defaults |
| 28 |
//! |
| 29 |
//! The defaults can be setup by calling `init()` or `try_init()` at the start |
| 30 |
//! of the program. |
| 31 |
//! |
| 32 |
//! ## Enable logging |
| 33 |
//! |
| 34 |
//! This crate uses [env_logger][] internally, so the same ways of enabling |
| 35 |
//! logs through an environment variable are supported. |
| 36 |
//! |
| 37 |
//! [env_logger]: https://docs.rs/env_logger |
| 38 |
|
| 39 |
#[doc(hidden)] |
| 40 |
pub extern crate env_logger; |
| 41 |
|
| 42 |
extern crate log; |
| 43 |
|
| 44 |
use std::fmt; |
| 45 |
use std::sync::atomic::{AtomicUsize, Ordering}; |
| 46 |
|
| 47 |
use env_logger::{ |
| •48 |
fmt::style::{AnsiColor, Color, Style}, |
| 49 |
Builder, |
| 50 |
}; |
| 51 |
use log::Level; |
| 52 |
|
| •53 |
const BSTYLE: Style = Style::new().bold(); |
| 54 |
|
| 55 |
/// Initializes the global logger with a pretty env logger. |
| 56 |
/// |
| 57 |
/// This should be called early in the execution of a Rust program, and the |
| 58 |
/// global logger may only be initialized once. Future initialization attempts |
| 59 |
/// will return an error. |
| 60 |
/// |
| 61 |
/// # Panics |
| 62 |
/// |
| 63 |
/// This function fails to set the global logger if one has already been set. |
| 64 |
pub fn init() { |
| 65 |
try_init().unwrap(); |
| 66 |
} |
| 67 |
|
| 68 |
/// Initializes the global logger with a timed pretty env logger. |
| 69 |
/// |
| 70 |
/// This should be called early in the execution of a Rust program, and the |
| 71 |
/// global logger may only be initialized once. Future initialization attempts |
| 72 |
/// will return an error. |
| 73 |
/// |
| 74 |
/// # Panics |
| 75 |
/// |
| 76 |
/// This function fails to set the global logger if one has already been set. |
| 77 |
pub fn init_timed() { |
| 78 |
try_init_timed().unwrap(); |
| 79 |
} |
| 80 |
|
| 81 |
/// Initializes the global logger with a pretty env logger. |
| 82 |
/// |
| 83 |
/// This should be called early in the execution of a Rust program, and the |
| 84 |
/// global logger may only be initialized once. Future initialization attempts |
| 85 |
/// will return an error. |
| 86 |
/// |
| 87 |
/// # Errors |
| 88 |
/// |
| 89 |
/// This function fails to set the global logger if one has already been set. |
| 90 |
pub fn try_init() -> Result<(), log::SetLoggerError> { |
| 91 |
try_init_custom_env("RUST_LOG") |
| 92 |
} |
| 93 |
|
| 94 |
/// Initializes the global logger with a timed pretty env logger. |
| 95 |
/// |
| 96 |
/// This should be called early in the execution of a Rust program, and the |
| 97 |
/// global logger may only be initialized once. Future initialization attempts |
| 98 |
/// will return an error. |
| 99 |
/// |
| 100 |
/// # Errors |
| 101 |
/// |
| 102 |
/// This function fails to set the global logger if one has already been set. |
| 103 |
pub fn try_init_timed() -> Result<(), log::SetLoggerError> { |
| 104 |
try_init_timed_custom_env("RUST_LOG") |
| 105 |
} |
| 106 |
|
| 107 |
/// Initialized the global logger with a pretty env logger, with a custom variable name. |
| 108 |
/// |
| 109 |
/// This should be called early in the execution of a Rust program, and the |
| 110 |
/// global logger may only be initialized once. Future initialization attempts |
| 111 |
/// will return an error. |
| 112 |
/// |
| 113 |
/// # Panics |
| 114 |
/// |
| 115 |
/// This function fails to set the global logger if one has already been set. |
| 116 |
pub fn init_custom_env(environment_variable_name: &str) { |
| 117 |
try_init_custom_env(environment_variable_name).unwrap(); |
| 118 |
} |
| 119 |
|
| 120 |
/// Initialized the global logger with a pretty env logger, with a custom variable name. |
| 121 |
/// |
| 122 |
/// This should be called early in the execution of a Rust program, and the |
| 123 |
/// global logger may only be initialized once. Future initialization attempts |
| 124 |
/// will return an error. |
| 125 |
/// |
| 126 |
/// # Errors |
| 127 |
/// |
| 128 |
/// This function fails to set the global logger if one has already been set. |
| 129 |
pub fn try_init_custom_env(environment_variable_name: &str) -> Result<(), log::SetLoggerError> { |
| 130 |
let mut builder = formatted_builder(); |
| 131 |
|
| 132 |
if let Ok(s) = ::std::env::var(environment_variable_name) { |
| 133 |
builder.parse_filters(&s); |
| 134 |
} |
| 135 |
|
| 136 |
builder.try_init() |
| 137 |
} |
| 138 |
|
| 139 |
/// Initialized the global logger with a timed pretty env logger, with a custom variable name. |
| 140 |
/// |
| 141 |
/// This should be called early in the execution of a Rust program, and the |
| 142 |
/// global logger may only be initialized once. Future initialization attempts |
| 143 |
/// will return an error. |
| 144 |
/// |
| 145 |
/// # Errors |
| 146 |
/// |
| 147 |
/// This function fails to set the global logger if one has already been set. |
| 148 |
pub fn try_init_timed_custom_env( |
| 149 |
environment_variable_name: &str, |
| 150 |
) -> Result<(), log::SetLoggerError> { |
| 151 |
let mut builder = formatted_timed_builder(); |
| 152 |
|
| 153 |
if let Ok(s) = ::std::env::var(environment_variable_name) { |
| 154 |
builder.parse_filters(&s); |
| 155 |
} |
| 156 |
|
| 157 |
builder.try_init() |
| 158 |
} |
| 159 |
|
| 160 |
/// Returns a `env_logger::Builder` for further customization. |
| 161 |
/// |
| 162 |
/// This method will return a colored and formatted `env_logger::Builder` |
| 163 |
/// for further customization. Refer to env_logger::Build crate documentation |
| 164 |
/// for further details and usage. |
| 165 |
pub fn formatted_builder() -> Builder { |
| 166 |
let mut builder = Builder::new(); |
| 167 |
|
| 168 |
builder.format(|f, record| { |
| 169 |
use std::io::Write; |
| 170 |
|
| 171 |
let target = record.target(); |
| 172 |
let max_width = max_target_width(target); |
| 173 |
|
| •174 |
let (lstyle, level) = colored_level(record.level()); |
| 175 |
|
| •176 |
let target = Padded { |
| 177 |
value: target, |
| 178 |
width: max_width, |
| •179 |
}; |
| 180 |
|
| •181 |
writeln!( |
| 182 |
f, |
| 183 |
" {}{}{:#} {}{}{:#} > {}", |
| 184 |
lstyle, |
| 185 |
level, |
| 186 |
lstyle, |
| 187 |
BSTYLE, |
| 188 |
target, |
| 189 |
BSTYLE, |
| 190 |
record.args(), |
| 191 |
) |
| 192 |
}); |
| 193 |
|
| 194 |
builder |
| 195 |
} |
| 196 |
|
| 197 |
/// Returns a `env_logger::Builder` for further customization. |
| 198 |
/// |
| 199 |
/// This method will return a colored and time formatted `env_logger::Builder` |
| 200 |
/// for further customization. Refer to env_logger::Build crate documentation |
| 201 |
/// for further details and usage. |
| 202 |
pub fn formatted_timed_builder() -> Builder { |
| 203 |
let mut builder = Builder::new(); |
| 204 |
|
| 205 |
builder.format(|f, record| { |
| 206 |
use std::io::Write; |
| 207 |
let target = record.target(); |
| 208 |
let max_width = max_target_width(target); |
| 209 |
|
| •210 |
let (lstyle, level) = colored_level(record.level()); |
| 211 |
|
| •212 |
let target = Padded { |
| 213 |
value: target, |
| 214 |
width: max_width, |
| •215 |
}; |
| 216 |
|
| 217 |
let time = f.timestamp_millis(); |
| 218 |
|
| •219 |
writeln!( |
| 220 |
f, |
| 221 |
" {}{}{:#} {} {}{}{:#} > {}", |
| 222 |
lstyle, |
| 223 |
time, |
| 224 |
lstyle, |
| 225 |
level, |
| 226 |
BSTYLE, |
| 227 |
target, |
| 228 |
BSTYLE, |
| 229 |
record.args(), |
| 230 |
) |
| 231 |
}); |
| 232 |
|
| 233 |
builder |
| 234 |
} |
| 235 |
|
| 236 |
struct Padded<T> { |
| 237 |
value: T, |
| 238 |
width: usize, |
| 239 |
} |
| 240 |
|
| 241 |
impl<T: fmt::Display> fmt::Display for Padded<T> { |
| 242 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 243 |
write!(f, "{: <width$}", self.value, width = self.width) |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
static MAX_MODULE_WIDTH: AtomicUsize = AtomicUsize::new(0); |
| 248 |
|
| 249 |
fn max_target_width(target: &str) -> usize { |
| 250 |
let max_width = MAX_MODULE_WIDTH.load(Ordering::Relaxed); |
| 251 |
if max_width < target.len() { |
| 252 |
MAX_MODULE_WIDTH.store(target.len(), Ordering::Relaxed); |
| 253 |
target.len() |
| 254 |
} else { |
| 255 |
max_width |
| 256 |
} |
| 257 |
} |
| 258 |
|
| •259 |
const fn colored_level(level: Level) -> (Style, &'static str) { |
| 260 |
let style = Style::new(); |
| 261 |
let (style, level) = match level { |
| 262 |
Level::Trace => ( |
| 263 |
style.fg_color(Some(Color::Ansi(AnsiColor::Magenta))), |
| 264 |
"TRACE", |
| 265 |
), |
| 266 |
Level::Debug => (style.fg_color(Some(Color::Ansi(AnsiColor::Blue))), "DEBUG"), |
| 267 |
Level::Info => (style.fg_color(Some(Color::Ansi(AnsiColor::Green))), "INFO "), |
| 268 |
Level::Warn => ( |
| 269 |
style.fg_color(Some(Color::Ansi(AnsiColor::Yellow))), |
| 270 |
"WARN ", |
| 271 |
), |
| 272 |
Level::Error => (style.fg_color(Some(Color::Ansi(AnsiColor::Red))), "ERROR"), |
| 273 |
}; |
| 274 |
(style, level) |
| 275 |
} |