实现远程日志

This commit is contained in:
2026-01-29 01:57:40 +08:00
parent f1d846bb8b
commit d8917639af
9 changed files with 649 additions and 2 deletions

View File

@@ -1,8 +1,16 @@
pub mod manager;
pub mod protobuf;
use actix_web::{App, HttpResponse, HttpServer, Responder, get, web};
use chrono::Local;
use colored::{Color, Colorize};
use fern::Dispatch;
use log::Level;
#[tokio::main]
async fn main() {
HttpServer::new(|| App::new().service(root))
init_log();
HttpServer::new(|| App::new().service(root).service(manager::ws_connect))
.bind(("0.0.0.0", 2018))
.unwrap()
.run()
@@ -10,6 +18,34 @@ async fn main() {
.unwrap();
}
fn get_time() -> String {
let now = Local::now();
now.format("%Y-%m-%d %H:%M:%S").to_string()
}
fn init_log() {
let mut console_dispatch = Dispatch::new()
.format(|out, message, record| {
let (color, title) = match record.level() {
Level::Error => (Color::Red, "Error"),
Level::Warn => (Color::Yellow, "Warn"),
Level::Info => (Color::Green, "Info"),
Level::Debug => (Color::BrightWhite, "Debug"),
Level::Trace => (Color::White, "Trace"),
};
out.finish(format_args!(
"[{} {}]\t{}",
get_time(),
title.color(color),
message
))
})
.chain(std::io::stdout())
.level(log::LevelFilter::Debug);
console_dispatch.apply().expect("写入日志失败");
}
#[get("/")]
async fn root() -> impl Responder {
HttpResponse::Ok()