Skip to content

Commit 211f5a2

Browse files
committed
chore(stdlib): Optimise log function by converting level to enum
# Conflicts: # src/stdlib/util.rs
1 parent df97ad9 commit 211f5a2

1 file changed

Lines changed: 24 additions & 11 deletions

File tree

src/stdlib/log.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ impl Function for Log {
9393
ctx: &mut FunctionCompileContext,
9494
arguments: ArgumentList,
9595
) -> Compiled {
96+
use tracing::Level;
97+
9698
let levels: &[Value] = &[
9799
"trace".into(),
98100
"debug".into(),
@@ -107,6 +109,16 @@ impl Function for Log {
107109
.unwrap_or_else(|| DEFAULT_LEVEL.clone())
108110
.try_bytes()
109111
.expect("log level not bytes");
112+
113+
let level = match level.as_ref() {
114+
b"trace" => Level::TRACE,
115+
b"debug" => Level::DEBUG,
116+
b"info" => Level::INFO,
117+
b"warn" => Level::WARN,
118+
b"error" => Level::ERROR,
119+
_ => unreachable!(),
120+
};
121+
110122
let rate_limit_secs = ConstOrExpr::<i64>::default(
111123
arguments.optional("rate_limit_secs"),
112124
state,
@@ -135,26 +147,26 @@ impl Function for Log {
135147

136148
#[cfg(not(target_arch = "wasm32"))]
137149
mod implementation {
138-
use tracing::{debug, error, info, trace, warn};
150+
use tracing::{Level, debug, error, info, trace, warn};
139151

140152
use crate::compiler::prelude::*;
141153

142-
pub(super) fn log(rate_limit_secs: i64, level: &Bytes, value: &Value, span: Span) -> Resolved {
154+
pub(super) fn log(rate_limit_secs: i64, level: Level, value: &Value, span: Span) -> Resolved {
143155
let res = value.to_string_lossy();
144-
match level.as_ref() {
145-
b"trace" => {
156+
match level {
157+
Level::TRACE => {
146158
trace!(message = %res, internal_log_rate_secs = rate_limit_secs, vrl_position = span.start());
147159
}
148-
b"debug" => {
160+
Level::DEBUG => {
149161
debug!(message = %res, internal_log_rate_secs = rate_limit_secs, vrl_position = span.start());
150162
}
151-
b"warn" => {
163+
Level::WARN => {
152164
warn!(message = %res, internal_log_rate_secs = rate_limit_secs, vrl_position = span.start());
153165
}
154-
b"error" => {
166+
Level::ERROR => {
155167
error!(message = %res, internal_log_rate_secs = rate_limit_secs, vrl_position = span.start());
156168
}
157-
_ => {
169+
Level::INFO => {
158170
info!(message = %res, internal_log_rate_secs = rate_limit_secs, vrl_position = span.start());
159171
}
160172
}
@@ -165,7 +177,7 @@ mod implementation {
165177
pub(super) struct LogFn {
166178
pub(super) span: Span,
167179
pub(super) value: Box<dyn Expression>,
168-
pub(super) level: Bytes,
180+
pub(super) level: Level,
169181
pub(super) rate_limit_secs: ConstOrExpr<i64>,
170182
}
171183

@@ -176,7 +188,7 @@ mod implementation {
176188

177189
let span = self.span;
178190

179-
log(rate_limit_secs, &self.level, &value, span)
191+
log(rate_limit_secs, self.level, &value, span)
180192
}
181193

182194
fn type_def(&self, _: &state::TypeState) -> TypeDef {
@@ -188,6 +200,7 @@ mod implementation {
188200
#[cfg(all(test, not(target_arch = "wasm32")))]
189201
mod tests {
190202
use tracing_test::traced_test;
203+
use tracing::Level;
191204

192205
use super::*;
193206
use crate::value;
@@ -210,7 +223,7 @@ mod tests {
210223
// Check that a message is logged without additional quotes
211224
implementation::log(
212225
1,
213-
&Bytes::from("warn"),
226+
Level::WARN,
214227
&value!("simple test message"),
215228
Span::default(),
216229
)

0 commit comments

Comments
 (0)