From 21d81ce0d8411b247cf16d4b87b9d94d9ec90a6a Mon Sep 17 00:00:00 2001 From: jizhuozhi Date: Tue, 29 Oct 2024 00:01:13 +0800 Subject: [PATCH 1/3] feat: lazy formatted log --- plugins/wasm-rust/src/log.rs | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/plugins/wasm-rust/src/log.rs b/plugins/wasm-rust/src/log.rs index 4656b8669b..89be432003 100644 --- a/plugins/wasm-rust/src/log.rs +++ b/plugins/wasm-rust/src/log.rs @@ -13,6 +13,7 @@ // limitations under the License. use proxy_wasm::hostcalls; +use std::fmt::{format, Arguments}; pub enum LogLevel { Trace, @@ -68,4 +69,45 @@ impl Log { pub fn critical(&self, msg: &str) { self.log(LogLevel::Critical, msg) } + + fn logf(&self, level: LogLevel, format_args: Arguments) { + let level = match level { + LogLevel::Trace => proxy_wasm::types::LogLevel::Trace, + LogLevel::Debug => proxy_wasm::types::LogLevel::Debug, + LogLevel::Info => proxy_wasm::types::LogLevel::Info, + LogLevel::Warn => proxy_wasm::types::LogLevel::Warn, + LogLevel::Error => proxy_wasm::types::LogLevel::Error, + LogLevel::Critical => proxy_wasm::types::LogLevel::Critical, + }; + if let Ok(log_level) = hostcalls::get_log_level() { + if (level as i32) < (log_level as i32) { + return; + } + hostcalls::log(level, format(format_args).as_str()).unwrap(); + } + } + + pub fn tracef(&self, format_args: Arguments) { + self.logf(LogLevel::Trace, format_args) + } + + pub fn debugf(&self, format_args: Arguments) { + self.logf(LogLevel::Debug, format_args) + } + + pub fn infof(&self, format_args: Arguments) { + self.logf(LogLevel::Info, format_args) + } + + pub fn warnf(&self, format_args: Arguments) { + self.logf(LogLevel::Warn, format_args) + } + + pub fn errorf(&self, format_args: Arguments) { + self.logf(LogLevel::Error, format_args) + } + + pub fn criticalf(&self, format_args: Arguments) { + self.logf(LogLevel::Critical, format_args) + } } From 6902042052ce2c94352d54eaf84de72b4821229c Mon Sep 17 00:00:00 2001 From: jizhuozhi Date: Tue, 29 Oct 2024 12:39:38 +0800 Subject: [PATCH 2/3] feat: lazy formatted log --- plugins/wasm-rust/src/log.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/plugins/wasm-rust/src/log.rs b/plugins/wasm-rust/src/log.rs index 89be432003..56dd84b3ca 100644 --- a/plugins/wasm-rust/src/log.rs +++ b/plugins/wasm-rust/src/log.rs @@ -83,30 +83,60 @@ impl Log { if (level as i32) < (log_level as i32) { return; } - hostcalls::log(level, format(format_args).as_str()).unwrap(); + hostcalls::log(level, format!("[{}] {}", self.plugin_name, format_args).as_str()).unwrap(); } } + /// ``` + /// use higress_wasm_rust::log::Log; + /// let log = Log::new("foobar".into_string()); + /// log.tracef(format_args!("Hello, {}!","World")); + /// ``` pub fn tracef(&self, format_args: Arguments) { self.logf(LogLevel::Trace, format_args) } + /// ``` + /// use higress_wasm_rust::log::Log; + /// let log = Log::new("foobar".into_string()); + /// log.debugf(format_args!("Hello, {}!","World")); + /// ``` pub fn debugf(&self, format_args: Arguments) { self.logf(LogLevel::Debug, format_args) } + /// ``` + /// use higress_wasm_rust::log::Log; + /// let log = Log::new("foobar".into_string()); + /// log.infof(format_args!("Hello, {}!","World")); + /// ``` pub fn infof(&self, format_args: Arguments) { self.logf(LogLevel::Info, format_args) } + /// ``` + /// use higress_wasm_rust::log::Log; + /// let log = Log::new("foobar".into_string()); + /// log.warnf(format_args!("Hello, {}!","World")); + /// ``` pub fn warnf(&self, format_args: Arguments) { self.logf(LogLevel::Warn, format_args) } + /// ``` + /// use higress_wasm_rust::log::Log; + /// let log = Log::new("foobar".into_string()); + /// log.errorf(format_args!("Hello, {}!","World")); + /// ``` pub fn errorf(&self, format_args: Arguments) { self.logf(LogLevel::Error, format_args) } + /// ``` + /// use higress_wasm_rust::log::Log; + /// let log = Log::new("foobar".into_string()); + /// log.criticalf(format_args!("Hello, {}!","World")); + /// ``` pub fn criticalf(&self, format_args: Arguments) { self.logf(LogLevel::Critical, format_args) } From 8d45659f8b087bb3d00c458e1a538e9b6d28efb8 Mon Sep 17 00:00:00 2001 From: jizhuozhi Date: Tue, 29 Oct 2024 13:05:48 +0800 Subject: [PATCH 3/3] feat: lazy formatted log --- plugins/wasm-rust/src/log.rs | 41 +++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/plugins/wasm-rust/src/log.rs b/plugins/wasm-rust/src/log.rs index 56dd84b3ca..e469558673 100644 --- a/plugins/wasm-rust/src/log.rs +++ b/plugins/wasm-rust/src/log.rs @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -use proxy_wasm::hostcalls; -use std::fmt::{format, Arguments}; +use proxy_wasm::{hostcalls, types}; +use std::fmt::Arguments; pub enum LogLevel { Trace, @@ -35,14 +35,7 @@ impl Log { fn log(&self, level: LogLevel, msg: &str) { let msg = format!("[{}] {}", self.plugin_name, msg); - let level = match level { - LogLevel::Trace => proxy_wasm::types::LogLevel::Trace, - LogLevel::Debug => proxy_wasm::types::LogLevel::Debug, - LogLevel::Info => proxy_wasm::types::LogLevel::Info, - LogLevel::Warn => proxy_wasm::types::LogLevel::Warn, - LogLevel::Error => proxy_wasm::types::LogLevel::Error, - LogLevel::Critical => proxy_wasm::types::LogLevel::Critical, - }; + let level = types::LogLevel::from(level); hostcalls::log(level, msg.as_str()).unwrap(); } @@ -71,19 +64,16 @@ impl Log { } fn logf(&self, level: LogLevel, format_args: Arguments) { - let level = match level { - LogLevel::Trace => proxy_wasm::types::LogLevel::Trace, - LogLevel::Debug => proxy_wasm::types::LogLevel::Debug, - LogLevel::Info => proxy_wasm::types::LogLevel::Info, - LogLevel::Warn => proxy_wasm::types::LogLevel::Warn, - LogLevel::Error => proxy_wasm::types::LogLevel::Error, - LogLevel::Critical => proxy_wasm::types::LogLevel::Critical, - }; + let level = types::LogLevel::from(level); if let Ok(log_level) = hostcalls::get_log_level() { if (level as i32) < (log_level as i32) { return; } - hostcalls::log(level, format!("[{}] {}", self.plugin_name, format_args).as_str()).unwrap(); + hostcalls::log( + level, + format!("[{}] {}", self.plugin_name, format_args).as_str(), + ) + .unwrap(); } } @@ -141,3 +131,16 @@ impl Log { self.logf(LogLevel::Critical, format_args) } } + +impl From for types::LogLevel { + fn from(value: LogLevel) -> Self { + match value { + LogLevel::Trace => types::LogLevel::Trace, + LogLevel::Debug => types::LogLevel::Debug, + LogLevel::Info => types::LogLevel::Info, + LogLevel::Warn => types::LogLevel::Warn, + LogLevel::Error => types::LogLevel::Error, + LogLevel::Critical => types::LogLevel::Critical, + } + } +}