|
|
@@ -0,0 +1,102 @@
|
|
|
+use std::fmt::Debug;
|
|
|
+use std::io;
|
|
|
+use tracing::{Event, Subscriber};
|
|
|
+use tracing_appender_timezone::non_blocking::WorkerGuard;
|
|
|
+use tracing_subscriber::{fmt, Layer};
|
|
|
+use tracing_subscriber::layer::{Context, SubscriberExt};
|
|
|
+use tracing::field::{Field, Visit};
|
|
|
+use tracing_appender_timezone::rolling::{RollingFileAppender, Rotation};
|
|
|
+
|
|
|
+
|
|
|
+struct ErrorMessageVisitor {
|
|
|
+ message: String
|
|
|
+}
|
|
|
+
|
|
|
+impl Visit for ErrorMessageVisitor {
|
|
|
+ fn record_debug(&mut self, field: &Field, value: &dyn Debug) {
|
|
|
+ if field.name() == "message" {
|
|
|
+ self.message = format!("{:?}", value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+// 错误报告发送到指定服务器
|
|
|
+struct ReportingLayer {
|
|
|
+ account_name: String,
|
|
|
+}
|
|
|
+impl<S> Layer<S> for ReportingLayer
|
|
|
+ where
|
|
|
+ S: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
|
|
|
+{
|
|
|
+ fn on_event(&self, event: &Event<'_>, _ctx: Context<'_, S>) {
|
|
|
+ if event.metadata().level() == &tracing::Level::ERROR {
|
|
|
+ let mut visitor = ErrorMessageVisitor {
|
|
|
+ message: String::new()
|
|
|
+ };
|
|
|
+ event.record(&mut visitor);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+pub fn init_log_with_debug() {
|
|
|
+ let _ = final_init(tracing::Level::DEBUG.as_str(), 0, "test".to_string());
|
|
|
+}
|
|
|
+
|
|
|
+pub fn init_log_with_trace() {
|
|
|
+ let _ = final_init(tracing::Level::TRACE.as_str(), 0, "test".to_string());
|
|
|
+}
|
|
|
+
|
|
|
+pub fn init_log_with_info() {
|
|
|
+ let _ = final_init(tracing::Level::INFO.as_str(), 0, "test".to_string());
|
|
|
+}
|
|
|
+
|
|
|
+pub fn final_init(level: &str, port: u32, account_name: String) -> WorkerGuard {
|
|
|
+ let mut path = String::new();
|
|
|
+ path.push_str("./logs");
|
|
|
+ path.push_str(port.to_string().as_str());
|
|
|
+
|
|
|
+ let file_appender = RollingFileAppender::builder()
|
|
|
+ .time_zone(8)
|
|
|
+ .rotation(Rotation::DAILY)
|
|
|
+ .filename_suffix("log")
|
|
|
+ .build(path)
|
|
|
+ .expect("initializing rolling file appender failed");
|
|
|
+ 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(local_time.clone())
|
|
|
+ .with_target(true)
|
|
|
+ .with_level(true)
|
|
|
+ .with_writer(io::stdout)
|
|
|
+ .with_span_events(fmt::format::FmtSpan::FULL);
|
|
|
+
|
|
|
+ let file_layer = fmt::layer()
|
|
|
+ .with_timer(local_time.clone())
|
|
|
+ .with_target(true)
|
|
|
+ .with_ansi(false)
|
|
|
+ .with_level(true)
|
|
|
+ .with_writer(non_blocking.clone())
|
|
|
+ .with_span_events(fmt::format::FmtSpan::FULL);
|
|
|
+
|
|
|
+ let reporting_layer = ReportingLayer {
|
|
|
+ account_name
|
|
|
+ };
|
|
|
+
|
|
|
+ let layer = tracing_subscriber::Registry::default()
|
|
|
+ .with(fmt_layer)
|
|
|
+ .with(file_layer)
|
|
|
+ .with(reporting_layer)
|
|
|
+ .with(tracing_subscriber::EnvFilter::new(level));
|
|
|
+
|
|
|
+ tracing::subscriber::set_global_default(layer).unwrap();
|
|
|
+
|
|
|
+ return guard;
|
|
|
+}
|