美文网首页
Rust - 日志Json输出

Rust - 日志Json输出

作者: 国服最坑开发 | 来源:发表于2022-05-08 01:55 被阅读0次

    依赖: Cargo.toml

    [dependencies]
    actix-web = "4"
    
    log = "0.4.17"
    tracing = "0.1.34"
    tracing-subscriber = { version = "0.3.11", features = ["json", "valuable"] }
    

    使用:

    use actix_web::{App, HttpServer, Responder, get};
    use tracing::info;
    use tracing::{Level};
    
    
    #[actix_web::main]
    async fn main() -> std::io::Result<()> {
        tracing_subscriber::fmt().with_max_level(Level::INFO).json().init();
        info!("start at http://127.0.0.1:80/");
        HttpServer::new(|| {
            App::new().service(index)
        }).bind(("0.0.0.0", 80))?.run().await
    }
    
    #[get("/")]
    async fn index() -> impl Responder {
        let sid = "123123";
        let did = "1231234444";
        let t = 123;
    
        info!(sid, did, "this is a message {}", t);
    
        "hello, web actix".to_string()
    }
    

    输出到stdout

    相关文章

      网友评论

          本文标题:Rust - 日志Json输出

          本文链接:https://www.haomeiwen.com/subject/lkvfurtx.html