@@ -110,7 +110,7 @@ class StructuredLogger:
110110 """Sniffs device logs for structured log messages, extracts those into apache arrow format.
111111 Also writes the raw log messages to raw.txt"""
112112
113- def __init__ (self , client : MeshInterface , dir_path : str ) -> None :
113+ def __init__ (self , client : MeshInterface , dir_path : str , include_raw = True ) -> None :
114114 """Initialize the StructuredLogger object.
115115
116116 client (MeshInterface): The MeshInterface object to monitor.
@@ -123,6 +123,10 @@ def __init__(self, client: MeshInterface, dir_path: str) -> None:
123123 (lambda x , y : x + y ), map (lambda x : x .fields , log_defs .values ())
124124 )
125125
126+ self .include_raw = include_raw
127+ if self .include_raw :
128+ all_fields .append (("raw" , pa .string ()))
129+
126130 self .writer .set_schema (pa .schema (all_fields ))
127131
128132 self .raw_file : Optional [
@@ -151,6 +155,9 @@ def _onLogMessage(self, line: str) -> None:
151155
152156 line (str): the line of log output
153157 """
158+
159+ di = {} # the dictionary of the fields we found to log
160+
154161 m = log_regex .match (line )
155162 if m :
156163 src = m .group (1 )
@@ -163,13 +170,18 @@ def _onLogMessage(self, line: str) -> None:
163170 r = d .format .parse (args ) # get the values with the correct types
164171 if r :
165172 di = r .named
166- di ["time" ] = datetime .now ()
167- self .writer .add_row (di )
168173 else :
169174 logging .warning (f"Failed to parse slog { line } with { d .format } " )
170175 else :
171176 logging .warning (f"Unknown Structured Log: { line } " )
172177
178+ # Store our structured log record
179+ if di or self .include_raw :
180+ di ["time" ] = datetime .now ()
181+ if self .include_raw :
182+ di ["raw" ] = line
183+ self .writer .add_row (di )
184+
173185 if self .raw_file :
174186 self .raw_file .write (line + "\n " ) # Write the raw log
175187
0 commit comments