From bf5680da6133aab5123f25290848111f84dc342f Mon Sep 17 00:00:00 2001 From: GyDi Date: Sun, 19 Dec 2021 17:50:25 +0800 Subject: [PATCH] feat: change log file format --- src-tauri/Cargo.lock | 1 + src-tauri/Cargo.toml | 1 + src-tauri/src/utils/init.rs | 32 ++++++++++++++++---------------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 5959f87..612eaf9 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -42,6 +42,7 @@ checksum = "8b26702f315f53b6071259e15dd9d64528213b44d61de1ec926eca7715d62203" name = "app" version = "0.1.0" dependencies = [ + "chrono", "dirs 4.0.0", "log", "log4rs", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 1962f3f..235b1f7 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -14,6 +14,7 @@ tauri-build = { version = "1.0.0-beta.4" } [dependencies] dirs = "4.0.0" +chrono = "0.4.19" serde_json = "1.0" serde_yaml = "0.8" serde = { version = "1.0", features = ["derive"] } diff --git a/src-tauri/src/utils/init.rs b/src-tauri/src/utils/init.rs index 519435c..0e1c709 100644 --- a/src-tauri/src/utils/init.rs +++ b/src-tauri/src/utils/init.rs @@ -1,32 +1,30 @@ extern crate serde_yaml; +use chrono::Local; use log::LevelFilter; use log4rs::append::console::ConsoleAppender; use log4rs::append::file::FileAppender; use log4rs::config::{Appender, Config, Root}; use log4rs::encode::pattern::PatternEncoder; -use std::fs; +use std::fs::{self, File}; use std::io::Write; use std::path::PathBuf; -use std::time::{SystemTime, UNIX_EPOCH}; use tauri::PackageInfo; use crate::utils::{app_home_dir, app_resources_dir}; /// initialize this instance's log file fn init_log(log_dir: &PathBuf) { - let log_time = SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap() - .as_secs(); - let log_file = format!("log-{:?}", log_time); + let local_time = Local::now().format("%Y-%m-%d_%H:%M:%S").to_string(); + let log_file = format!("{}.log", local_time); let log_file = log_dir.join(log_file); - let stdout = ConsoleAppender::builder().build(); + let time_format = "{d(%Y-%m-%d %H:%M:%S)} - {m}{n}"; + let stdout = ConsoleAppender::builder() + .encoder(Box::new(PatternEncoder::new(time_format))) + .build(); let tofile = FileAppender::builder() - .encoder(Box::new(PatternEncoder::new( - "{d(%Y-%m-%d %H:%M:%S)} - {m}{n}", - ))) + .encoder(Box::new(PatternEncoder::new(time_format))) .build(log_file) .unwrap(); @@ -62,11 +60,13 @@ fn init_config_file(app_dir: &PathBuf, res_dir: &PathBuf) { fs::copy(clash_tmpl, clash_path).unwrap(); } else { // make sure that the config.yaml not null - let content = "mixed-port: 7890\nallow-lan: false\n".as_bytes(); - fs::File::create(clash_path) - .unwrap() - .write(content) - .unwrap(); + let content = b"\ + mixed-port: 7890\n\ + log-level: info\n\ + allow-lan: false\n\ + external-controller: 127.0.0.1:9090\n\ + secret: \"\"\n"; + File::create(clash_path).unwrap().write(content).unwrap(); } }