From 779dfef515366295fc04ee85c295643dcd6f59ee Mon Sep 17 00:00:00 2001 From: Alex Snaps Date: Mon, 18 Dec 2023 17:42:24 -0500 Subject: [PATCH] replaced env_logger by tokio tracing --- Cargo.lock | 146 ++++++++++++++++++++++++--------- limitador-server/Cargo.toml | 4 +- limitador-server/src/config.rs | 2 +- limitador-server/src/main.rs | 24 +++--- 4 files changed, 124 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 90ab1c58..ca51f972 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -902,19 +902,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "env_logger" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0" -dependencies = [ - "humantime", - "is-terminal", - "log", - "regex", - "termcolor", -] - [[package]] name = "equivalent" version = "1.0.1" @@ -1263,12 +1250,6 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" -[[package]] -name = "humantime" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" - [[package]] name = "hyper" version = "0.14.27" @@ -1562,7 +1543,6 @@ dependencies = [ "actix-web", "clap", "const_format", - "env_logger", "lazy_static", "limitador", "log", @@ -1577,6 +1557,9 @@ dependencies = [ "tokio", "tonic", "tonic-build", + "tracing", + "tracing-log", + "tracing-subscriber", "url", ] @@ -1645,6 +1628,15 @@ dependencies = [ "libc", ] +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + [[package]] name = "matchit" version = "0.7.2" @@ -1793,6 +1785,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "num-traits" version = "0.2.16" @@ -1877,6 +1879,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "paperclip" version = "0.8.1" @@ -2353,8 +2361,17 @@ checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47" dependencies = [ "aho-corasick", "memchr", - "regex-automata", - "regex-syntax", + "regex-automata 0.3.8", + "regex-syntax 0.7.5", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", ] [[package]] @@ -2365,9 +2382,15 @@ checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795" dependencies = [ "aho-corasick", "memchr", - "regex-syntax", + "regex-syntax 0.7.5", ] +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + [[package]] name = "regex-syntax" version = "0.7.5" @@ -2652,6 +2675,15 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae1a47186c03a32177042e55dbc5fd5aee900b8e0069a8d70fba96a9375cd012" +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "shlex" version = "1.2.0" @@ -2819,15 +2851,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "termcolor" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" -dependencies = [ - "winapi-util", -] - [[package]] name = "thiserror" version = "1.0.48" @@ -2848,6 +2871,16 @@ dependencies = [ "syn 2.0.32", ] +[[package]] +name = "thread_local" +version = "1.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "time" version = "0.3.29" @@ -3061,11 +3094,10 @@ checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" [[package]] name = "tracing" -version = "0.1.37" +version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ - "cfg-if", "log", "pin-project-lite", "tracing-attributes", @@ -3074,9 +3106,9 @@ dependencies = [ [[package]] name = "tracing-attributes" -version = "0.1.26" +version = "0.1.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" +checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", @@ -3085,11 +3117,41 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.31" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" dependencies = [ + "log", "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", ] [[package]] @@ -3193,6 +3255,12 @@ dependencies = [ "getrandom", ] +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "vcpkg" version = "0.2.15" diff --git a/limitador-server/Cargo.toml b/limitador-server/Cargo.toml index 011a2cd7..59bc4167 100644 --- a/limitador-server/Cargo.toml +++ b/limitador-server/Cargo.toml @@ -26,7 +26,9 @@ prost = "0.12" prost-types = "0.12" serde_yaml = "0.9" log = "0.4" -env_logger = "0.10.0" +tracing = "0.1.40" +tracing-log = "0.2.0" +tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } url = "2" actix-web = "4.1" actix-rt = "2" diff --git a/limitador-server/src/config.rs b/limitador-server/src/config.rs index 3cbb7712..3dcffa2a 100644 --- a/limitador-server/src/config.rs +++ b/limitador-server/src/config.rs @@ -20,7 +20,7 @@ use crate::envoy_rls::server::RateLimitHeaders; use limitador::storage; -use log::LevelFilter; +use tracing::level_filters::LevelFilter; #[derive(Debug)] pub struct Configuration { diff --git a/limitador-server/src/main.rs b/limitador-server/src/main.rs index d1ecdf65..39fc1570 100644 --- a/limitador-server/src/main.rs +++ b/limitador-server/src/main.rs @@ -14,7 +14,6 @@ use crate::envoy_rls::server::{run_envoy_rls_server, RateLimitHeaders}; use crate::http_api::server::run_http_server; use clap::{value_parser, Arg, ArgAction, Command}; use const_format::formatcp; -use env_logger::Builder; use limitador::counter::Counter; use limitador::errors::LimitadorError; use limitador::limit::Limit; @@ -30,7 +29,6 @@ use limitador::storage::{AsyncCounterStorage, AsyncStorage, Storage}; use limitador::{ storage, AsyncRateLimiter, AsyncRateLimiterBuilder, RateLimiter, RateLimiterBuilder, }; -use log::LevelFilter; use notify::event::{ModifyKind, RenameMode}; use notify::{Error, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher}; use std::env::VarError; @@ -42,6 +40,7 @@ use std::{env, process}; use sysinfo::{RefreshKind, System, SystemExt}; use thiserror::Error; use tokio::runtime::Handle; +use tracing::level_filters::LevelFilter; mod envoy_rls; mod http_api; @@ -285,12 +284,15 @@ async fn main() -> Result<(), Box> { let config = { let (config, version) = create_config(); println!("{LIMITADOR_HEADER} {version}"); - let mut builder = Builder::new(); - if let Some(level) = config.log_level { - builder.filter(None, level); + let builder = if let Some(level) = config.log_level { + tracing_subscriber::fmt().with_max_level(level) } else { - builder.parse_default_env(); - } + tracing_subscriber::fmt().with_max_level( + tracing_subscriber::filter::EnvFilter::from_default_env() + .max_level_hint() + .unwrap_or(LevelFilter::ERROR), + ) + }; builder.init(); info!("Version: {}", version); @@ -749,10 +751,10 @@ fn create_config() -> (Configuration, &'static str) { config.log_level = match matches.get_count("v") { 0 => None, - 1 => Some(LevelFilter::Warn), - 2 => Some(LevelFilter::Info), - 3 => Some(LevelFilter::Debug), - 4 => Some(LevelFilter::Trace), + 1 => Some(LevelFilter::WARN), + 2 => Some(LevelFilter::INFO), + 3 => Some(LevelFilter::DEBUG), + 4 => Some(LevelFilter::TRACE), _ => unreachable!("Verbosity should at most be 4!"), };