Skip to content

Commit fac5494

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

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

src/log.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ fn write_additional_values(out: &mut dyn Write, log_entry: &IndexMap<String, Str
128128
for additional_value_prefix in additional_values {
129129
for additional_value in log_entry
130130
.keys()
131-
.filter(|k| *k == additional_value_prefix || k.starts_with(&format!("{}{}", additional_value_prefix, " > ")))
131+
.filter(|k| *k == additional_value_prefix || k.starts_with(&format!("{} > ", additional_value_prefix)) || k.starts_with(&format!("{}[", additional_value_prefix)))
132132
{
133133
if let Some(value) = get_string_value(log_entry, &[additional_value.to_string()]) {
134134
let mut variables: BTreeMap<String, String> = BTreeMap::new();
@@ -247,6 +247,58 @@ mod tests {
247247
);
248248
}
249249

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

0 commit comments

Comments
 (0)