Răsfoiți Sursa

日志修好了。

skyffire 1 an în urmă
părinte
comite
aabc3982d6
4 a modificat fișierele cu 26 adăugiri și 35 ștergeri
  1. 1 1
      Cargo.toml
  2. 7 2
      global/Cargo.toml
  3. 17 31
      global/src/log_utils.rs
  4. 1 1
      src/main.rs

+ 1 - 1
Cargo.toml

@@ -14,7 +14,7 @@ tokio = { version = "1.31.0", features = ["full"] }
 chrono = "0.4.26"
 tracing = "0.1"
 tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
-tracing-appender = "0.2.2"
+tracing-appender-timezone = { git = "https://github.com/skyfffire/tracing-appender-timezone.git" }
 serde = { version = "1.0.188", features = ["derive"] }
 actix-rt = "2.5.0"
 actix-web = "4.0.0-beta.12"

+ 7 - 2
global/Cargo.toml

@@ -9,8 +9,13 @@ edition = "2021"
 rust_decimal = "1.32.0"
 rust_decimal_macros = "1.32.0"
 tracing = "0.1"
-tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
-tracing-appender = "0.2.3"
+tracing-subscriber = { version = "0.3.17", features = [
+    "env-filter",
+    "time",
+    "local-time"
+] }
+time = { version = "0.3.7", features = ["macros"] }
+tracing-appender-timezone = { git = "https://github.com/skyfffire/tracing-appender-timezone.git" }
 toml = "0.5.11"
 serde = "1.0.183"
 serde_derive = "1.0"

+ 17 - 31
global/src/log_utils.rs

@@ -1,36 +1,14 @@
 use std::collections::HashMap;
 use std::fmt::Debug;
 use std::io;
-use chrono::{Datelike, FixedOffset, Timelike, Utc};
 use tracing::{Event, Subscriber, warn};
-use tracing_appender::non_blocking::WorkerGuard;
+use tracing_appender_timezone::non_blocking::WorkerGuard;
 use tracing_subscriber::{fmt, Layer};
-use tracing_subscriber::fmt::format::Writer;
-use tracing_subscriber::fmt::time::FormatTime;
 use tracing_subscriber::layer::{Context, SubscriberExt};
 use reqwest::{Client};
-use rust_decimal::prelude::ToPrimitive;
 use tracing::field::{Field, Visit};
-use tracing_appender::rolling::{RollingFileAppender, Rotation};
-
-// 用來格式化日誌的輸出時間格式
-struct LocalTimer;
-
-impl FormatTime for LocalTimer {
-    fn format_time(&self, w: &mut Writer<'_>) -> std::fmt::Result {
-        let now = Utc::now().with_timezone(&FixedOffset::east_opt(8 * 3600).unwrap());
-        write!(
-            w,
-            "{:02}-{:02} {:02}:{:02}:{:02}.{:03}",
-            now.month(),
-            now.day(),
-            now.hour(),
-            now.minute(),
-            now.second(),
-            now.nanosecond() / 1e6.to_u32().unwrap()
-        )
-    }
-}
+use tracing_appender_timezone::rolling::{RollingFileAppender, Rotation};
+
 
 struct ErrorMessageVisitor {
     message: String
@@ -107,22 +85,30 @@ pub fn final_init(level: &str, port: u32, account_name: String) -> WorkerGuard {
     path.push_str(port.to_string().as_str());
 
     let file_appender = RollingFileAppender::builder()
-        .rotation(Rotation::HOURLY)
+        .time_zone(8)
+        .rotation(Rotation::DAILY)
         .filename_suffix("log")
         .build(path)
         .expect("initializing rolling file appender failed");
-    let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
+    let (non_blocking, guard) = tracing_appender_timezone::non_blocking(file_appender);
+
+    use time::{macros::format_description, UtcOffset};
+    use tracing_subscriber::{fmt::time::OffsetTime};
+    let local_time = OffsetTime::new(
+        UtcOffset::from_hms(8, 0, 0).unwrap(),
+        format_description!("[month]-[day] [hour]:[minute]:[second].[subsecond digits:3]"),
+    );
 
     let fmt_layer = fmt::layer()
-        .with_timer(LocalTimer)
-        .with_target(true)
+        .with_timer(local_time.clone())
+        .with_target(false)
         .with_level(true)
         .with_writer(io::stdout)
         .with_span_events(fmt::format::FmtSpan::FULL);
 
     let file_layer = fmt::layer()
-        .with_timer(LocalTimer)
-        .with_target(true)
+        .with_timer(local_time.clone())
+        .with_target(false)
         .with_ansi(false)
         .with_level(true)
         .with_writer(non_blocking.clone())

+ 1 - 1
src/main.rs

@@ -6,7 +6,7 @@ use std::sync::Arc;
 use std::sync::atomic::{AtomicBool, Ordering};
 use std::time::Duration;
 use tracing::{info, warn};
-use tracing_appender::non_blocking::WorkerGuard;
+use tracing_appender_timezone::non_blocking::WorkerGuard;
 use global::log_utils::send_remote_err_log;
 use global::params::Params;