Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement daily log file rotation #46

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 55 additions & 21 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ use chrono::Utc;
use std::fs;
#[cfg(not(target_os = "windows"))]
use std::os::unix::fs::symlink;
use std::path::Path;
use std::path::{Path, PathBuf};

pub(crate) struct FilesystemLogger {
file_path: String,
log_dir: String,
level: Level,
}

impl FilesystemLogger {
pub(crate) fn new(log_dir: String, level: Level) -> Result<Self, ()> {
let log_file_name =
format!("ldk_node_{}.log", chrono::offset::Local::now().format("%Y_%m_%d"));
let log_file_name = FilesystemLogger::make_log_file_name();
rolznz marked this conversation as resolved.
Show resolved Hide resolved
let log_file_path = format!("{}/{}", log_dir, log_file_name);

if let Some(parent_dir) = Path::new(&log_file_path).parent() {
Expand All @@ -32,21 +31,59 @@ impl FilesystemLogger {
.open(log_file_path.clone())
.map_err(|e| eprintln!("ERROR: Failed to open log file: {}", e))?;

#[cfg(not(target_os = "windows"))]
{
// Create a symlink to the current log file, with prior cleanup
let log_file_symlink = parent_dir.join("ldk_node_latest.log");
if log_file_symlink.as_path().is_symlink() {
fs::remove_file(&log_file_symlink).map_err(|e| {
eprintln!("ERROR: Failed to remove log file symlink: {}", e)
})?;
}
symlink(&log_file_name, &log_file_symlink)
.map_err(|e| eprintln!("ERROR: Failed to create log file symlink: {}", e))?;
FilesystemLogger::make_log_file_symlink(&parent_dir, &log_file_name)?;
}

Ok(Self { log_dir, level })
}

fn make_log_file_name() -> String {
format!("ldk_node_{}.log", chrono::offset::Local::now().format("%Y_%m_%d"))
}

fn make_log_file_symlink<D: AsRef<Path>, F: AsRef<Path>>(
log_dir: D, log_file_name: F,
) -> Result<(), ()> {
#[cfg(not(target_os = "windows"))]
{
// Create a symlink to the current log file, with prior cleanup
let log_file_symlink = log_dir.as_ref().join("ldk_node_latest.log");
if log_file_symlink.as_path().is_symlink() {
fs::remove_file(&log_file_symlink)
.map_err(|e| eprintln!("ERROR: Failed to remove log file symlink: {}", e))?;
}
symlink(&log_file_name, &log_file_symlink)
.map_err(|e| eprintln!("ERROR: Failed to create log file symlink: {}", e))?;
}

Ok(())
}

fn log_file_path(&self) -> PathBuf {
PathBuf::from(format!("{}/{}", self.log_dir, FilesystemLogger::make_log_file_name()))
}

fn open_log_file(&self) -> Result<fs::File, ()> {
let log_path = self.log_file_path();
let is_new_file = log_path.try_exists().and_then(|e| Ok(!e)).unwrap_or(false);

let ret = fs::OpenOptions::new()
.create(true)
.append(true)
.open(&log_path)
.map_err(|e| eprintln!("ERROR: Failed to open log file: {}", e))?;

if is_new_file {
// Do not check for errors; in concurrent scenarios, this is not
// unlikely to fail. The concurrent thread should be able to finish
// the operation.
let _ = FilesystemLogger::make_log_file_symlink(
&self.log_dir,
log_path.file_name().unwrap(),
);
}

Ok(Self { file_path: log_file_path, level })
Ok(ret)
}
}
impl Logger for FilesystemLogger {
Expand All @@ -63,12 +100,9 @@ impl Logger for FilesystemLogger {
record.line,
raw_log
);
fs::OpenOptions::new()
.create(true)
.append(true)
.open(self.file_path.clone())
self.open_log_file()
.expect("Failed to open log file")
.write_all(log.as_bytes())
.expect("Failed to write to log file")
.expect("Failed to write to log file");
}
}
Loading