Skip to content

Commit 455cb9c

Browse files
authored
print tracing event fields (#26)
1 parent 412044c commit 455cb9c

File tree

2 files changed

+107
-20
lines changed

2 files changed

+107
-20
lines changed

src/bridges/tracing.rs

Lines changed: 93 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,16 @@ mod tests {
162162
let guard = set_local_logfire(config).unwrap();
163163

164164
tracing::subscriber::with_default(guard.subscriber.clone(), || {
165+
tracing::info!("root event"); // FIXME: this event is not emitted
166+
tracing::info!(name: "root event with value", field_value = 1); // FIXME: this event is not emitted
167+
165168
let root = tracing::span!(Level::INFO, "root span").entered();
166169
let _ = tracing::span!(Level::INFO, "hello world span").entered();
167170
let _ = tracing::span!(Level::DEBUG, "debug span");
168171
let _ = tracing::span!(parent: &root, Level::DEBUG, "debug span with explicit parent");
172+
169173
tracing::info!("hello world log");
174+
tracing::info!(name: "hello world log with value", field_value = 1);
170175
});
171176

172177
let spans = exporter.get_finished_spans().unwrap();
@@ -221,7 +226,7 @@ mod tests {
221226
"code.lineno",
222227
),
223228
value: I64(
224-
11,
229+
14,
225230
),
226231
},
227232
KeyValue {
@@ -327,7 +332,7 @@ mod tests {
327332
"code.lineno",
328333
),
329334
value: I64(
330-
12,
335+
15,
331336
),
332337
},
333338
KeyValue {
@@ -443,7 +448,7 @@ mod tests {
443448
"code.lineno",
444449
),
445450
value: I64(
446-
12,
451+
15,
447452
),
448453
},
449454
KeyValue {
@@ -565,7 +570,7 @@ mod tests {
565570
"code.lineno",
566571
),
567572
value: I64(
568-
13,
573+
16,
569574
),
570575
},
571576
KeyValue {
@@ -681,7 +686,7 @@ mod tests {
681686
"code.lineno",
682687
),
683688
value: I64(
684-
13,
689+
16,
685690
),
686691
},
687692
KeyValue {
@@ -803,7 +808,7 @@ mod tests {
803808
"code.lineno",
804809
),
805810
value: I64(
806-
14,
811+
17,
807812
),
808813
},
809814
KeyValue {
@@ -919,7 +924,7 @@ mod tests {
919924
"code.lineno",
920925
),
921926
value: I64(
922-
14,
927+
17,
923928
),
924929
},
925930
KeyValue {
@@ -1041,7 +1046,7 @@ mod tests {
10411046
"code.lineno",
10421047
),
10431048
value: I64(
1044-
11,
1049+
14,
10451050
),
10461051
},
10471052
KeyValue {
@@ -1152,7 +1157,73 @@ mod tests {
11521157
"code.lineno",
11531158
),
11541159
value: I64(
1155-
169,
1160+
173,
1161+
),
1162+
},
1163+
],
1164+
dropped_attributes_count: 0,
1165+
},
1166+
Event {
1167+
name: "hello world log with value",
1168+
timestamp: SystemTime {
1169+
tv_sec: 9,
1170+
tv_nsec: 0,
1171+
},
1172+
attributes: [
1173+
KeyValue {
1174+
key: Static(
1175+
"level",
1176+
),
1177+
value: String(
1178+
Static(
1179+
"INFO",
1180+
),
1181+
),
1182+
},
1183+
KeyValue {
1184+
key: Static(
1185+
"target",
1186+
),
1187+
value: String(
1188+
Static(
1189+
"logfire::bridges::tracing::tests",
1190+
),
1191+
),
1192+
},
1193+
KeyValue {
1194+
key: Static(
1195+
"field_value",
1196+
),
1197+
value: I64(
1198+
1,
1199+
),
1200+
},
1201+
KeyValue {
1202+
key: Static(
1203+
"code.filepath",
1204+
),
1205+
value: String(
1206+
Static(
1207+
"src/bridges/tracing.rs",
1208+
),
1209+
),
1210+
},
1211+
KeyValue {
1212+
key: Static(
1213+
"code.namespace",
1214+
),
1215+
value: String(
1216+
Static(
1217+
"logfire::bridges::tracing::tests",
1218+
),
1219+
),
1220+
},
1221+
KeyValue {
1222+
key: Static(
1223+
"code.lineno",
1224+
),
1225+
value: I64(
1226+
174,
11561227
),
11571228
},
11581229
],
@@ -1195,11 +1266,16 @@ mod tests {
11951266
let guard = set_local_logfire(config).unwrap();
11961267

11971268
tracing::subscriber::with_default(guard.subscriber.clone(), || {
1269+
tracing::info!("root event");
1270+
tracing::info!(name: "root event with value", field_value = 1);
1271+
11981272
let root = tracing::span!(Level::INFO, "root span").entered();
11991273
let _ = tracing::span!(Level::INFO, "hello world span").entered();
12001274
let _ = tracing::span!(Level::DEBUG, "debug span");
12011275
let _ = tracing::span!(parent: &root, Level::DEBUG, "debug span with explicit parent");
1276+
12021277
tracing::info!("hello world log");
1278+
tracing::info!(name: "hello world log with value", field_value = 1);
12031279
});
12041280

12051281
guard.shutdown_handler.shutdown().unwrap();
@@ -1217,11 +1293,14 @@ mod tests {
12171293
});
12181294

12191295
assert_snapshot!(output, @r#"
1220-
1970-01-01T00:00:00.000000Z INFO logfire::bridges::tracing::tests root span
1221-
1970-01-01T00:00:00.000001Z INFO logfire::bridges::tracing::tests hello world span
1222-
1970-01-01T00:00:00.000002Z DEBUG logfire::bridges::tracing::tests debug span
1223-
1970-01-01T00:00:00.000003Z DEBUG logfire::bridges::tracing::tests debug span with explicit parent
1224-
1970-01-01T00:00:00.000004Z INFO logfire::bridges::tracing::tests hello world log
1296+
1970-01-01T00:00:00.000000Z INFO logfire::bridges::tracing::tests root event
1297+
1970-01-01T00:00:00.000001Z INFO logfire::bridges::tracing::tests root event with value field_value=1
1298+
1970-01-01T00:00:00.000002Z INFO logfire::bridges::tracing::tests root span
1299+
1970-01-01T00:00:00.000003Z INFO logfire::bridges::tracing::tests hello world span
1300+
1970-01-01T00:00:00.000004Z DEBUG logfire::bridges::tracing::tests debug span
1301+
1970-01-01T00:00:00.000005Z DEBUG logfire::bridges::tracing::tests debug span with explicit parent
1302+
1970-01-01T00:00:00.000006Z INFO logfire::bridges::tracing::tests hello world log
1303+
1970-01-01T00:00:00.000007Z INFO logfire::bridges::tracing::tests hello world log with value field_value=1
12251304
"#);
12261305
}
12271306
}

