actix-web使用tklog

toml

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

[dependencies]
tklog = "0.2.6"
tokio = "1.40"
log = "0.4"
actix-web = "4"

rs

use actix_web::{get, HttpResponse, web, App, HttpServer};
use std::sync::Arc;
use tokio::sync::Mutex;
use tklog::{async_debug, async_error, async_fatal, async_info, async_trace, async_warn, LEVEL, Format, ASYNC_LOG, MODE, async_infos};

async fn async_log_init() {
    ASYNC_LOG
        .set_console(false) // 控制台输出
        .set_level(LEVEL::Info) // 日志等级
        .set_formatter("{time} {file} {level} | {message}\n") //打印格式
        .set_cutmode_by_time("api.log", MODE::DAY, 10, true) //根据日志轮转,每日,压缩
        .await;
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    //初始化日志
    async_log_init().await;
    // 启动actix-web服务器
    HttpServer::new(move || {
        App::new()
            .service(async_log_handler)
        })
        .bind("127.0.0.1:8888")?
        .run()
        .await
}

// 定义一个异步的处理函数,使用全局日志实例
#[get("/")]
async fn async_log_handler() -> HttpResponse {
    // 使用日志实例记录信息
    async_info!("async_infos>>>>", "CCCCCCCCCC", 1, 2, 3);
    // 返回响应
    HttpResponse::Ok().body("Hello, actix-web with tklog!")
}
posted @ 2024-11-05 16:01  朝阳1  阅读(14)  评论(0编辑  收藏  举报