Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tjardoo committed Sep 12, 2024
1 parent ed6f897 commit 0def8b6
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 34 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ chrono = "0.4"
members = [
"examples/console",
"examples/formatted_console",
"examples/single",
"examples/daily",
"examples/single_file",
"examples/daily_file",
"examples/stack",
"examples/custom",
]
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Ftail is simple logging implementation for the `log` crate with support for mult

- [Console](#console)
- [Formatted console](#formatted-console)
- [Single](#single)
- [Daily](#daily)
- [Custom](#custom)
- [Single file](#single-file)
- [Daily file](#daily-file)
- [Custom driver](#custom-driver)

## Usage

Expand All @@ -16,10 +16,14 @@ use log::LevelFilter;

Ftail::new()
.console(LevelFilter::Debug)
.daily_file("logs", LevelFilter::Error)
.init()?;

log::trace!("This is a trace message");
log::debug!("This is a debug message");
log::info!("This is an info message");
log::warn!("This is a warning message");
log::error!("This is an error message");
```

## Drivers
Expand Down Expand Up @@ -82,38 +86,38 @@ This is an error message
examples\console\src/main.rs:16
```

### Single
### Single file

Logs to the single log file `logs/demo.log`.

The `single` driver takes the following parameters:
The `single_file` 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)
.single_file("logs/demo.log", true, LevelFilter::Trace)
.init()?;
```

### Daily
### Daily file

Logs to a daily log file in the `logs` directory.

The `daily` driver takes the following parameters:
The `daily_file` driver takes the following parameters:

- `dir`: the directory to store the log files
- `level`: the minumum log level to log

```rust
Ftail::new()
.daily("logs", LevelFilter::Trace)
.daily_file("logs", LevelFilter::Trace)
.init()?;
```

### Custom
### Custom driver

Create your own log driver.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "daily"
name = "daily_file"
version = "0.1.0"
edition = "2021"
publish = false
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use log::LevelFilter;
// This example demonstrates how to log messages to a daily log file in the logs directory.

fn main() -> Result<(), Box<dyn std::error::Error>> {
Ftail::new().daily("logs", LevelFilter::Trace).init()?;
Ftail::new().daily_file("logs", LevelFilter::Trace).init()?;

log::trace!("This is a trace message");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "single"
name = "single_file"
version = "0.1.0"
edition = "2021"
publish = false
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use log::LevelFilter;

fn main() -> Result<(), Box<dyn std::error::Error>> {
Ftail::new()
.single("logs/demo.log", true, LevelFilter::Trace)
.single_file("logs/demo.log", true, LevelFilter::Trace)
.init()?;

log::trace!("This is a trace message");
Expand Down
5 changes: 3 additions & 2 deletions examples/stack/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use log::LevelFilter;

fn main() -> Result<(), Box<dyn std::error::Error>> {
Ftail::new()
.single("logs/trace.log", true, LevelFilter::Trace)
.single("logs/error.log", true, LevelFilter::Error)
.console(LevelFilter::Info)
.single_file("logs/trace.log", true, LevelFilter::Trace)
.single_file("logs/error.log", true, LevelFilter::Error)
.init()?;

log::trace!("This is a trace message");
Expand Down
8 changes: 4 additions & 4 deletions src/drivers/daily.rs → src/drivers/daily_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ use crate::{
};

/// A logger that logs messages to a daily log file.
pub struct DailyLogger {
pub struct DailyFileLogger {
file: Mutex<LineWriter<File>>,
dir: String,
current_date: Mutex<String>,
}

impl DailyLogger {
impl DailyFileLogger {
pub fn new(dir: &str) -> Result<Self, FtailError> {
let today = chrono::Local::now().format("%Y-%m-%d").to_string();
let path = format!("{}/{}.log", dir, today);
Expand All @@ -34,7 +34,7 @@ impl DailyLogger {
return Err(FtailError::PermissionsError(dir.to_string()));
}

Ok(DailyLogger {
Ok(DailyFileLogger {
file: Mutex::new(LineWriter::new(file)),
dir: dir.to_string(),
current_date: Mutex::new(today),
Expand Down Expand Up @@ -62,7 +62,7 @@ impl DailyLogger {
}
}

impl Log for DailyLogger {
impl Log for DailyFileLogger {
fn enabled(&self, _metadata: &log::Metadata) -> bool {
true
}
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod console;
pub mod daily;
pub mod daily_file;
pub mod formatted_console;
pub mod single;
pub mod single_file;
8 changes: 4 additions & 4 deletions src/drivers/single.rs → src/drivers/single_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use crate::{
};

/// A logger that logs messages to a single log file.
pub struct SingleLogger {
pub struct SingleFileLogger {
file: Mutex<LineWriter<File>>,
}

impl SingleLogger {
impl SingleFileLogger {
pub fn new(path: &str, append: bool) -> Result<Self, FtailError> {
let file = std::fs::OpenOptions::new()
.create(true)
Expand All @@ -30,13 +30,13 @@ impl SingleLogger {
return Err(FtailError::PermissionsError(path.to_string()));
}

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

impl Log for SingleLogger {
impl Log for SingleFileLogger {
fn enabled(&self, _metadata: &log::Metadata) -> bool {
true
}
Expand Down
163 changes: 157 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,154 @@
//! # Ftail
//!
//! Ftail is simple logging implementation for the `log` crate with support for multiple drivers.
//!
//! - [Console](#console)
//! - [Formatted console](#formatted-console)
//! - [Single file](#single-file)
//! - [Daily file](#daily-file)
//! - [Custom driver](#custom-driver)
//!
//! ## Usage
//!
//! ```rust
//! use ftail::Ftail;
//! use log::LevelFilter;
//!
//! Ftail::new()
//! .console(LevelFilter::Debug)
//! .daily_file("logs", LevelFilter::Error)
//! .init()?;
//!
//! log::trace!("This is a trace message");
//! log::debug!("This is a debug message");
//! log::info!("This is an info message");
//! log::warn!("This is a warning message");
//! log::error!("This is an error message");
//! ```
//!
//! ## Drivers
//!
//! ### Console
//!
//! Logs to the standard output without any formatting.
//!
//! The `stdout` driver takes the following parameters:
//!
//! - `level`: the minumum log level to log
//!
//! ```rust
//! Ftail::new()
//! .console(LevelFilter::Trace)
//! .init()?;
//! ```
//!
//! ```log
//! 2024-09-10 14:41:57 TRACE stdout This is a trace message
//! 2024-09-10 14:41:57 DEBUG stdout This is a debug message
//! 2024-09-10 14:41:57 INFO foo bar
//! 2024-09-10 14:41:57 WARN stdout This is a warning message
//! 2024-09-10 14:41:57 ERROR stdout This is an error message
//! ```
//!
//! ### Formatted Console
//!
//! 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()
//! .formatted_console(LevelFilter::Trace)
//! .init()?;
//! ```
//!
//! ```log
//! 2024-09-10 14:42:21 · TRACE
//! This is a trace message
//! examples\console\src/main.rs:8
//!
//! 2024-09-10 14:42:21 · DEBUG
//! This is a debug message
//! examples\console\src/main.rs:10
//!
//! 2024-09-10 14:42:21 · INFO
//! bar
//! examples\console\src/main.rs:12
//!
//! 2024-09-10 14:42:21 · WARN
//! This is a warning message
//! examples\console\src/main.rs:14
//!
//! 2024-09-10 14:42:21 · ERROR
//! This is an error message
//! examples\console\src/main.rs:16
//! ```
//!
//! ### Single file
//!
//! Logs to the single log file `logs/demo.log`.
//!
//! The `single_file` 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_file("logs/demo.log", true, LevelFilter::Trace)
//! .init()?;
//! ```
//!
//! ### Daily file
//!
//! Logs to a daily log file in the `logs` directory.
//!
//! The `daily_file` driver takes the following parameters:
//!
//! - `dir`: the directory to store the log files
//! - `level`: the minumum log level to log
//!
//! ```rust
//! Ftail::new()
//! .daily_file("logs", LevelFilter::Trace)
//! .init()?;
//! ```
//!
//! ### Custom driver
//!
//! Create your own log driver.
//!
//! You can add text formatting, by using the `use ftail::ansi_escape::TextStyling;` module.
//!
//! ```rust
//! Ftail::new()
//! .custom(Box::new(Box::new(CustomLogger {})), LevelFilter::Debug)
//! .init()?;
//!
//! // the custom logger implementation
//! struct CustomLogger {}
//!
//! impl Log for CustomLogger {
//! fn enabled(&self, _metadata: &log::Metadata) -> bool {
//! true
//! }
//!
//! fn log(&self, record: &log::Record) {
//! let time = chrono::Local::now().format("%H:%M:%S").to_string();
//!
//! println!("{} {} {}", time, record.level(), record.args());
//! }
//!
//! fn flush(&self) {}
//! }
//! ```
use drivers::{
console::ConsoleLogger, daily::DailyLogger, formatted_console::FormattedConsoleLogger,
single::SingleLogger,
console::ConsoleLogger, daily_file::DailyFileLogger, formatted_console::FormattedConsoleLogger,
single_file::SingleFileLogger,
};
use error::FtailError;
use log::Log;
Expand Down Expand Up @@ -49,13 +197,16 @@ impl Ftail {
}

/// Add a driver that logs messages to a single file.
pub fn single(self, path: &str, append: bool, level: log::LevelFilter) -> Self {
self.add_driver(Box::new(SingleLogger::new(path, append).unwrap()), level)
pub fn single_file(self, path: &str, append: bool, level: log::LevelFilter) -> Self {
self.add_driver(
Box::new(SingleFileLogger::new(path, append).unwrap()),
level,
)
}

/// Add a driver that logs messages to a daily log file.
pub fn daily(self, path: &str, level: log::LevelFilter) -> Self {
self.add_driver(Box::new(DailyLogger::new(path).unwrap()), level)
pub fn daily_file(self, path: &str, level: log::LevelFilter) -> Self {
self.add_driver(Box::new(DailyFileLogger::new(path).unwrap()), level)
}

/// Add a custom driver.
Expand Down

0 comments on commit 0def8b6

Please sign in to comment.