简体   繁体   English

为什么货物测试显示我的踪迹! 当我指定 RUST_LOG=debug?

[英]Why does cargo test show me trace! logs when I specify RUST_LOG=debug?

Why does cargo test show me trace!为什么cargo test显示我的trace! logs when I specify RUST_LOG=debug ?我指定RUST_LOG=debug时的日志?

I'm using SimpleLogger as a test backend, initialized like that ( init_logger is called at the beginning of each test function):我使用SimpleLogger作为测试后端,像这样初始化(在每个测试函数的开头调用init_logger ):

static INIT_LOGGER: Once = Once::new();

pub fn init_logger() {
    INIT_LOGGER.call_once(|| {
        SimpleLogger::new().init().unwrap();
    });
}

I run the tests with:我运行测试:

RUST_LOG=debug cargo test

And I see all log messages, INFO , DEBUG ...我看到所有日志消息, INFODEBUG ...

I'd like to see only the DEBUG messages.我只想看到DEBUG消息。

Also, I'd like to see only the messages from my own crate, not from its dependencies, is that possible?另外,我只想看到来自我自己的箱子的消息,而不是来自它的依赖项,这可能吗?

SimpleLogger by default does not react to environment variables. SimpleLogger默认不对环境变量做出反应。

Use the .env() method to enable that behaviour:使用.env()方法启用该行为:

use std::sync::Once;

use simple_logger::SimpleLogger;

static INIT_LOGGER: Once = Once::new();
pub fn init_logger() {
    INIT_LOGGER.call_once(|| {
        SimpleLogger::new().env().init().unwrap();
    });
}

fn main() {
    init_logger();
    log::error!("error");
    log::warn!("warn");
    log::info!("info");
    log::debug!("debug");
    log::trace!("trace");
}
> RUST_LOG=debug cargo run
2022-07-09T14:49:19.733Z ERROR [streamer] error
2022-07-09T14:49:19.733Z WARN [streamer] warn
2022-07-09T14:49:19.733Z INFO [streamer] info
2022-07-09T14:49:19.733Z DEBUG [streamer] debug

> RUST_LOG=info cargo run
2022-07-09T14:49:25.022Z ERROR [streamer] error
2022-07-09T14:49:25.022Z WARN [streamer] warn
2022-07-09T14:49:25.022Z INFO [streamer] info

Or use the much more popular EnvLogger instead.或者改用更流行的EnvLogger

With EnvLogger you can do exactly what you were asking for;使用EnvLogger ,您可以完全按照您的要求进行操作; you can enable different log levels for different dependencies.您可以为不同的依赖项启用不同的日志级别。 Here is a more detailed description of what can be achieved with EnvLogger .这是对EnvLogger可以实现 的功能的更详细描述

use std::sync::Once;

static INIT_LOGGER: Once = Once::new();
pub fn init_logger() {
    INIT_LOGGER.call_once(|| {
        env_logger::init();
    });
}

fn main() {
    init_logger();
    log::error!("error");
    log::warn!("warn");
    log::info!("info");
    log::debug!("debug");
    log::trace!("trace");
}
> RUST_LOG=debug cargo run
[2022-07-09T14:52:42Z ERROR streamer] error
[2022-07-09T14:52:42Z WARN  streamer] warn
[2022-07-09T14:52:42Z INFO  streamer] info
[2022-07-09T14:52:42Z DEBUG streamer] debug

> RUST_LOG=info cargo run
[2022-07-09T14:52:46Z ERROR streamer] error
[2022-07-09T14:52:46Z WARN  streamer] warn
[2022-07-09T14:52:46Z INFO  streamer] info

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有哪些方法可以运行 Rust cargo test 并让测试运行器将任何日志 output 显示到控制台 stdout 或 stderr? - What are ways to run Rust cargo test and have the test runner display any log output to the console stdout or stderr? 如何通过 cargo test 分离测试的执行? - How do I separate the execution of tests via cargo test? 货物测试-释放导致堆栈溢出。 为什么没有货台? - cargo test --release causes a stack overflow. Why doesn't cargo bench? 为什么“assertSelectorExists()”断言不显示我指定的失败消息? - Why doesn't the "assertSelectorExists()" assertion show the failure message that I specify? 使用自动化仪器时如何清除编辑器日志和跟踪日志 - How to clear Editor log and Trace Log when using Automation Instrument 我想测试我的应用程序是否可以访问 500 个内部服务器,并且什么时候我想显示我的自定义错误页面 - I want to test my app for to hit a 500 Internal server and when it does I want to show my custom error page 当我尝试将对象保存到数据库时,Rails功能测试为什么失败 - Why Does Rails Function Test Fail When I try to Save Object To DB 如何测试 Rust 中的可选功能? - How does one test optional features in Rust? 如何在 Rust 中测试私有方法? - How do I test private methods in Rust? 如何指定没有输入的测试用例 - How to specify a test case that does not have an input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM