bitwarden_logging/
event.rs1use std::collections::HashMap;
4
5use serde::{Deserialize, Serialize};
6#[cfg(feature = "wasm")]
7use wasm_bindgen::prelude::*;
8
9use crate::visitor::MessageVisitor;
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
13#[serde(rename_all = "camelCase")]
14#[bitwarden_ffi::wasm_record]
15pub struct FlightRecorderEvent {
16 pub timestamp: i64,
18 pub level: String,
20 pub target: String,
22 pub message: String,
24 #[cfg_attr(feature = "wasm", tsify(type = "Record<string, string>"))]
26 pub fields: HashMap<String, String>,
27 #[serde(default, skip_serializing_if = "Option::is_none")]
30 #[cfg_attr(feature = "wasm", tsify(optional))]
31 pub process: Option<String>,
32}
33
34impl From<&tracing::Event<'_>> for FlightRecorderEvent {
35 fn from(event: &tracing::Event<'_>) -> Self {
36 let mut visitor = MessageVisitor::default();
37 event.record(&mut visitor);
38
39 Self {
40 timestamp: chrono::Utc::now().timestamp_millis(),
41 level: event.metadata().level().to_string(),
42 target: event.metadata().target().to_string(),
43 message: visitor.message,
44 fields: visitor.fields,
45 process: None,
46 }
47 }
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_event_serialization_roundtrip() {
56 let event = FlightRecorderEvent {
57 timestamp: 1234567890,
58 level: "INFO".to_string(),
59 target: "test::module".to_string(),
60 message: "Test message".to_string(),
61 fields: HashMap::new(),
62 process: None,
63 };
64
65 let json = serde_json::to_string(&event).expect("should serialize");
66 let deserialized: FlightRecorderEvent =
67 serde_json::from_str(&json).expect("should deserialize");
68
69 assert_eq!(deserialized.timestamp, 1234567890);
70 assert_eq!(deserialized.level, "INFO");
71 assert_eq!(deserialized.target, "test::module");
72 assert_eq!(deserialized.message, "Test message");
73 assert!(deserialized.fields.is_empty());
74 }
75
76 #[test]
77 fn test_event_with_fields() {
78 let mut fields = HashMap::new();
79 fields.insert("user_id".to_string(), "abc-123".to_string());
80 fields.insert("action".to_string(), "login".to_string());
81
82 let event = FlightRecorderEvent {
83 timestamp: 0,
84 level: "INFO".to_string(),
85 target: "test".to_string(),
86 message: "hello".to_string(),
87 fields,
88 process: None,
89 };
90
91 let json = serde_json::to_string(&event).expect("should serialize");
92 assert!(json.contains("\"user_id\":\"abc-123\""));
93 assert!(json.contains("\"action\":\"login\""));
94 }
95
96 #[test]
97 fn test_event_without_process_omits_field() {
98 let event = FlightRecorderEvent {
99 timestamp: 0,
100 level: "INFO".to_string(),
101 target: "test".to_string(),
102 message: "hello".to_string(),
103 fields: HashMap::new(),
104 process: None,
105 };
106
107 let json = serde_json::to_string(&event).expect("should serialize");
108 assert!(!json.contains("process"));
109 let deserialized: FlightRecorderEvent =
110 serde_json::from_str(&json).expect("should deserialize");
111 assert_eq!(deserialized.process, None);
112 }
113}