Skip to content

Commit 62a7435

Browse files
committed
fix: print array values in additional fields
closes #131
1 parent 3b9af9b commit 62a7435

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

src/log.rs

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,9 @@ fn get_string_value_or_default(value: &IndexMap<String, String>, keys: &[String]
126126

127127
fn write_additional_values(out: &mut dyn Write, log_entry: &IndexMap<String, String>, additional_values: &[String], handlebars: &Handlebars<'static>) {
128128
for additional_value_prefix in additional_values {
129-
for additional_value in log_entry
130-
.keys()
131-
.filter(|k| *k == additional_value_prefix || k.starts_with(&format!("{}{}", additional_value_prefix, " > ")))
132-
{
129+
for additional_value in log_entry.keys().filter(|k| {
130+
*k == additional_value_prefix || k.starts_with(&format!("{} > ", additional_value_prefix)) || k.starts_with(&format!("{}[", additional_value_prefix))
131+
}) {
133132
if let Some(value) = get_string_value(log_entry, &[additional_value.to_string()]) {
134133
let mut variables: BTreeMap<String, String> = BTreeMap::new();
135134
variables.insert("key".to_string(), additional_value.to_string());
@@ -247,6 +246,58 @@ mod tests {
247246
);
248247
}
249248

249+
#[test]
250+
fn write_log_entry_with_array() {
251+
let handlebars = fblog_handlebar_registry_default_format();
252+
let mut out: Vec<u8> = Vec::new();
253+
let mut log_entry: Map<String, Value> = Map::new();
254+
log_entry.insert("message".to_string(), Value::String("something happened".to_string()));
255+
log_entry.insert("time".to_string(), Value::String("2017-07-06T15:21:16".to_string()));
256+
log_entry.insert("process".to_string(), Value::String("rust".to_string()));
257+
log_entry.insert("fu".to_string(), Value::Array(vec![Value::String("bower".to_string())]));
258+
log_entry.insert("level".to_string(), Value::String("info".to_string()));
259+
let mut log_settings = LogSettings::new_default_settings();
260+
log_settings.add_additional_values(vec!["process".to_string(), "fu".to_string()]);
261+
262+
print_log_line(&mut out, None, &log_entry, &log_settings, &handlebars);
263+
264+
assert_eq!(
265+
out_to_string(out),
266+
"\
267+
2017-07-06T15:21:16 INFO: something happened
268+
process: rust
269+
fu[0]: \"bower\"
270+
"
271+
);
272+
}
273+
#[test]
274+
fn write_log_entry_with_nested() {
275+
let handlebars = fblog_handlebar_registry_default_format();
276+
let mut out: Vec<u8> = Vec::new();
277+
let mut fu: Map<String, Value> = Map::new();
278+
fu.insert("test".to_string(), Value::String("hello".to_string()));
279+
280+
let mut log_entry: Map<String, Value> = Map::new();
281+
log_entry.insert("message".to_string(), Value::String("something happened".to_string()));
282+
log_entry.insert("time".to_string(), Value::String("2017-07-06T15:21:16".to_string()));
283+
log_entry.insert("process".to_string(), Value::String("rust".to_string()));
284+
log_entry.insert("fu".to_string(), Value::Object(fu));
285+
log_entry.insert("level".to_string(), Value::String("info".to_string()));
286+
let mut log_settings = LogSettings::new_default_settings();
287+
log_settings.add_additional_values(vec!["process".to_string(), "fu".to_string()]);
288+
289+
print_log_line(&mut out, None, &log_entry, &log_settings, &handlebars);
290+
291+
assert_eq!(
292+
out_to_string(out),
293+
"\
294+
2017-07-06T15:21:16 INFO: something happened
295+
process: rust
296+
fu > test: hello
297+
"
298+
);
299+
}
300+
250301
#[test]
251302
fn write_log_entry_with_additional_field_and_prefix() {
252303
let handlebars = fblog_handlebar_registry_default_format();

0 commit comments

Comments
 (0)