Deal with unexpected chars in log

This commit is contained in:
Brian Read 2024-06-14 15:48:14 +01:00
parent b5970977e6
commit 528bebf7a7
2 changed files with 25 additions and 20 deletions

View File

@ -5,7 +5,7 @@
<html><head> <html><head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>SMEServer Mailstats</title> <title>SMEServer Mailstats</title>
<link rel='stylesheet' type='text/css' href='../css/mailstats.css' /> <link rel='stylesheet' type='text/css' href='css/mailstats.css' />
<!-- Check links --> <!-- Check links -->
<script> <script>
function LinkCheck(url){ function LinkCheck(url){

View File

@ -31,6 +31,7 @@ from collections import defaultdict
import smtplib import smtplib
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from email.mime.text import MIMEText
import codecs
Mailstats_version = '1.2' Mailstats_version = '1.2'
@ -188,25 +189,29 @@ def read_in_yesterday_log_file(file_path):
# Get current date and calculate yesterday's date # Get current date and calculate yesterday's date
log_entries = [] log_entries = []
skip_record_count = 0; skip_record_count = 0;
with open(file_path, 'r') as file: with codecs.open(file_path, 'rb','utf-8', errors='replace') as file:
for Line in file: try:
#extract time stamp for Line in file:
try: #extract time stamp
entry = split_timestamp_and_data(Line) try:
# compare with yesterday entry = split_timestamp_and_data(Line)
timestamp_str = truncate_microseconds(entry[0]) # compare with yesterday
except ValueError as e: timestamp_str = truncate_microseconds(entry[0])
#print(f"ValueError {e} on timestamp create {timestamp_str}:{entry[0]} {entry[1]}") except ValueError as e:
skip_record_count += 1 #print(f"ValueError {e} on timestamp create {timestamp_str}:{entry[0]} {entry[1]}")
continue skip_record_count += 1
# Parse the timestamp string into a datetime object continue
# Ignoring extra microseconds # Parse the timestamp string into a datetime object
try: # Ignoring extra microseconds
timestamp = datetime.strptime(timestamp_str, "%Y-%m-%d %H:%M:%S") try:
except ValueError as e: timestamp = datetime.strptime(timestamp_str, "%Y-%m-%d %H:%M:%S")
print(f"ValueError {e} on timestamp extract {timestamp_str}:{entry[1]}") except ValueError as e:
if timestamp.date() == yesterday.date(): print(f"ValueError {e} on timestamp extract {timestamp_str}:{entry[1]}")
log_entries.append((timestamp, entry[1])) if timestamp.date() == yesterday.date():
log_entries.append((timestamp, entry[1]))
except UnicodeDecodeError as e:
#print(f"{Line} {len(log_entries)} {e} ")
pass
return [log_entries,skip_record_count] return [log_entries,skip_record_count]
def filter_summary_records(log_entries): def filter_summary_records(log_entries):