refactor(log): use time macro (#2604)

pull/2611/head
Tony 2 months ago committed by GitHub
parent 4cd625049d
commit 2445e103f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -31,7 +31,7 @@ thiserror = { workspace = true }
serde_repr = "0.1" serde_repr = "0.1"
byte-unit = "5" byte-unit = "5"
log = { workspace = true, features = ["kv_unstable"] } log = { workspace = true, features = ["kv_unstable"] }
time = { version = "0.3", features = ["formatting", "local-offset"] } time = { version = "0.3", features = ["formatting", "local-offset", "macros"] }
fern = "0.7" fern = "0.7"
tracing = { workspace = true, optional = true } tracing = { workspace = true, optional = true }

@ -27,9 +27,9 @@ use tauri::{
Manager, Runtime, Manager, Runtime,
}; };
use tauri::{AppHandle, Emitter}; use tauri::{AppHandle, Emitter};
use time::{macros::format_description, OffsetDateTime};
pub use fern; pub use fern;
use time::OffsetDateTime;
pub const WEBVIEW_TARGET: &str = "webview"; pub const WEBVIEW_TARGET: &str = "webview";
@ -277,9 +277,7 @@ pub struct Builder {
impl Default for Builder { impl Default for Builder {
fn default() -> Self { fn default() -> Self {
#[cfg(desktop)] #[cfg(desktop)]
let format = let format = format_description!("[[[year]-[month]-[day]][[[hour]:[minute]:[second]]");
time::format_description::parse("[[[year]-[month]-[day]][[[hour]:[minute]:[second]]")
.unwrap();
let dispatch = fern::Dispatch::new().format(move |out, message, record| { let dispatch = fern::Dispatch::new().format(move |out, message, record| {
out.finish( out.finish(
#[cfg(mobile)] #[cfg(mobile)]
@ -318,9 +316,7 @@ impl Builder {
pub fn timezone_strategy(mut self, timezone_strategy: TimezoneStrategy) -> Self { pub fn timezone_strategy(mut self, timezone_strategy: TimezoneStrategy) -> Self {
self.timezone_strategy = timezone_strategy.clone(); self.timezone_strategy = timezone_strategy.clone();
let format = let format = format_description!("[[[year]-[month]-[day]][[[hour]:[minute]:[second]]");
time::format_description::parse("[[[year]-[month]-[day]][[[hour]:[minute]:[second]]")
.unwrap();
self.dispatch = self.dispatch.format(move |out, message, record| { self.dispatch = self.dispatch.format(move |out, message, record| {
out.finish(format_args!( out.finish(format_args!(
"{}[{}][{}] {}", "{}[{}][{}] {}",
@ -416,9 +412,7 @@ impl Builder {
#[cfg(feature = "colored")] #[cfg(feature = "colored")]
pub fn with_colors(self, colors: fern::colors::ColoredLevelConfig) -> Self { pub fn with_colors(self, colors: fern::colors::ColoredLevelConfig) -> Self {
let format = let format = format_description!("[[[year]-[month]-[day]][[[hour]:[minute]:[second]]");
time::format_description::parse("[[[year]-[month]-[day]][[[hour]:[minute]:[second]]")
.unwrap();
let timezone_strategy = self.timezone_strategy.clone(); let timezone_strategy = self.timezone_strategy.clone();
self.format(move |out, message, record| { self.format(move |out, message, record| {
@ -600,11 +594,9 @@ fn get_log_file_path(
let to = dir.as_ref().join(format!( let to = dir.as_ref().join(format!(
"{}_{}.log", "{}_{}.log",
file_name, file_name,
timezone_strategy timezone_strategy.get_now().format(&format_description!(
.get_now() "[year]-[month]-[day]_[hour]-[minute]-[second]"
.format(&time::format_description::parse( ))?,
"[year]-[month]-[day]_[hour]-[minute]-[second]"
)?)?,
)); ));
if to.is_file() { if to.is_file() {
// designated rotated log file name already exists // designated rotated log file name already exists

Loading…
Cancel
Save