Skip to content

Commit

Permalink
Merge branch 'master' of github.com:tjardoo/ftail
Browse files Browse the repository at this point in the history
  • Loading branch information
tjardoo committed Sep 17, 2024
2 parents 9551eca + 598111c commit 3f0be47
Show file tree
Hide file tree
Showing 13 changed files with 447 additions and 31 deletions.
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"
license = "MIT"
description = "Ftail is simple logging implementation for the `log` crate with support for multiple drivers."
repository = "https://github.com/tjardoo/ftail"
documentation = "https://docs.rs/ftail"
keywords = ["log", "logging", "filelog"]

[dependencies]
Expand All @@ -16,6 +17,9 @@ chrono-tz = { version = "0.10", optional = true }
default = []
timezone = ["chrono-tz"]

[dev-dependencies]
ftail = { path = ".", features = ["timezone"] }

[workspace]
members = [
"examples/console",
Expand All @@ -25,3 +29,6 @@ members = [
"examples/stack",
"examples/custom",
]

[lib]
doctest = false
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ You can set the following configuration options:
- `.datetime_format("%Y-%m-%d %H:%M:%S.3f")` to set the datetime format
- `.timezone(ftail::Tz::UTC)` to set the timezone [requires feature `timezone`]
- `.max_file_size(100)` to set the maximum file size in MB (will move older logs to .old{N})
- `.filter_levels(vec![Level::Debug, Level::Error])` only log messages with the specified levels
- `.filter_targets(vec!["foo", "bar"])` only log messages with the specified targets

## Drivers

Expand Down Expand Up @@ -152,11 +154,19 @@ struct CustomLogger {
}

impl Log for CustomLogger {
fn enabled(&self, _metadata: &log::Metadata) -> bool {
true
fn enabled(&self, metadata: &log::Metadata) -> bool {
if self.config.level_filter == LevelFilter::Off {
return true;
}

metadata.level() <= self.config.level_filter
}

fn log(&self, record: &log::Record) {
if !self.enabled(record.metadata()) {
return;
}

let time = chrono::Local::now()
.format(&self.config.datetime_format)
.to_string();
Expand Down
15 changes: 12 additions & 3 deletions examples/console/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
use ftail::Ftail;
use log::LevelFilter;
use log::{Level, LevelFilter};
use std::vec;

// This example demonstrates how to log messages to stdout.

fn main() -> Result<(), Box<dyn std::error::Error>> {
Ftail::new()
// .timezone(ftail::Tz::UTC)
.timezone(ftail::Tz::Europe__Amsterdam)
.datetime_format("%d-%m-%Y %H:%M:%S")
.timezone(ftail::Tz::Europe__Amsterdam) // optional
.datetime_format("%d-%m-%Y %H:%M:%S%.3f") // optional
.console(LevelFilter::Off)
.filter_levels(vec![
Level::Trace,
Level::Debug,
Level::Info,
// Level::Warn,
Level::Error,
]) // optional
.filter_targets(vec!["foo"]) // optional
.init()?;

log::trace!("This is a trace message");
Expand Down
12 changes: 10 additions & 2 deletions examples/custom/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@ struct CustomLogger {
}

impl Log for CustomLogger {
fn enabled(&self, _metadata: &log::Metadata) -> bool {
true
fn enabled(&self, metadata: &log::Metadata) -> bool {
if self.config.level_filter == LevelFilter::Off {
return true;
}

metadata.level() <= self.config.level_filter
}

fn log(&self, record: &log::Record) {
if !self.enabled(record.metadata()) {
return;
}

let time = chrono::Local::now()
.format(&self.config.datetime_format)
.to_string();
Expand Down
14 changes: 11 additions & 3 deletions src/drivers/console.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use log::Log;
use log::{LevelFilter, Log};

use crate::{
formatters::{default::DefaultFormatter, Formatter},
Expand All @@ -17,11 +17,19 @@ impl ConsoleLogger {
}

impl Log for ConsoleLogger {
fn enabled(&self, _metadata: &log::Metadata) -> bool {
true
fn enabled(&self, metadata: &log::Metadata) -> bool {
if self.config.level_filter == LevelFilter::Off {
return true;
}

metadata.level() <= self.config.level_filter
}

fn log(&self, record: &log::Record) {
if !self.enabled(record.metadata()) {
return;
}

let formatter = DefaultFormatter::new(record, &self.config);

println!("{}", formatter.format());
Expand Down
14 changes: 11 additions & 3 deletions src/drivers/daily_file.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use log::Log;
use log::{LevelFilter, Log};
use std::{
fs::File,
io::{LineWriter, Write},
Expand Down Expand Up @@ -70,11 +70,19 @@ impl DailyFileLogger {
}

impl Log for DailyFileLogger {
fn enabled(&self, _metadata: &log::Metadata) -> bool {
true
fn enabled(&self, metadata: &log::Metadata) -> bool {
if self.config.level_filter == LevelFilter::Off {
return true;
}

metadata.level() <= self.config.level_filter
}

fn log(&self, record: &log::Record) {
if !self.enabled(record.metadata()) {
return;
}

rotate_if_exceeds_max_file_size(&self.file, self.file_path.clone(), &self.config);
self.rotate_daily_file();

Expand Down
14 changes: 11 additions & 3 deletions src/drivers/formatted_console.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use log::Log;
use log::{LevelFilter, Log};

use crate::{
formatters::{readable::ReadableFormatter, Formatter},
Expand All @@ -17,11 +17,19 @@ impl FormattedConsoleLogger {
}

impl Log for FormattedConsoleLogger {
fn enabled(&self, _metadata: &log::Metadata) -> bool {
true
fn enabled(&self, metadata: &log::Metadata) -> bool {
if self.config.level_filter == LevelFilter::Off {
return true;
}

metadata.level() <= self.config.level_filter
}

fn log(&self, record: &log::Record) {
if !self.enabled(record.metadata()) {
return;
}

let formatter = ReadableFormatter::new(record, &self.config);

println!("{}", formatter.format());
Expand Down
2 changes: 2 additions & 0 deletions src/drivers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ pub mod console;
pub mod daily_file;
pub mod formatted_console;
pub mod single_file;
#[cfg(test)]
pub mod test;
14 changes: 11 additions & 3 deletions src/drivers/single_file.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use log::Log;
use log::{LevelFilter, Log};
use std::{
fs::File,
io::{LineWriter, Write},
Expand Down Expand Up @@ -44,11 +44,19 @@ impl SingleFileLogger {
}

impl Log for SingleFileLogger {
fn enabled(&self, _metadata: &log::Metadata) -> bool {
true
fn enabled(&self, metadata: &log::Metadata) -> bool {
if self.config.level_filter == LevelFilter::Off {
return true;
}

metadata.level() <= self.config.level_filter
}

fn log(&self, record: &log::Record) {
if !self.enabled(record.metadata()) {
return;
}

rotate_if_exceeds_max_file_size(&self.file, self.file_path.clone(), &self.config);

let formatter = DefaultFormatter::new(record, &self.config);
Expand Down
44 changes: 44 additions & 0 deletions src/drivers/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::{
formatters::{default::DefaultFormatter, Formatter},
Config,
};
use log::{LevelFilter, Log};
use std::sync::{Arc, Mutex};

pub struct TestLogger {
pub buffer: Arc<Mutex<Vec<String>>>,
pub config: Config,
}

impl TestLogger {
#[cfg(test)]
pub fn new(config: Config) -> Self {
TestLogger {
buffer: Arc::new(Mutex::new(Vec::new())),
config,
}
}
}

impl Log for TestLogger {
fn enabled(&self, metadata: &log::Metadata) -> bool {
if self.config.level_filter == LevelFilter::Off {
return true;
}

metadata.level() <= self.config.level_filter
}

fn log(&self, record: &log::Record) {
if !self.enabled(record.metadata()) {
return;
}

let formatter = DefaultFormatter::new(record, &self.config);

let mut buffer = self.buffer.lock().unwrap();
buffer.push(formatter.format());
}

fn flush(&self) {}
}
5 changes: 5 additions & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use log::LevelFilter;

use crate::Config;

pub mod default;
Expand All @@ -10,10 +12,13 @@ pub trait Formatter {
impl Config {
pub fn new() -> Config {
Config {
level_filter: LevelFilter::Off,
datetime_format: "%Y-%m-%d %H:%M:%S".to_string(),
#[cfg(feature = "timezone")]
timezone: chrono_tz::Tz::UTC,
max_file_size: None,
levels: None,
targets: None,
}
}
}
Expand Down
Loading

0 comments on commit 3f0be47

Please sign in to comment.