изменение системы логирования
This commit is contained in:
+9
-9
@@ -4,7 +4,7 @@ import socket
|
|||||||
import aiofiles
|
import aiofiles
|
||||||
import call_handler
|
import call_handler
|
||||||
import config
|
import config
|
||||||
import logging
|
from main import logger
|
||||||
|
|
||||||
|
|
||||||
def send_action(s, action):
|
def send_action(s, action):
|
||||||
@@ -69,21 +69,21 @@ async def ami_listening():
|
|||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
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))
|
s.connect((config.AMI_HOST, config.AMI_PORT))
|
||||||
break
|
break
|
||||||
except ConnectionRefusedError:
|
except ConnectionRefusedError:
|
||||||
logging.warning("Connection refused. Retrying...")
|
logger.warning("Connection refused. Retrying...")
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
logging.info("Connected to Asterisk")
|
logger.info("Connected to Asterisk")
|
||||||
logging.info("Logging in...")
|
logger.info("Logging in...")
|
||||||
|
|
||||||
login_action = f"Action: Login\r\nUsername: {config.AMI_USER}\r\nSecret: {config.AMI_PASSWORD}\r\n\r\n"
|
login_action = f"Action: Login\r\nUsername: {config.AMI_USER}\r\nSecret: {config.AMI_PASSWORD}\r\n\r\n"
|
||||||
send_action(s, login_action)
|
send_action(s, login_action)
|
||||||
|
|
||||||
logging.info("Logged in")
|
logger.info("Logged in")
|
||||||
logging.info("Listening for events...")
|
logger.info("Listening for events...")
|
||||||
|
|
||||||
events_action = "Action: Events\r\nEventMask: on\r\n\r\n"
|
events_action = "Action: Events\r\nEventMask: on\r\n\r\n"
|
||||||
send_action(s, events_action)
|
send_action(s, events_action)
|
||||||
@@ -96,9 +96,9 @@ async def ami_listening():
|
|||||||
if config.DEBUG:
|
if config.DEBUG:
|
||||||
await full_log(event)
|
await full_log(event)
|
||||||
except (KeyboardInterrupt, SystemExit):
|
except (KeyboardInterrupt, SystemExit):
|
||||||
logging.info("Exiting...")
|
logger.info("Exiting...")
|
||||||
s.close()
|
s.close()
|
||||||
daily_check_pending_task.cancel()
|
daily_check_pending_task.cancel()
|
||||||
except ConnectionResetError:
|
except ConnectionResetError:
|
||||||
logging.warning("Connection reset. Restarting...")
|
logger.warning("Connection reset. Restarting...")
|
||||||
await ami_listening()
|
await ami_listening()
|
||||||
|
|||||||
+10
-28
@@ -1,24 +1,11 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import logging
|
from main import logger
|
||||||
import wave
|
import wave
|
||||||
import config
|
import config
|
||||||
import medods
|
import medods
|
||||||
import aiofiles
|
|
||||||
import os
|
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:
|
class CallHandler:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.date = datetime.now().date()
|
self.date = datetime.now().date()
|
||||||
@@ -45,11 +32,11 @@ class CallHandler:
|
|||||||
def check_date():
|
def check_date():
|
||||||
if self.date != datetime.now().date():
|
if self.date != datetime.now().date():
|
||||||
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 config.DEBUG:
|
||||||
if len(self.calls) > 0:
|
if len(self.calls) > 0:
|
||||||
logging.warning("Calls unhandled:")
|
logger.warning("Calls unhandled:")
|
||||||
logging.warning(self.calls)
|
logger.warning(self.calls)
|
||||||
self.finished = []
|
self.finished = []
|
||||||
self.pending = []
|
self.pending = []
|
||||||
self.calls = {}
|
self.calls = {}
|
||||||
@@ -57,9 +44,6 @@ class CallHandler:
|
|||||||
try:
|
try:
|
||||||
if check_linkedid(event):
|
if check_linkedid(event):
|
||||||
|
|
||||||
if config.DEBUG:
|
|
||||||
await calls_to_log(event)
|
|
||||||
|
|
||||||
linkedid = event.get("Linkedid")
|
linkedid = event.get("Linkedid")
|
||||||
|
|
||||||
if linkedid in self.finished:
|
if linkedid in self.finished:
|
||||||
@@ -286,8 +270,8 @@ class CallHandler:
|
|||||||
):
|
):
|
||||||
await self.call_pending(linkedid)
|
await self.call_pending(linkedid)
|
||||||
return
|
return
|
||||||
except:
|
except Exception as e:
|
||||||
pass
|
logger.error(e)
|
||||||
|
|
||||||
async def incoming_call(self, event, linkedid):
|
async def incoming_call(self, event, linkedid):
|
||||||
def phone_number(number: str):
|
def phone_number(number: str):
|
||||||
@@ -313,15 +297,13 @@ class CallHandler:
|
|||||||
}
|
}
|
||||||
|
|
||||||
exten = event.get("Exten") if event.get("Exten") else event.get("Extension")
|
exten = event.get("Exten") if event.get("Exten") else event.get("Extension")
|
||||||
logging.info(
|
logger.info(f"New incoming call: ID={linkedid}, Client={client}, Phone={exten}")
|
||||||
f"New incoming call: ID={linkedid}, Client={client}, Phone={exten}"
|
|
||||||
)
|
|
||||||
await medods.incoming_call(linkedid, client, exten)
|
await medods.incoming_call(linkedid, client, exten)
|
||||||
|
|
||||||
async def call_started(self, linkedid, responsible):
|
async def call_started(self, linkedid, responsible):
|
||||||
|
|
||||||
self.calls[linkedid]["started"] = datetime.now()
|
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)
|
await medods.call_started(linkedid)
|
||||||
|
|
||||||
async def call_pending(self, linkedid):
|
async def call_pending(self, linkedid):
|
||||||
@@ -424,7 +406,7 @@ class CallHandler:
|
|||||||
).total_seconds()
|
).total_seconds()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
logging.info(
|
logger.info(
|
||||||
f"Call finished: ID={linkedid}, Duration={duration}, Record ID={uniqueid}"
|
f"Call finished: ID={linkedid}, Duration={duration}, Record ID={uniqueid}"
|
||||||
)
|
)
|
||||||
await medods.call_finished(linkedid, duration)
|
await medods.call_finished(linkedid, duration)
|
||||||
@@ -435,5 +417,5 @@ class CallHandler:
|
|||||||
self.calls.pop(linkedid)
|
self.calls.pop(linkedid)
|
||||||
|
|
||||||
async def call_lost(self, 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)
|
await medods.call_lost(linkedid)
|
||||||
|
|||||||
@@ -5,19 +5,19 @@ import os
|
|||||||
import asterisk_ami
|
import asterisk_ami
|
||||||
import config
|
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():
|
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
|
# Подключение к AMI
|
||||||
await asterisk_ami.ami_listening()
|
await asterisk_ami.ami_listening()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user