에러 로깅

[package]
name = "rust_part"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log = "0.4"
pretty_env_logger = "0.4.0"


[dev-dependencies]

기본 사용법

extern crate pretty_env_logger;
#[macro_use] extern crate log;

fn main() {
    pretty_env_logger::init();

    trace!("a trace example");
    debug!("deboogging");
    info!("such information");
    warn!("o_O");
    error!("boom");
}
RUST_LOG=trace cargo run
 TRACE rust_part > a trace example
 DEBUG rust_part > deboogging
 INFO  rust_part > such information
 WARN  rust_part > o_O
 ERROR rust_part > boom

커스터마이징

use log::{debug, error, info, trace, warn};

const LOG_LEVEL: log::Level = log::Level::Trace;

fn main() {
    let mut log_builder = pretty_env_logger::formatted_timed_builder();
    log_builder.parse_filters(LOG_LEVEL.as_str()).init();

    trace!("a trace example");
    debug!("deboogging");
    info!("such information");
    warn!("o_O");
    error!("boom");
}
 2023-03-04T10:21:20.575Z TRACE rust_part > a trace example
 2023-03-04T10:21:20.575Z DEBUG rust_part > deboogging
 2023-03-04T10:21:20.575Z INFO  rust_part > such information
 2023-03-04T10:21:20.575Z WARN  rust_part > o_O
 2023-03-04T10:21:20.575Z ERROR rust_part > boom