@@ -36,10 +36,10 @@ pub static KNOWN_SCHEMA_LIST: Lazy<EventProcessor> =
3636pub struct Unacceptable ( String ) ;
3737
3838/// Defines a schema for extracting structured data from logs using regular expressions
39- #[ derive( Debug ) ]
39+ #[ derive( Debug , Default ) ]
4040pub struct SchemaDefinition {
4141 /// Regular expression pattern used to match and capture fields from log strings
42- pattern : Option < Regex > ,
42+ patterns : Vec < Regex > ,
4343 // Maps field names to regex capture groups
4444 field_mappings : Vec < HashSet < String > > ,
4545}
@@ -70,35 +70,35 @@ impl SchemaDefinition {
7070 return true ;
7171 }
7272
73- let Some ( pattern) = self . pattern . as_ref ( ) else {
74- return false ;
75- } ;
76-
7773 let Some ( event) = extract_log
7874 . and_then ( |field| obj. get ( field) )
7975 . and_then ( |s| s. as_str ( ) )
8076 else {
8177 return false ;
8278 } ;
8379
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+ }
9694 }
97- }
9895
99- obj. extend ( extracted_fields) ;
96+ obj. extend ( extracted_fields) ;
97+
98+ return true ;
99+ }
100100
101- true
101+ false
102102 }
103103}
104104
@@ -134,26 +134,24 @@ impl EventProcessor {
134134 serde_json:: from_str ( json_text) . expect ( "Known formats are stored as JSON text" ) ;
135135
136136 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 ( ) ) ;
138144 // Compile the regex pattern if present
139145 // 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)
142148 . inspect_err ( |err| {
143149 error ! ( "Error compiling regex pattern: {err}; Pattern: {pattern}" )
144150 } )
145151 . 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+ }
157155 }
158156 }
159157
@@ -345,7 +343,7 @@ mod tests {
345343 fn test_no_pattern_missing_fields ( ) {
346344 // Create a schema definition with no pattern
347345 let schema = SchemaDefinition {
348- pattern : None ,
346+ patterns : vec ! [ ] ,
349347 field_mappings : vec ! [ HashSet :: from_iter( [
350348 "field1" . to_string( ) ,
351349 "field2" . to_string( ) ,
0 commit comments