Fixed the conversion of log paths (FileSpec) to String

This commit is contained in:
Thorsten Sommer 2025-03-03 11:45:02 +01:00
parent dbc2b4f6bc
commit fe60d22f27
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -1,6 +1,7 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::error::Error; use std::error::Error;
use std::fmt::Debug; use std::fmt::Debug;
use std::path;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::OnceLock; use std::sync::OnceLock;
use flexi_logger::{DeferredNow, Duplicate, FileSpec, Logger, LoggerHandle}; use flexi_logger::{DeferredNow, Duplicate, FileSpec, Logger, LoggerHandle};
@ -56,7 +57,7 @@ pub fn init_logging() {
.suffix("log"); .suffix("log");
// Store the startup log path: // Store the startup log path:
LOG_STARTUP_PATH.set(log_path.as_pathbuf(None).canonicalize().unwrap().to_str().unwrap().to_string()).expect("Cannot store the startup log path"); let _ = LOG_STARTUP_PATH.set(convert_log_path_to_string(&log_path));
let runtime_logger = Logger::try_with_str(log_config).expect("Cannot create logging") let runtime_logger = Logger::try_with_str(log_config).expect("Cannot create logging")
.log_to_file(log_path) .log_to_file(log_path)
@ -81,7 +82,7 @@ pub fn switch_to_file_logging(logger_path: PathBuf) -> Result<(), Box<dyn Error>
.basename("events") .basename("events")
.suppress_timestamp() .suppress_timestamp()
.suffix("log"); .suffix("log");
LOG_APP_PATH.set(log_path.as_pathbuf(None).to_str().unwrap().to_string()).expect("Cannot store the app log path"); let _ = LOG_APP_PATH.set(convert_log_path_to_string(&log_path));
LOGGER.get().expect("No LOGGER was set").handle.reset_flw(&FileLogWriter::builder(log_path))?; LOGGER.get().expect("No LOGGER was set").handle.reset_flw(&FileLogWriter::builder(log_path))?;
Ok(()) Ok(())
@ -176,6 +177,18 @@ fn file_logger_format(
write!(w, "{}", &record.args()) write!(w, "{}", &record.args())
} }
fn convert_log_path_to_string(log_path: &FileSpec) -> String {
let log_path = log_path.as_pathbuf(None);
match path::absolute(log_path) {
Ok(log_path) => match log_path.to_str() {
Some(path_string) => path_string.to_string(),
None => String::from(""),
}.to_string(),
Err(_) => String::from(""),
}
}
#[get("/log/paths")] #[get("/log/paths")]
pub async fn get_log_paths(_token: APIToken) -> Json<LogPathsResponse> { pub async fn get_log_paths(_token: APIToken) -> Json<LogPathsResponse> {
Json(LogPathsResponse { Json(LogPathsResponse {