From c3d7f42d63dfcd1d27b83a179445a8a7cdad9e1e Mon Sep 17 00:00:00 2001 From: bconn98 Date: Thu, 29 Feb 2024 23:44:48 -0500 Subject: [PATCH 1/4] chore: clippy fixes + msrv bump --- .github/workflows/main.yml | 4 ++-- Cargo.toml | 6 +++--- README.md | 4 ++-- src/append/rolling_file/policy/compound/mod.rs | 2 +- src/config/raw.rs | 8 +++----- src/config/runtime.rs | 2 +- 6 files changed, 12 insertions(+), 14 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b8219b46..1f58427f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,7 +17,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - rust_versions: ["stable", "1.69"] + rust_versions: ["stable", "1.70"] os: [ubuntu-latest, windows-latest] steps: - name: Checkout the source code @@ -63,7 +63,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - rust_versions: ["stable", "1.69"] + rust_versions: ["stable", "1.70"] steps: - name: Checkout the source code uses: actions/checkout@v4 diff --git a/Cargo.toml b/Cargo.toml index ff9c9fd3..5826a3b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ repository = "https://github.com/estk/log4rs" readme = "README.md" keywords = ["log", "logger", "logging", "log4"] edition = "2018" -rust-version = "1.69" +rust-version = "1.70" [features] default = ["all_components", "config_parsing", "yaml_format"] @@ -62,13 +62,13 @@ fnv = "1.0" humantime = { version = "2.1", optional = true } log = { version = "0.4.20", features = ["std"] } log-mdc = { version = "0.1", optional = true } -serde = { version = "1.0", optional = true, features = ["derive"] } +serde = { version = "1.0.196", optional = true, features = ["derive"] } serde-value = { version = "0.7", optional = true } thread-id = { version = "4", optional = true } typemap-ors = { version = "1.0.0", optional = true } serde_json = { version = "1.0", optional = true } serde_yaml = { version = "0.9", optional = true } -toml = { version = "0.8", optional = true } +toml = { version = "0.8.10", optional = true } parking_lot = { version = "0.12.0", optional = true } rand = { version = "0.8", optional = true} thiserror = "1.0.15" diff --git a/README.md b/README.md index 056d1e4d..b3c36ff2 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![crates.io](https://img.shields.io/crates/v/log4rs.svg)](https://crates.io/crates/log4rs) [![License: MIT OR Apache-2.0](https://img.shields.io/crates/l/clippy.svg)](#license) ![CI](https://github.com/estk/log4rs/workflows/CI/badge.svg) -[![Minimum rustc version](https://img.shields.io/badge/rustc-1.69+-green.svg)](https://github.com/estk/log4rs#rust-version-requirements) +[![Minimum rustc version](https://img.shields.io/badge/rustc-1.70+-green.svg)](https://github.com/estk/log4rs#rust-version-requirements) log4rs is a highly configurable logging framework modeled after Java's Logback and log4j libraries. @@ -71,7 +71,7 @@ fn main() { ## Rust Version Requirements -1.69 +1.70 ## Building for Dev diff --git a/src/append/rolling_file/policy/compound/mod.rs b/src/append/rolling_file/policy/compound/mod.rs index 484af19c..3e14ae06 100644 --- a/src/append/rolling_file/policy/compound/mod.rs +++ b/src/append/rolling_file/policy/compound/mod.rs @@ -2,7 +2,7 @@ //! //! Requires the `compound_policy` feature. #[cfg(feature = "config_parsing")] -use serde::{self, de}; +use serde::de; #[cfg(feature = "config_parsing")] use serde_value::Value; #[cfg(feature = "config_parsing")] diff --git a/src/config/raw.rs b/src/config/raw.rs index a092d56b..9868b495 100644 --- a/src/config/raw.rs +++ b/src/config/raw.rs @@ -89,9 +89,7 @@ //! ``` #![allow(deprecated)] -use std::{ - borrow::ToOwned, collections::HashMap, fmt, marker::PhantomData, sync::Arc, time::Duration, -}; +use std::{collections::HashMap, fmt, marker::PhantomData, sync::Arc, time::Duration}; use anyhow::anyhow; use derivative::Derivative; @@ -288,9 +286,9 @@ impl Deserializers { } /// Deserializes a value of a specific type and kind. - pub fn deserialize(&self, kind: &str, config: Value) -> anyhow::Result> + pub fn deserialize(&self, kind: &str, config: Value) -> anyhow::Result> where - T: Deserializable, + T: Deserializable + ?Sized, { match self.0.get::>().and_then(|m| m.get(kind)) { Some(b) => b.deserialize(config, self), diff --git a/src/config/runtime.rs b/src/config/runtime.rs index 6b80019f..3d04947e 100644 --- a/src/config/runtime.rs +++ b/src/config/runtime.rs @@ -1,7 +1,7 @@ //! log4rs configuration use log::LevelFilter; -use std::{collections::HashSet, iter::IntoIterator}; +use std::collections::HashSet; use thiserror::Error; use crate::{append::Append, filter::Filter}; From 4486edd986f843bfe237e59425df7beb5f51d07f Mon Sep 17 00:00:00 2001 From: bconn98 Date: Sat, 2 Mar 2024 19:08:56 -0500 Subject: [PATCH 2/4] chore: backout msrv bump, knock toml down --- .github/workflows/main.yml | 4 ++-- Cargo.toml | 4 ++-- README.md | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1f58427f..b8219b46 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,7 +17,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - rust_versions: ["stable", "1.70"] + rust_versions: ["stable", "1.69"] os: [ubuntu-latest, windows-latest] steps: - name: Checkout the source code @@ -63,7 +63,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - rust_versions: ["stable", "1.70"] + rust_versions: ["stable", "1.69"] steps: - name: Checkout the source code uses: actions/checkout@v4 diff --git a/Cargo.toml b/Cargo.toml index 5826a3b9..2f44bb2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ repository = "https://github.com/estk/log4rs" readme = "README.md" keywords = ["log", "logger", "logging", "log4"] edition = "2018" -rust-version = "1.70" +rust-version = "1.69" [features] default = ["all_components", "config_parsing", "yaml_format"] @@ -68,7 +68,7 @@ thread-id = { version = "4", optional = true } typemap-ors = { version = "1.0.0", optional = true } serde_json = { version = "1.0", optional = true } serde_yaml = { version = "0.9", optional = true } -toml = { version = "0.8.10", optional = true } +toml = { version = "0.8.9", optional = true } parking_lot = { version = "0.12.0", optional = true } rand = { version = "0.8", optional = true} thiserror = "1.0.15" diff --git a/README.md b/README.md index b3c36ff2..056d1e4d 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![crates.io](https://img.shields.io/crates/v/log4rs.svg)](https://crates.io/crates/log4rs) [![License: MIT OR Apache-2.0](https://img.shields.io/crates/l/clippy.svg)](#license) ![CI](https://github.com/estk/log4rs/workflows/CI/badge.svg) -[![Minimum rustc version](https://img.shields.io/badge/rustc-1.70+-green.svg)](https://github.com/estk/log4rs#rust-version-requirements) +[![Minimum rustc version](https://img.shields.io/badge/rustc-1.69+-green.svg)](https://github.com/estk/log4rs#rust-version-requirements) log4rs is a highly configurable logging framework modeled after Java's Logback and log4j libraries. @@ -71,7 +71,7 @@ fn main() { ## Rust Version Requirements -1.70 +1.69 ## Building for Dev From 049e2cc5a6ebd86fc1278fa3a93d25a05b7b9bb0 Mon Sep 17 00:00:00 2001 From: bconn98 Date: Sat, 2 Mar 2024 20:40:04 -0500 Subject: [PATCH 3/4] limit toml --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2f44bb2e..8f7134ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,7 +68,7 @@ thread-id = { version = "4", optional = true } typemap-ors = { version = "1.0.0", optional = true } serde_json = { version = "1.0", optional = true } serde_yaml = { version = "0.9", optional = true } -toml = { version = "0.8.9", optional = true } +toml = { version = "<0.8.10", optional = true } parking_lot = { version = "0.12.0", optional = true } rand = { version = "0.8", optional = true} thiserror = "1.0.15" From ef454ab336966cbe417034a206c84b3d4a59a5d0 Mon Sep 17 00:00:00 2001 From: bconn98 Date: Sat, 2 Mar 2024 21:18:42 -0500 Subject: [PATCH 4/4] chore: add filter tests --- Cargo.toml | 1 + src/filter/mod.rs | 42 ++++++++++++++++++ src/filter/threshold.rs | 95 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 8f7134ca..9f648269 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -88,6 +88,7 @@ streaming-stats = "0.2.3" humantime = "2.1" tempfile = "3.8" mock_instant = "0.3" +serde_test = "1.0.176" [[example]] name = "json_logger" diff --git a/src/filter/mod.rs b/src/filter/mod.rs index 093dd2d1..ddb1c219 100644 --- a/src/filter/mod.rs +++ b/src/filter/mod.rs @@ -31,6 +31,7 @@ impl Deserializable for dyn Filter { } } +#[derive(PartialEq, Debug)] /// The response returned by a filter. pub enum Response { /// Accept the log event. @@ -78,3 +79,44 @@ impl<'de> de::Deserialize<'de> for FilterConfig { }) } } + +#[cfg(test)] +mod test { + #[cfg(feature = "config_parsing")] + use super::*; + #[cfg(feature = "config_parsing")] + use serde_test::{assert_de_tokens, assert_de_tokens_error, Token}; + + #[test] + #[cfg(feature = "config_parsing")] + fn test_cfg_deserializer() { + let filter = FilterConfig { + kind: "threshold".to_owned(), + config: Value::Map({ + let mut map = BTreeMap::new(); + map.insert( + Value::String("level".to_owned()), + Value::String("error".to_owned()), + ); + map + }), + }; + + let mut cfg = vec![ + Token::Struct { + name: "FilterConfig", + len: 2, + }, + Token::Str("kind"), + Token::Str("threshold"), + Token::Str("level"), + Token::Str("error"), + Token::StructEnd, + ]; + + assert_de_tokens(&filter, &cfg); + + cfg[1] = Token::Str("knd"); + assert_de_tokens_error::(&cfg, "missing field `kind`"); + } +} diff --git a/src/filter/threshold.rs b/src/filter/threshold.rs index 86c6e289..c62c5198 100644 --- a/src/filter/threshold.rs +++ b/src/filter/threshold.rs @@ -66,3 +66,98 @@ impl Deserialize for ThresholdFilterDeserializer { Ok(Box::new(ThresholdFilter::new(config.level))) } } + +#[cfg(test)] +mod test { + use log::{Level, LevelFilter, Record}; + + use super::*; + + #[cfg(feature = "config_parsing")] + use crate::config::Deserializers; + + #[cfg(feature = "config_parsing")] + use serde_test::{assert_de_tokens, assert_de_tokens_error, Token}; + + #[test] + #[cfg(feature = "config_parsing")] + fn test_cfg_deserialize() { + let filter_cfg = ThresholdFilterConfig { + level: LevelFilter::Off, + }; + + let mut cfg = vec![ + Token::Struct { + name: "ThresholdFilterConfig", + len: 1, + }, + Token::Str("level"), + Token::Enum { + name: "LevelFilter", + }, + Token::Str("Off"), + Token::Unit, + Token::StructEnd, + ]; + + assert_de_tokens(&filter_cfg, &cfg); + + cfg[1] = Token::Str("leel"); + assert_de_tokens_error::(&cfg, "missing field `level`"); + + cfg[1] = Token::Str("level"); + cfg[3] = Token::Str("On"); + cfg.remove(4); // No Unit on this one as the Option is invalid + assert_de_tokens_error::( + &cfg, + "unknown variant `On`, expected one of `OFF`, `ERROR`, `WARN`, `INFO`, `DEBUG`, `TRACE`", + ); + } + + #[test] + fn test_filter_new_vs_struct() { + assert_eq!( + ThresholdFilter::new(LevelFilter::Info), + ThresholdFilter { + level: LevelFilter::Info + } + ); + } + + #[test] + fn test_threshold_filter() { + let thres = ThresholdFilter::new(LevelFilter::Info); + let debug_record = Record::builder() + .level(Level::Debug) + .args(format_args!("the message")) + .module_path(Some("path")) + .file(Some("file")) + .line(Some(132)) + .build(); + + assert_eq!(thres.filter(&debug_record), Response::Reject); + + let error_record = Record::builder() + .level(Level::Error) + .args(format_args!("the message")) + .module_path(Some("path")) + .file(Some("file")) + .line(Some(132)) + .build(); + + assert_eq!(thres.filter(&error_record), Response::Neutral); + } + + #[test] + #[cfg(feature = "config_parsing")] + fn test_cfg_deserializer() { + let filter_cfg = ThresholdFilterConfig { + level: LevelFilter::Off, + }; + + let deserializer = ThresholdFilterDeserializer; + + let res = deserializer.deserialize(filter_cfg, &Deserializers::default()); + assert!(res.is_ok()); + } +}