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

Timezone as feature #2

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Timezone as feature
  • Loading branch information
tjardoo committed Sep 14, 2024
commit 493b5904a381b047b0929335e000dfbc57422e75
9 changes: 9 additions & 0 deletions .github/workflows/cargo-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,12 @@ jobs:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose

cargo-build-with-features:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose --features timezone
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ keywords = ["log", "logging", "filelog"]
[dependencies]
log = { version = "0.4", features = ["std"] }
chrono = "0.4"
chrono-tz = "0.10"
chrono-tz = { version = "0.10", optional = true }

[features]
default = []
timezone = ["chrono-tz"]

[workspace]
members = [
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use ftail::Ftail;
use log::LevelFilter;

Ftail::new()
.timezone(chrono_tz::Europe::Amsterdam) // optional (default is UTC)
.console(LevelFilter::Debug)
.daily_file("logs", LevelFilter::Error)
.init()?;
Expand All @@ -37,6 +36,11 @@ log::warn!("This is a warning message");
log::error!("This is an error message");
```

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`]

## Drivers

### Console
Expand Down
3 changes: 1 addition & 2 deletions examples/console/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ publish = false

[dependencies]
log = "0.4"
ftail = { path = "../../../ftail" }
chrono-tz = "0.10"
ftail = { path = "../../../ftail", features = ["timezone"] }
4 changes: 2 additions & 2 deletions examples/console/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use log::LevelFilter;

fn main() -> Result<(), Box<dyn std::error::Error>> {
Ftail::new()
.timezone(chrono_tz::UTC)
// .timezone(chrono_tz::Europe::Amsterdam)
// .timezone(ftail::Tz::UTC)
.timezone(ftail::Tz::Europe__Amsterdam)
.datetime_format("%d-%m-%Y %H:%M:%S")
.console(LevelFilter::Off)
.init()?;
Expand Down
1 change: 1 addition & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ impl Config {
pub fn new() -> Config {
Config {
datetime_format: "%Y-%m-%d %H:%M:%S".to_string(),
#[cfg(feature = "timezone")]
timezone: chrono_tz::Tz::UTC,
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
//! use log::LevelFilter;
//!
//! Ftail::new()
//! .timezone(chrono_tz::Europe::Amsterdam) // optional (default is UTC)
//! .console(LevelFilter::Debug)
//! .daily_file("logs", LevelFilter::Error)
//! .init()?;
Expand All @@ -37,6 +36,11 @@
//! log::error!("This is an error message");
//! ```
//!
//! 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`]
//!
//! ## Drivers
//!
//! ### Console
Expand Down Expand Up @@ -177,6 +181,9 @@ use drivers::{
use error::FtailError;
use log::Log;

#[cfg(feature = "timezone")]
pub use chrono_tz::Tz;

/// Module containing the ANSI escape codes.
pub mod ansi_escape;
/// Module containing the drivers.
Expand Down Expand Up @@ -210,6 +217,7 @@ pub(crate) struct InitializedLogDriver {
#[derive(Clone)]
pub struct Config {
pub datetime_format: String,
#[cfg(feature = "timezone")]
pub timezone: chrono_tz::Tz,
}

Expand All @@ -223,6 +231,7 @@ impl Ftail {
}
}

#[cfg(feature = "timezone")]
/// Set the timezone for the logger.
pub fn timezone(mut self, timezone: chrono_tz::Tz) -> Self {
self.config.timezone = timezone;
Expand Down
10 changes: 8 additions & 2 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ impl<'a> LogWriter<'a> {
}

pub fn get_datetime(&self) -> String {
chrono::Local::now()
#[cfg(not(feature = "timezone"))]
return chrono::Local::now()
.format(&self.config.datetime_format)
.to_string();

#[cfg(feature = "timezone")]
return chrono::Local::now()
.with_timezone(&self.config.timezone)
.format(&self.config.datetime_format)
.to_string()
.to_string();
}

pub fn get_level(&self) -> String {
Expand Down
Loading