1
0
Fork 0

Upgrade log files with rolling file per day

This commit is contained in:
Ethanell 2019-12-26 17:28:43 +01:00
parent eac1820d3a
commit f90e69a979
3 changed files with 21 additions and 11 deletions

View file

@ -13,6 +13,7 @@ from sqlalchemy.orm import sessionmaker
from TelegramEDT.EDTcalendar import Calendar from TelegramEDT.EDTcalendar import Calendar
from TelegramEDT.base import Base, User from TelegramEDT.base import Base, User
from TelegramEDT.lang import lang from TelegramEDT.lang import lang
from TelegramEDT.logger import logger
tables = False tables = False
if not isdir("logs"): if not isdir("logs"):
@ -22,14 +23,6 @@ if not isdir("calendars"):
if not isfile("edt.db"): if not isfile("edt.db"):
tables = True tables = True
logger = logging.getLogger("TelegramEDT")
log_date = datetime.datetime.now(datetime.timezone.utc).astimezone(tz=None).date()
logging.basicConfig(
filename=f"logs/{log_date}.log",
format="{%(levelname)s}[%(asctime)s]: %(name)s | %(message)s",
level=logging.INFO,
)
if not isfile("token.ini"): if not isfile("token.ini"):
logger.critical("No token specified, impossible to start the bot !") logger.critical("No token specified, impossible to start the bot !")
exit(1) exit(1)

15
TelegramEDT/logger.py Normal file
View file

@ -0,0 +1,15 @@
import logging
from logging import handlers
log_format = "{%(levelname)s}[%(asctime)s]: %(name)s | %(message)s"
logging.basicConfig(
format=log_format,
level=logging.INFO
)
logger = logging.getLogger("TelegramEDT")
handler = handlers.TimedRotatingFileHandler("logs/current.log", when="d", interval=1)
handler.suffix = "%Y-%m-%d"
handler.style = log_format
handler.setFormatter(logging.Formatter(log_format))
logger.addHandler(handler)

View file

@ -1,9 +1,11 @@
import datetime
from aiogram import types from aiogram import types
from aiogram.types import ParseMode from aiogram.types import ParseMode
from aiogram.utils import markdown from aiogram.utils import markdown
from aiogram.utils.exceptions import MessageIsTooLong from aiogram.utils.exceptions import MessageIsTooLong
from TelegramEDT import ADMIN_ID, bot, dbL, dp, key, log_date, logger, session, check_id from TelegramEDT import ADMIN_ID, bot, dbL, dp, key, logger, session, check_id
from TelegramEDT.base import User from TelegramEDT.base import User
logger = logger.getChild("tools") logger = logger.getChild("tools")
@ -24,11 +26,11 @@ async def get_logs(message: types.Message):
int(message.text[9:]) int(message.text[9:])
except ValueError: except ValueError:
await message.chat.do(types.ChatActions.UPLOAD_DOCUMENT) await message.chat.do(types.ChatActions.UPLOAD_DOCUMENT)
await message.reply_document(types.InputFile(f"logs/{log_date}.log"), caption=f"The {log_date} logs", await message.reply_document(types.InputFile(f"logs/current.log"), caption="The logs file",
reply_markup=key) reply_markup=key)
else: else:
await message.chat.do(types.ChatActions.TYPING) await message.chat.do(types.ChatActions.TYPING)
logs = (open(f"logs/{log_date}.log", "r").readlines())[-int(message.text[9:]):] logs = (open(f"logs/current.log", "r").readlines())[-int(message.text[9:]):]
log = str() log = str()
for i in logs: for i in logs:
log += i log += i