|
|
@ -21,9 +21,11 @@ use tauri::{
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
pub use fern;
|
|
|
|
pub use fern;
|
|
|
|
|
|
|
|
use time::OffsetDateTime;
|
|
|
|
|
|
|
|
|
|
|
|
const DEFAULT_MAX_FILE_SIZE: u128 = 40000;
|
|
|
|
const DEFAULT_MAX_FILE_SIZE: u128 = 40000;
|
|
|
|
const DEFAULT_ROTATION_STRATEGY: RotationStrategy = RotationStrategy::KeepOne;
|
|
|
|
const DEFAULT_ROTATION_STRATEGY: RotationStrategy = RotationStrategy::KeepOne;
|
|
|
|
|
|
|
|
const DEFAULT_TIMEZONE_STRATEGY: TimezoneStrategy = TimezoneStrategy::UseUtc;
|
|
|
|
const DEFAULT_LOG_TARGETS: [LogTarget; 2] = [LogTarget::Stdout, LogTarget::LogDir];
|
|
|
|
const DEFAULT_LOG_TARGETS: [LogTarget; 2] = [LogTarget::Stdout, LogTarget::LogDir];
|
|
|
|
|
|
|
|
|
|
|
|
/// An enum representing the available verbosity levels of the logger.
|
|
|
|
/// An enum representing the available verbosity levels of the logger.
|
|
|
@ -83,6 +85,24 @@ pub enum RotationStrategy {
|
|
|
|
KeepOne,
|
|
|
|
KeepOne,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
|
|
|
pub enum TimezoneStrategy {
|
|
|
|
|
|
|
|
UseUtc,
|
|
|
|
|
|
|
|
UseLocal,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl TimezoneStrategy {
|
|
|
|
|
|
|
|
pub fn get_now(&self) -> OffsetDateTime {
|
|
|
|
|
|
|
|
match self {
|
|
|
|
|
|
|
|
TimezoneStrategy::UseUtc => OffsetDateTime::now_utc(),
|
|
|
|
|
|
|
|
TimezoneStrategy::UseLocal => match OffsetDateTime::now_local() {
|
|
|
|
|
|
|
|
Ok(v) => v,
|
|
|
|
|
|
|
|
Err(_) => OffsetDateTime::now_utc() // Fallback to UTC since Rust cannot determine local timezone
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Clone)]
|
|
|
|
#[derive(Debug, Serialize, Clone)]
|
|
|
|
struct RecordPayload {
|
|
|
|
struct RecordPayload {
|
|
|
|
message: String,
|
|
|
|
message: String,
|
|
|
@ -145,6 +165,7 @@ fn log(
|
|
|
|
pub struct Builder {
|
|
|
|
pub struct Builder {
|
|
|
|
dispatch: fern::Dispatch,
|
|
|
|
dispatch: fern::Dispatch,
|
|
|
|
rotation_strategy: RotationStrategy,
|
|
|
|
rotation_strategy: RotationStrategy,
|
|
|
|
|
|
|
|
timezone_strategy: TimezoneStrategy,
|
|
|
|
max_file_size: u128,
|
|
|
|
max_file_size: u128,
|
|
|
|
targets: Vec<LogTarget>,
|
|
|
|
targets: Vec<LogTarget>,
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -157,7 +178,7 @@ impl Default for Builder {
|
|
|
|
let dispatch = fern::Dispatch::new().format(move |out, message, record| {
|
|
|
|
let dispatch = fern::Dispatch::new().format(move |out, message, record| {
|
|
|
|
out.finish(format_args!(
|
|
|
|
out.finish(format_args!(
|
|
|
|
"{}[{}][{}] {}",
|
|
|
|
"{}[{}][{}] {}",
|
|
|
|
time::OffsetDateTime::now_utc().format(&format).unwrap(),
|
|
|
|
DEFAULT_TIMEZONE_STRATEGY.get_now().format(&format).unwrap(),
|
|
|
|
record.target(),
|
|
|
|
record.target(),
|
|
|
|
record.level(),
|
|
|
|
record.level(),
|
|
|
|
message
|
|
|
|
message
|
|
|
@ -166,6 +187,7 @@ impl Default for Builder {
|
|
|
|
Self {
|
|
|
|
Self {
|
|
|
|
dispatch,
|
|
|
|
dispatch,
|
|
|
|
rotation_strategy: DEFAULT_ROTATION_STRATEGY,
|
|
|
|
rotation_strategy: DEFAULT_ROTATION_STRATEGY,
|
|
|
|
|
|
|
|
timezone_strategy: DEFAULT_TIMEZONE_STRATEGY,
|
|
|
|
max_file_size: DEFAULT_MAX_FILE_SIZE,
|
|
|
|
max_file_size: DEFAULT_MAX_FILE_SIZE,
|
|
|
|
targets: DEFAULT_LOG_TARGETS.into(),
|
|
|
|
targets: DEFAULT_LOG_TARGETS.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -182,6 +204,24 @@ impl Builder {
|
|
|
|
self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn timezone_strategy(mut self, timezone_strategy: TimezoneStrategy) -> Self {
|
|
|
|
|
|
|
|
self.timezone_strategy = timezone_strategy.clone();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let format =
|
|
|
|
|
|
|
|
time::format_description::parse("[[[year]-[month]-[day]][[[hour]:[minute]:[second]]")
|
|
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
self.dispatch = fern::Dispatch::new().format(move |out, message, record| {
|
|
|
|
|
|
|
|
out.finish(format_args!(
|
|
|
|
|
|
|
|
"{}[{}][{}] {}",
|
|
|
|
|
|
|
|
timezone_strategy.get_now().format(&format).unwrap(),
|
|
|
|
|
|
|
|
record.target(),
|
|
|
|
|
|
|
|
record.level(),
|
|
|
|
|
|
|
|
message
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
self
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn max_file_size(mut self, max_file_size: u128) -> Self {
|
|
|
|
pub fn max_file_size(mut self, max_file_size: u128) -> Self {
|
|
|
|
self.max_file_size = max_file_size;
|
|
|
|
self.max_file_size = max_file_size;
|
|
|
|
self
|
|
|
|
self
|
|
|
@ -231,7 +271,7 @@ impl Builder {
|
|
|
|
self.format(move |out, message, record| {
|
|
|
|
self.format(move |out, message, record| {
|
|
|
|
out.finish(format_args!(
|
|
|
|
out.finish(format_args!(
|
|
|
|
"{}[{}][{}] {}",
|
|
|
|
"{}[{}][{}] {}",
|
|
|
|
time::OffsetDateTime::now_utc().format(&format).unwrap(),
|
|
|
|
self.timezone_strategy.get_now().format(&format).unwrap(),
|
|
|
|
record.target(),
|
|
|
|
record.target(),
|
|
|
|
colors.color(record.level()),
|
|
|
|
colors.color(record.level()),
|
|
|
|
message
|
|
|
|
message
|
|
|
@ -259,6 +299,7 @@ impl Builder {
|
|
|
|
&path,
|
|
|
|
&path,
|
|
|
|
app_name,
|
|
|
|
app_name,
|
|
|
|
&self.rotation_strategy,
|
|
|
|
&self.rotation_strategy,
|
|
|
|
|
|
|
|
&self.timezone_strategy,
|
|
|
|
self.max_file_size,
|
|
|
|
self.max_file_size,
|
|
|
|
)?)?
|
|
|
|
)?)?
|
|
|
|
.into()
|
|
|
|
.into()
|
|
|
@ -273,6 +314,7 @@ impl Builder {
|
|
|
|
&path,
|
|
|
|
&path,
|
|
|
|
app_name,
|
|
|
|
app_name,
|
|
|
|
&self.rotation_strategy,
|
|
|
|
&self.rotation_strategy,
|
|
|
|
|
|
|
|
&self.timezone_strategy,
|
|
|
|
self.max_file_size,
|
|
|
|
self.max_file_size,
|
|
|
|
)?)?
|
|
|
|
)?)?
|
|
|
|
.into()
|
|
|
|
.into()
|
|
|
@ -306,6 +348,7 @@ fn get_log_file_path(
|
|
|
|
dir: &impl AsRef<Path>,
|
|
|
|
dir: &impl AsRef<Path>,
|
|
|
|
app_name: &str,
|
|
|
|
app_name: &str,
|
|
|
|
rotation_strategy: &RotationStrategy,
|
|
|
|
rotation_strategy: &RotationStrategy,
|
|
|
|
|
|
|
|
timezone_strategy: &TimezoneStrategy,
|
|
|
|
max_file_size: u128,
|
|
|
|
max_file_size: u128,
|
|
|
|
) -> plugin::Result<PathBuf> {
|
|
|
|
) -> plugin::Result<PathBuf> {
|
|
|
|
let path = dir.as_ref().join(format!("{app_name}.log"));
|
|
|
|
let path = dir.as_ref().join(format!("{app_name}.log"));
|
|
|
@ -318,7 +361,7 @@ fn get_log_file_path(
|
|
|
|
let to = dir.as_ref().join(format!(
|
|
|
|
let to = dir.as_ref().join(format!(
|
|
|
|
"{}_{}.log",
|
|
|
|
"{}_{}.log",
|
|
|
|
app_name,
|
|
|
|
app_name,
|
|
|
|
time::OffsetDateTime::now_utc()
|
|
|
|
timezone_strategy.get_now()
|
|
|
|
.format(
|
|
|
|
.format(
|
|
|
|
&time::format_description::parse(
|
|
|
|
&time::format_description::parse(
|
|
|
|
"[year]-[month]-[day]_[hour]-[minute]-[second]"
|
|
|
|
"[year]-[month]-[day]_[hour]-[minute]-[second]"
|
|
|
|