Skip to content

Commit

Permalink
Add single driver
Browse files Browse the repository at this point in the history
  • Loading branch information
tjardoo committed Sep 11, 2024
1 parent b213b25 commit 0d53aad
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 11 deletions.
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ log = { version = "0.4", features = ["std"] }
chrono = "0.4"

[workspace]
members = ["examples/console", "examples/custom", "examples/stack", "examples/stdout"]
members = [
"examples/console",
"examples/custom",
"examples/single",
"examples/stack",
"examples/stdout",
]
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ log::info!("This is an info message");

Logs to the standard output.

The `stdout` driver takes the following parameters:

- `level`: the minumum log level to log

```rust
Ftail::new()
.stdout(LevelFilter::Trace)
Expand All @@ -46,6 +50,10 @@ Ftail::new()

Logs to the standard output with formatted and colored output.

The `console` driver takes the following parameters:

- `level`: the minumum log level to log

```rust
Ftail::new()
.console(LevelFilter::Trace)
Expand Down Expand Up @@ -76,7 +84,19 @@ examples\console\src/main.rs:16

### Single

TODO
Logs to a single file.

The `single` driver takes the following parameters:

- `path`: the path to the log file
- `append`: whether to append to the log file or overwrite it
- `level`: the minumum log level to log

```rust
Ftail::new()
.single("logs/demo.log", true, LevelFilter::Trace)
.init()?;
```

### Daily

Expand Down
4 changes: 2 additions & 2 deletions examples/console/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

log::info!(target: "foo", "bar");

log::warn!("This is a warning");
log::warn!("This is a warning message");

log::error!("This is an error");
log::error!("This is an error message");

Ok(())
}
4 changes: 2 additions & 2 deletions examples/custom/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

log::info!(target: "foo", "bar");

log::warn!("This is a warning");
log::warn!("This is a warning message");

log::error!("This is an error");
log::error!("This is an error message");

Ok(())
}
Expand Down
9 changes: 9 additions & 0 deletions examples/single/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "single"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
log = "0.4"
ftail = { path = "../../../ftail" }
1 change: 1 addition & 0 deletions examples/single/logs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
demo.log
20 changes: 20 additions & 0 deletions examples/single/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use ftail::Ftail;
use log::LevelFilter;

fn main() -> Result<(), Box<dyn std::error::Error>> {
Ftail::new()
.single("logs/demo.log", true, 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(())
}
4 changes: 2 additions & 2 deletions examples/stack/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

log::info!(target: "foo", "bar");

log::warn!("This is a warning");
log::warn!("This is a warning message");

log::error!("This is an error");
log::error!("This is an error message");

Ok(())
}
4 changes: 2 additions & 2 deletions examples/stdout/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

log::info!(target: "foo", "bar");

log::warn!("This is a warning");
log::warn!("This is a warning message");

log::error!("This is an error");
log::error!("This is an error message");

Ok(())
}
1 change: 1 addition & 0 deletions src/drivers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod console;
pub mod single;
pub mod stdout;
61 changes: 61 additions & 0 deletions src/drivers/single.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use log::Log;
use std::{
fs::File,
io::{LineWriter, Write},
sync::Mutex,
};

pub struct SingleLogger {
file: Mutex<LineWriter<File>>,
}

impl SingleLogger {
pub fn new(path: &str, append: bool) -> Self {
let file = std::fs::OpenOptions::new()
.create(true)
.write(true)
.append(append)
.open(path)
.unwrap();

// @todo
// let md = std::fs::metadata("logs").unwrap();
// let permissions = md.permissions();

// if permissions.readonly() {
// return Err(P2000Error::ConfigurationError(format!(
// "directory /logs is readonly"
// )));
// }

SingleLogger {
file: Mutex::new(LineWriter::new(file)),
}
}
}

impl Log for SingleLogger {
fn enabled(&self, _metadata: &log::Metadata) -> bool {
true
}

fn log(&self, record: &log::Record) {
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();
}
}
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use drivers::{console::ConsoleLogger, stdout::StdOutLogger};
use drivers::{console::ConsoleLogger, single::SingleLogger, stdout::StdOutLogger};
use error::FtailError;
use log::Log;

Expand Down Expand Up @@ -36,6 +36,10 @@ impl Ftail {
self.add_driver(Box::new(Box::new(ConsoleLogger {})), level)
}

pub fn single(self, path: &str, append: bool, level: log::LevelFilter) -> Self {
self.add_driver(Box::new(SingleLogger::new(path, append)), level)
}

pub fn custom(self, driver: Box<dyn Log>, level: log::LevelFilter) -> Self {
self.add_driver(Box::new(driver), level)
}
Expand Down

0 comments on commit 0d53aad

Please sign in to comment.