Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

日志

Feature: log

依赖

log = ["tracing", "tracing-appender", "tracing-subscriber"]

日志文件

文件级别格式
logs/error.log.YYYY-MM-DDERRORJSON
logs/warn.log.YYYY-MM-DDWARNJSON
logs/info.log.YYYY-MM-DDINFOJSON

标准输出: DEBUG 及以上级别

通过 lib.rs 导出:

#![allow(unused)]
fn main() {
pub use tracing::{debug, error, info, trace, warn};
}

使用示例

#![allow(unused)]
fn main() {
use afaster::{debug, error, info, warn};

info!("Server started");
warn!("Cache miss: {}", key);
error!({ code = 50002, msg = "数据库错误" }, "Connection failed: {}", err);
debug!("Request processed in {:?}", duration);
}

带上下文的日志

#![allow(unused)]
fn main() {
tracing::error!(
    { code = 50201, msg = "微信小程序登录错误" },
    "详细错误信息: {}", e
);
}