mirror of
https://github.com/draga79/NotiMail.git
synced 2024-09-19 19:14:19 +02:00
Improved logging for multi e-mail accounts to clarify which operation gets done and which is the mailbox involved
This commit is contained in:
parent
b85545bb9c
commit
5f9c0636e5
1 changed files with 15 additions and 13 deletions
28
NotiMail.py
28
NotiMail.py
|
@ -78,7 +78,8 @@ from email.parser import BytesParser
|
||||||
# Setup logging
|
# Setup logging
|
||||||
logging.basicConfig(filename='notimail.log',
|
logging.basicConfig(filename='notimail.log',
|
||||||
level=logging.INFO,
|
level=logging.INFO,
|
||||||
format='%(asctime)s - %(levelname)s - %(message)s')
|
format='%(asctime)s - %(threadName)s - %(levelname)s - %(message)s')
|
||||||
|
|
||||||
|
|
||||||
class DatabaseHandler:
|
class DatabaseHandler:
|
||||||
def __init__(self, db_name="processed_emails.db"):
|
def __init__(self, db_name="processed_emails.db"):
|
||||||
|
@ -274,12 +275,12 @@ class IMAPHandler:
|
||||||
self.mail.select('inbox')
|
self.mail.select('inbox')
|
||||||
except imaplib.IMAP4.error as e:
|
except imaplib.IMAP4.error as e:
|
||||||
print(f"Cannot connect: {str(e)}")
|
print(f"Cannot connect: {str(e)}")
|
||||||
notifier.send_notification("Script Error", f"Cannot connect: {str(e)}")
|
notifier.send_notification("Script Error", f"[{self.email_user}] Cannot connect: {str(e)}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def idle(self):
|
def idle(self):
|
||||||
print("IDLE mode started. Waiting for new email...")
|
print("IDLE mode started. Waiting for new email...")
|
||||||
logging.info(f"IDLE mode started. Waiting for new email...")
|
logging.info(f"[{self.email_user}] IDLE mode started. Waiting for new email...")
|
||||||
try:
|
try:
|
||||||
tag = self.mail._new_tag().decode()
|
tag = self.mail._new_tag().decode()
|
||||||
self.mail.send(f'{tag} IDLE\r\n'.encode('utf-8'))
|
self.mail.send(f'{tag} IDLE\r\n'.encode('utf-8'))
|
||||||
|
@ -289,28 +290,28 @@ class IMAPHandler:
|
||||||
if line:
|
if line:
|
||||||
print(line.decode('utf-8'))
|
print(line.decode('utf-8'))
|
||||||
if b'BYE' in line:
|
if b'BYE' in line:
|
||||||
raise ConnectionAbortedError("Received BYE from server. Trying to reconnect...")
|
raise ConnectionAbortedError("[{self.email_user}] Received BYE from server. Trying to reconnect...")
|
||||||
if b'EXISTS' in line:
|
if b'EXISTS' in line:
|
||||||
break
|
break
|
||||||
self.mail.send(b'DONE\r\n')
|
self.mail.send(b'DONE\r\n')
|
||||||
self.mail.readline()
|
self.mail.readline()
|
||||||
except imaplib.IMAP4.abort as e:
|
except imaplib.IMAP4.abort as e:
|
||||||
print(f"Connection closed by server: {str(e)}")
|
print(f"Connection closed by server: {str(e)}")
|
||||||
logging.error(f"Connection closed by server: {str(e)}")
|
logging.error(f"[{self.email_user}] Connection closed by server: {str(e)}")
|
||||||
notifier.send_notification("Script Error", f"Connection closed by server: {str(e)}")
|
notifier.send_notification("Script Error", f"[{self.email_user}] Connection closed by server: {str(e)}")
|
||||||
raise ConnectionAbortedError("Connection lost. Trying to reconnect...")
|
raise ConnectionAbortedError("[{self.email_user}] Connection lost. Trying to reconnect...")
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
print("Socket timeout during IDLE, re-establishing connection...")
|
print("Socket timeout during IDLE, re-establishing connection...")
|
||||||
logging.info(f"Socket timeout during IDLE, re-establishing connection...")
|
logging.info(f"[{self.email_user}] Socket timeout during IDLE, re-establishing connection...")
|
||||||
raise ConnectionAbortedError("Socket timeout. Trying to reconnect...")
|
raise ConnectionAbortedError("[{self.email_user}] Socket timeout. Trying to reconnect...")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"An error occurred: {str(e)}")
|
print(f"An error occurred: {str(e)}")
|
||||||
logging.info(f"An error occurred: {str(e)}")
|
logging.info(f"[{self.email_user}] An error occurred: {str(e)}")
|
||||||
notifier.send_notification("Script Error", f"An error occurred: {str(e)}")
|
notifier.send_notification("Script Error", f"[{self.email_user}] An error occurred: {str(e)}")
|
||||||
raise
|
raise
|
||||||
finally:
|
finally:
|
||||||
print("IDLE mode stopped.")
|
print("IDLE mode stopped.")
|
||||||
logging.info(f"IDLE mode stopped.")
|
logging.info(f"[{self.email_user}] IDLE mode stopped.")
|
||||||
|
|
||||||
def process_emails(self):
|
def process_emails(self):
|
||||||
processor = EmailProcessor(self.mail)
|
processor = EmailProcessor(self.mail)
|
||||||
|
@ -324,7 +325,8 @@ class MultiIMAPHandler:
|
||||||
def run(self):
|
def run(self):
|
||||||
threads = []
|
threads = []
|
||||||
for handler in self.handlers:
|
for handler in self.handlers:
|
||||||
thread = threading.Thread(target=self.monitor_account, args=(handler,))
|
#thread = threading.Thread(target=self.monitor_account, args=(handler,))
|
||||||
|
thread = threading.Thread(target=self.monitor_account, args=(handler,), name=handler.email_user)
|
||||||
thread.daemon = True # Set thread as daemon
|
thread.daemon = True # Set thread as daemon
|
||||||
threads.append(thread)
|
threads.append(thread)
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
Loading…
Reference in a new issue