diff --git a/asterisk_ami.py b/asterisk_ami.py index c38a536..a9477d1 100644 --- a/asterisk_ami.py +++ b/asterisk_ami.py @@ -4,7 +4,7 @@ import socket import aiofiles import call_handler import config -import logging +from main import logger def send_action(s, action): @@ -69,21 +69,21 @@ async def ami_listening(): while True: try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - logging.info("Connecting to Asterisk...") + logger.info("Connecting to Asterisk...") s.connect((config.AMI_HOST, config.AMI_PORT)) break except ConnectionRefusedError: - logging.warning("Connection refused. Retrying...") + logger.warning("Connection refused. Retrying...") await asyncio.sleep(1) - logging.info("Connected to Asterisk") - logging.info("Logging in...") + logger.info("Connected to Asterisk") + logger.info("Logging in...") login_action = f"Action: Login\r\nUsername: {config.AMI_USER}\r\nSecret: {config.AMI_PASSWORD}\r\n\r\n" send_action(s, login_action) - logging.info("Logged in") - logging.info("Listening for events...") + logger.info("Logged in") + logger.info("Listening for events...") events_action = "Action: Events\r\nEventMask: on\r\n\r\n" send_action(s, events_action) @@ -96,9 +96,9 @@ async def ami_listening(): if config.DEBUG: await full_log(event) except (KeyboardInterrupt, SystemExit): - logging.info("Exiting...") + logger.info("Exiting...") s.close() daily_check_pending_task.cancel() except ConnectionResetError: - logging.warning("Connection reset. Restarting...") + logger.warning("Connection reset. Restarting...") await ami_listening() diff --git a/call_handler.py b/call_handler.py index c940041..f29ac7f 100644 --- a/call_handler.py +++ b/call_handler.py @@ -1,24 +1,11 @@ from datetime import datetime -import logging +from main import logger import wave import config import medods -import aiofiles import os -async def calls_to_log(in_dict): - date_dir = datetime.now().strftime("%Y-%m-%d") - date_dir_path = os.path.join("log", date_dir) - if not os.path.exists(date_dir_path): - os.makedirs(date_dir_path) - file_name = f"log/{date_dir}/{in_dict.get('Linkedid')}.log" - async with aiofiles.open(file_name, "a") as f: - await f.write(f"\n\n{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n") - for key, value in in_dict.items(): - await f.write(f"{key}: {value}\n") - - class CallHandler: def __init__(self): self.date = datetime.now().date() @@ -45,11 +32,11 @@ class CallHandler: def check_date(): if self.date != datetime.now().date(): self.date = datetime.now().date() - logging.info(f"Date changed to {self.date} and reset calls database") + logger.info(f"Date changed to {self.date} and reset calls database") if config.DEBUG: if len(self.calls) > 0: - logging.warning("Calls unhandled:") - logging.warning(self.calls) + logger.warning("Calls unhandled:") + logger.warning(self.calls) self.finished = [] self.pending = [] self.calls = {} @@ -57,9 +44,6 @@ class CallHandler: try: if check_linkedid(event): - if config.DEBUG: - await calls_to_log(event) - linkedid = event.get("Linkedid") if linkedid in self.finished: @@ -286,8 +270,8 @@ class CallHandler: ): await self.call_pending(linkedid) return - except: - pass + except Exception as e: + logger.error(e) async def incoming_call(self, event, linkedid): def phone_number(number: str): @@ -313,15 +297,13 @@ class CallHandler: } exten = event.get("Exten") if event.get("Exten") else event.get("Extension") - logging.info( - f"New incoming call: ID={linkedid}, Client={client}, Phone={exten}" - ) + logger.info(f"New incoming call: ID={linkedid}, Client={client}, Phone={exten}") await medods.incoming_call(linkedid, client, exten) async def call_started(self, linkedid, responsible): self.calls[linkedid]["started"] = datetime.now() - logging.info(f"Call started: ID={linkedid}, Responsible={responsible}") + logger.info(f"Call started: ID={linkedid}, Responsible={responsible}") await medods.call_started(linkedid) async def call_pending(self, linkedid): @@ -424,7 +406,7 @@ class CallHandler: ).total_seconds() ) ) - logging.info( + logger.info( f"Call finished: ID={linkedid}, Duration={duration}, Record ID={uniqueid}" ) await medods.call_finished(linkedid, duration) @@ -435,5 +417,5 @@ class CallHandler: self.calls.pop(linkedid) async def call_lost(self, linkedid): - logging.info(f"Call lost: ID={linkedid}") + logger.info(f"Call lost: ID={linkedid}") await medods.call_lost(linkedid) diff --git a/main.py b/main.py index 7c5a7ce..0d58a2d 100644 --- a/main.py +++ b/main.py @@ -5,19 +5,19 @@ import os import asterisk_ami import config +# Настройки логирования +if config.DEBUG: + if not os.path.exists("log"): + os.makedirs("log") + logging_conf = "logging_debug.conf" +else: + logging_conf = "logging.conf" + +logger = logging.getLogger("main") +logging.config.fileConfig(logging_conf) + async def main(): - - # Настройки логирования - if config.DEBUG: - if not os.path.exists("log"): - os.makedirs("log") - logging_conf = "logging_debug.conf" - else: - logging_conf = "logging.conf" - - logging.config.fileConfig(logging_conf) - # Подключение к AMI await asterisk_ami.ami_listening()