4444}
4545
4646# Argument parsing
47- parser = argparse .ArgumentParser (description = 'Process log files with colored output.' )
47+ parser = argparse .ArgumentParser (description = 'Process Zeek log files with colored output.' )
4848parser .add_argument ('-f' , '--foreground' , action = 'store_true' , help = 'Use foreground colors' )
4949parser .add_argument ('-d' , '--directory' , type = str , required = True , help = 'Zeek log directory' )
50- parser .add_argument ('-c' , '--filter-conn' , action = 'store_true' , help = 'If the flow is in other file, dont show the entry from conn.log' )
50+ parser .add_argument ('-c' , '--filter-conn' , action = 'store_true' , help = 'Filter conn.log by UID ' )
5151parser .add_argument ('-n' , '--no-ts-conversion' , action = 'store_true' , help = 'Disable timestamp conversion' )
5252parser .add_argument ('-t' , '--timezone' , type = str , default = 'UTC+2' , help = 'Timezone (e.g. UTC+2)' )
5353
@@ -88,7 +88,7 @@ def process_text_log_line(log_type, parts):
8888
8989 record = dict (zip (header , parts ))
9090
91- if not args .no_ts_conversion and 'ts' in record :
91+ if not args .no_ts_conversation and 'ts' in record :
9292 record ['ts' ] = convert_ts (record ['ts' ])
9393
9494 if log_type == 'files' and 'uid' in record :
@@ -110,9 +110,8 @@ def process_json_log_line(log_type, data):
110110 if not args .no_ts_conversion and 'ts' in data :
111111 data ['ts' ] = convert_ts (data ['ts' ])
112112
113- if 'uid' in data :
114- if log_type != 'conn' :
115- uids .add (data ['uid' ])
113+ if 'uid' in data and log_type != 'conn' :
114+ uids .add (data ['uid' ])
116115
117116 if log_type == 'conn' :
118117 conn_entries .append (data )
@@ -124,7 +123,7 @@ def process_json_log_line(log_type, data):
124123 ] + [str (v ) for k , v in data .items () if k not in ('ts' , 'uid' )])
125124 log_entries .append ((line , color_scheme [log_type ]))
126125
127- # Read each log file
126+ # Read and process each log file
128127for log_type , filename in file_patterns .items ():
129128 filepath = os .path .join (args .directory , filename )
130129 if os .path .isfile (filepath ):
@@ -141,40 +140,40 @@ def process_json_log_line(log_type, data):
141140 process_text_log_line (log_type , parts )
142141
143142# Handle conn.log with optional UID filtering
144- # -------------------------------------------
145- # This block processes all collected connection records (conn.log).
146- # It checks whether each connection's UID is already present in other logs,
147- # and optionally filters them out if the --filter-conn flag is set.
148- # The idea is that if the flow is on other file appart from conn.log,
149- # you know it has a conn.log entry, so dont show it
150143for record in conn_entries :
151144 uid = record .get ('uid' , '-' )
152145 if args .filter_conn and uid in uids :
153146 continue
147+
154148 ts_val = record .get ('ts' , '0' )
155149 ts_str = ts_val if args .no_ts_conversion or ' ' in str (ts_val ) else convert_ts (ts_val )
150+
151+ # Determine fields to print
152+ fields = log_headers .get ('conn' )
153+ if not fields :
154+ fields = sorted (k for k in record .keys () if k not in ('ts' , 'uid' ))
155+
156156 line = '\t ' .join ([
157157 ts_str ,
158158 'conn' ,
159159 uid
160- ] + [record .get (k , '-' ) for k in log_headers . get ( 'conn' , []) if k not in ( 'ts' , 'uid' ) ])
160+ ] + [str ( record .get (k , '-' )) for k in fields ])
161161 log_entries .append ((line , color_scheme ['conn' ]))
162162
163- # Sort all log entries by timestamp (handles both raw and formatted timestamps)
163+ # Sort log entries by timestamp (handles both formatted and raw timestamps)
164164def extract_ts (entry ):
165165 ts_str = entry [0 ].split ('\t ' )[0 ]
166166 try :
167167 if ' ' in ts_str :
168- ts_main = ' ' .join (ts_str .split (' ' )[:2 ]) # "YYYY-MM-DD HH:MM:SS.microsec"
168+ ts_main = ' ' .join (ts_str .split (' ' )[:2 ]) # e.g. '2025-03-31 08:41:08.271441'
169169 return datetime .strptime (ts_main , "%Y-%m-%d %H:%M:%S.%f" ).timestamp ()
170- else :
171- return float (ts_str )
170+ return float (ts_str )
172171 except :
173172 return 0
174173
175174log_entries .sort (key = extract_ts )
176175
177- # Output
176+ # Print results
178177for line , color in log_entries :
179178 print (f"{ color } { line } { reset_color } " )
180179
0 commit comments