src/internal/exporters/console.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,7 @@ impl ConsoleWriter {
195195

196196
let mut visitor = FieldsVisitor {
197197
message: None,
198-
// TODO: support formatting the fields? Maybe according to `ConsoleOptions`.
199-
// fields: Vec::new(),
198+
fields: Vec::new(),
200199
};
201200

202201
event.record(&mut visitor);
@@ -219,30 +218,39 @@ impl ConsoleWriter {
219218

220219
write!(w, " {}", BOLD.paint(msg))?;
221220

221+
if !visitor.fields.is_empty() {
222+
for (idx, (key, value)) in visitor.fields.iter().enumerate() {
223+
write!(w, " {}={value}", ITALIC.paint(*key))?;
224+
if idx < visitor.fields.len() - 1 {
225+
write!(w, ",")?;
226+
}
227+
}
228+
}
229+
222230
writeln!(w)
223231
}
224232
}
225233

226234
/// Internal helper to `visit` a `tracing::Event` and collect relevant fields.
227235
struct FieldsVisitor {
228236
message: Option<String>,
229-
// fields: Vec<(&'static str, String)>,
237+
fields: Vec<(&'static str, String)>,
230238
}
231239

232240
impl Visit for FieldsVisitor {
233241
fn record_str(&mut self, field: &tracing::field::Field, value: &str) {
234242
if field.name() == "message" {
235243
self.message = Some(value.to_string());
236244
} else {
237-
// self.fields.push((field.name(), value.to_string()));
245+
self.fields.push((field.name(), value.to_string()));
238246
}
239247
}
240248

241249
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
242250
if field.name() == "message" {
243251
self.message = Some(format!("{value:?}"));
244252
} else {
245-
// self.fields.push((field.name(), format!("{value:?}")));
253+
self.fields.push((field.name(), format!("{value:?}")));
246254
}
247255
}
248256
}
@@ -308,7 +316,7 @@ mod tests {
308316
1970-01-01T00:00:03.000000Z DEBUG logfire::internal::exporters::console::tests debug span
309317
1970-01-01T00:00:05.000000Z DEBUG logfire::internal::exporters::console::tests debug span with explicit parent
310318
1970-01-01T00:00:07.000000Z INFO logfire::internal::exporters::console::tests hello world log
311-
[2m1970-01-01T00:00:08.000000Z[0m[31m ERROR[0m [2;3mlogfire[0m [1mpanic: oh no![0m [3mlocation[0m=src/internal/exporters/console.rs:295:17, [3mbacktrace[0m=disabled backtrace
319+
[2m1970-01-01T00:00:08.000000Z[0m[31m ERROR[0m [2;3mlogfire[0m [1mpanic: oh no![0m [3mlocation[0m=src/internal/exporters/console.rs:303:17, [3mbacktrace[0m=disabled backtrace
312320
"#);
313321
}
314322
}

0 commit comments

Comments
 (0)