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

feat: lazy formatted log #1441

Merged
merged 5 commits into from
Oct 29, 2024
Merged
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
93 changes: 84 additions & 9 deletions plugins/wasm-rust/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use proxy_wasm::hostcalls;
use proxy_wasm::{hostcalls, types};
use std::fmt::Arguments;

pub enum LogLevel {
Trace,
Expand All @@ -34,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();
}

Expand All @@ -68,4 +62,85 @@ impl Log {
pub fn critical(&self, msg: &str) {
self.log(LogLevel::Critical, msg)
}

jizhuozhi marked this conversation as resolved.
Show resolved Hide resolved
fn logf(&self, level: LogLevel, format_args: Arguments) {
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();
}
}

/// ```
/// 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)
}
}

impl From<LogLevel> 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,
}
}
}
Loading