@@ -36,10 +36,10 @@ pub static KNOWN_SCHEMA_LIST: Lazy<EventProcessor> =
36
36
pub struct Unacceptable ( String ) ;
37
37
38
38
/// Defines a schema for extracting structured data from logs using regular expressions
39
- #[ derive( Debug ) ]
39
+ #[ derive( Debug , Default ) ]
40
40
pub struct SchemaDefinition {
41
41
/// Regular expression pattern used to match and capture fields from log strings
42
- pattern : Option < Regex > ,
42
+ patterns : Vec < Regex > ,
43
43
// Maps field names to regex capture groups
44
44
field_mappings : Vec < HashSet < String > > ,
45
45
}
@@ -70,35 +70,35 @@ impl SchemaDefinition {
70
70
return true ;
71
71
}
72
72
73
- let Some ( pattern) = self . pattern . as_ref ( ) else {
74
- return false ;
75
- } ;
76
-
77
73
let Some ( event) = extract_log
78
74
. and_then ( |field| obj. get ( field) )
79
75
. and_then ( |s| s. as_str ( ) )
80
76
else {
81
77
return false ;
82
78
} ;
83
79
84
- let Some ( captures) = pattern. captures ( event) else {
85
- return false ;
86
- } ;
87
- let mut extracted_fields = Map :: new ( ) ;
88
-
89
- // With named capture groups, you can iterate over the field names
90
- for field_name in self . field_mappings . iter ( ) . flatten ( ) {
91
- if let Some ( value) = captures. name ( field_name) {
92
- extracted_fields. insert (
93
- field_name. to_owned ( ) ,
94
- Value :: String ( value. as_str ( ) . to_string ( ) ) ,
95
- ) ;
80
+ for pattern in self . patterns . iter ( ) {
81
+ let Some ( captures) = pattern. captures ( event) else {
82
+ continue ;
83
+ } ;
84
+ let mut extracted_fields = Map :: new ( ) ;
85
+
86
+ // With named capture groups, you can iterate over the field names
87
+ for field_name in self . field_mappings . iter ( ) . flatten ( ) {
88
+ if let Some ( value) = captures. name ( field_name) {
89
+ extracted_fields. insert (
90
+ field_name. to_owned ( ) ,
91
+ Value :: String ( value. as_str ( ) . to_string ( ) ) ,
92
+ ) ;
93
+ }
96
94
}
97
- }
98
95
99
- obj. extend ( extracted_fields) ;
96
+ obj. extend ( extracted_fields) ;
97
+
98
+ return true ;
99
+ }
100
100
101
- true
101
+ false
102
102
}
103
103
}
104
104
@@ -134,26 +134,24 @@ impl EventProcessor {
134
134
serde_json:: from_str ( json_text) . expect ( "Known formats are stored as JSON text" ) ;
135
135
136
136
for format in formats {
137
- for regex in & format. regex {
137
+ for regex in format. regex {
138
+ let schema = processor
139
+ . schema_definitions
140
+ . entry ( format. name . clone ( ) )
141
+ . or_insert_with ( SchemaDefinition :: default) ;
142
+
143
+ schema. field_mappings . push ( regex. fields . clone ( ) ) ;
138
144
// Compile the regex pattern if present
139
145
// NOTE: we only warn if the pattern doesn't compile
140
- let pattern = regex. pattern . as_ref ( ) . and_then ( |pattern| {
141
- Regex :: new ( pattern)
146
+ if let Some ( pattern) = regex. pattern . and_then ( |pattern| {
147
+ Regex :: new ( & pattern)
142
148
. inspect_err ( |err| {
143
149
error ! ( "Error compiling regex pattern: {err}; Pattern: {pattern}" )
144
150
} )
145
151
. ok ( )
146
- } ) ;
147
-
148
- let schema = processor
149
- . schema_definitions
150
- . entry ( format. name . clone ( ) )
151
- . or_insert_with ( || SchemaDefinition {
152
- pattern,
153
- field_mappings : vec ! [ ] ,
154
- } ) ;
155
-
156
- schema. field_mappings . push ( regex. fields . clone ( ) ) ;
152
+ } ) {
153
+ schema. patterns . push ( pattern) ;
154
+ }
157
155
}
158
156
}
159
157
@@ -345,7 +343,7 @@ mod tests {
345
343
fn test_no_pattern_missing_fields ( ) {
346
344
// Create a schema definition with no pattern
347
345
let schema = SchemaDefinition {
348
- pattern : None ,
346
+ patterns : vec ! [ ] ,
349
347
field_mappings : vec ! [ HashSet :: from_iter( [
350
348
"field1" . to_string( ) ,
351
349
"field2" . to_string( ) ,
0 commit comments