-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "daily" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
log = "0.4" | ||
ftail = { path = "../../../ftail" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use ftail::Ftail; | ||
use log::LevelFilter; | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
Ftail::new().daily("logs", LevelFilter::Trace).init()?; | ||
|
||
log::trace!("This is a trace message"); | ||
|
||
log::debug!("This is a debug message"); | ||
|
||
log::info!(target: "foo", "bar"); | ||
|
||
log::warn!("This is a warning message"); | ||
|
||
log::error!("This is an error message"); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use log::Log; | ||
use std::{ | ||
fs::File, | ||
io::{LineWriter, Write}, | ||
sync::Mutex, | ||
}; | ||
|
||
pub struct DailyLogger { | ||
file: Mutex<LineWriter<File>>, | ||
dir: String, | ||
current_date: Mutex<String>, | ||
} | ||
|
||
impl DailyLogger { | ||
pub fn new(dir: &str) -> Self { | ||
let today = chrono::Local::now().format("%Y-%m-%d").to_string(); | ||
let path = format!("{}/{}.log", dir, today); | ||
|
||
let file = std::fs::OpenOptions::new() | ||
.create(true) | ||
.append(true) | ||
.open(path) | ||
.unwrap(); | ||
|
||
let md = std::fs::metadata(dir).unwrap(); | ||
|
||
if md.permissions().readonly() { | ||
panic!("The logs directory `{dir}` is readonly"); | ||
} | ||
|
||
DailyLogger { | ||
file: Mutex::new(LineWriter::new(file)), | ||
dir: dir.to_string(), | ||
current_date: Mutex::new(today), | ||
} | ||
} | ||
|
||
fn rotate_file_if_needed(&self) { | ||
let today = chrono::Local::now().format("%Y-%m-%d").to_string(); | ||
let mut current_date = self.current_date.lock().unwrap(); | ||
|
||
if *current_date != today { | ||
let path = format!("{}/{}.log", self.dir, today); | ||
|
||
let new_file = std::fs::OpenOptions::new() | ||
.create(true) | ||
.append(true) | ||
.open(path) | ||
.unwrap(); | ||
|
||
let mut file = self.file.lock().unwrap(); | ||
|
||
*file = LineWriter::new(new_file); | ||
*current_date = today; | ||
} | ||
} | ||
} | ||
|
||
impl Log for DailyLogger { | ||
fn enabled(&self, _metadata: &log::Metadata) -> bool { | ||
true | ||
} | ||
|
||
fn log(&self, record: &log::Record) { | ||
self.rotate_file_if_needed(); | ||
|
||
let now = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string(); | ||
|
||
let line = format!( | ||
"{} {} {} {}", | ||
now, | ||
record.level(), | ||
record.target(), | ||
record.args() | ||
); | ||
|
||
let mut file = self.file.lock().unwrap(); | ||
writeln!(file, "{}", line).unwrap(); | ||
file.flush().unwrap(); | ||
} | ||
|
||
fn flush(&self) { | ||
self.file.lock().unwrap().flush().unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod console; | ||
pub mod daily; | ||
pub mod single; | ||
pub mod stdout; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters