Skip to content

Commit

Permalink
New logging system
Browse files Browse the repository at this point in the history
  • Loading branch information
boomb0om committed Jul 10, 2022
1 parent 60c2552 commit ba4f336
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 13 deletions.
9 changes: 8 additions & 1 deletion bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ async def reply_image(message: types.Message, state: FSMContext):
"Can't recognize your attached photo"
)
return

logger.info(f'{message.from_user.id} sended photo')

image_bytes = await download_to_bytes(file_url)
image_bytes.name = filename
Expand All @@ -107,6 +109,8 @@ async def reply_document(message: types.Message, state: FSMContext):
)
return

logger.info(f'{message.from_user.id} sended document')

document_bytes = await download_to_bytes(file_url)
document_bytes.name = filename
# sending document using errors-safe method
Expand Down Expand Up @@ -140,14 +144,17 @@ async def keyboard_handler_message(message: types.Message, state: FSMContext):


async def on_startup(dispatcher):
logger.note('Bot started')
print('on_startup')

async def on_shutdown(dispatcher):
logger.note('Bot shutting down')
print('on_shutdown')


if __name__ == "__main__":
logger = setup_logger('test_bot.log')
bot_name = 'test_bot' if args.test else 'bot'
logger = setup_logger(bot_name, logging_dir=f'logs/{bot_name}')

# auth
executor.start_polling(dp, on_startup=on_startup, on_shutdown=on_shutdown, skip_updates=True)
56 changes: 44 additions & 12 deletions utils/logger.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,51 @@
import logging
import os
from logging.handlers import RotatingFileHandler
import sys
import os
from logging import Filter, LogRecord
import logging.config

# add new log level
NOTE_LEVEL_NUM = 25
logging.addLevelName(NOTE_LEVEL_NUM, "NOTE")
def notev(self, message, *args, **kws):
# Yes, logger takes its '*args' as 'args'.
self._log(NOTE_LEVEL_NUM, message, args, **kws)
logging.Logger.note = notev
logging.NOTE = NOTE_LEVEL_NUM

class LevelFilter(Filter):
def __init__(self, levels):
super().__init__()
self.levels = levels

def filter(self, record: LogRecord) -> int:
passed = record.levelno in self.levels
return passed


from .logger_config import LOGGERS_CONFIG

def setup_logger(bot_name, logging_dir='logs'):
logfile_errors = os.path.join(logging_dir, f'errors_{bot_name}.log')
logfile_info = os.path.join(logging_dir, f'info_{bot_name}.log')
logfile_debug = os.path.join(logging_dir, f'debug_{bot_name}.log')
logfile_main = os.path.join(logging_dir, f'main_{bot_name}.log')

def setup_logger(filename, logger_name='bot_logger', logging_dir='./logs/'):
os.makedirs(logging_dir, exist_ok=True)
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG)

s_handler = logging.StreamHandler(sys.stdout)
s_handler.setLevel(logging.DEBUG)
logger.addHandler(s_handler)
LOGGERS_CONFIG['handlers']['errors_file']['filename'] = logfile_errors
LOGGERS_CONFIG['handlers']['common_file']['filename'] = logfile_info
LOGGERS_CONFIG['handlers']['debug_file']['filename'] = logfile_debug
LOGGERS_CONFIG['handlers']['main_file']['filename'] = logfile_main

f_handler = logging.FileHandler(os.path.join(logging_dir, filename))
f_handler.setLevel(logging.WARNING)
f_format = logging.Formatter('[%(asctime)s][%(levelname)s] %(message)s')
f_handler.setFormatter(f_format)
logger.addHandler(f_handler)
logger_name = f'logger_{bot_name}'
LOGGERS_CONFIG['loggers'][logger_name] = {
'handlers': ['debug_file', 'errors_file', 'common_file', 'main_file', 'console'],
'level': logging.DEBUG
}

logging.config.dictConfig(LOGGERS_CONFIG)
logger = logging.getLogger(logger_name)
logger.note('Logger initialized')
return logger
63 changes: 63 additions & 0 deletions utils/logger_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import sys
import logging

from .logger import *

LOGGERS_CONFIG = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'default': {
'format': '[%(asctime)s][%(levelname)s]: %(message)s'
}
},
'filters': {
'important_only': {
'()': 'utils.logger.LevelFilter',
'levels': [logging.NOTE, logging.CRITICAL]
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'default',
'level': logging.DEBUG,
'stream': sys.stdout
},
'errors_file': {
'class': 'logging.FileHandler',
'formatter': 'default',
'filename': '',
'level': logging.NOTE, # logging.NOTE = 25
},
'common_file': {
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'default',
'level': logging.INFO,
'filename': '',
'maxBytes': 1024*1024*5,
'backupCount': 1
},
'debug_file': {
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'default',
'level': logging.DEBUG,
'filename': '',
'maxBytes': 1024*1024*2,
'backupCount': 1
},
'main_file': {
'class': 'logging.FileHandler',
'formatter': 'default',
'filters': ['important_only'],
'level': logging.NOTE,
'filename': '',
},
},
'loggers': {
'template': {
'handlers': ['console', 'errors_file', 'common_file', 'main_file'],
'level': logging.DEBUG
},
}
}

0 comments on commit ba4f336

Please sign in to comment.