2023-11-30 12:38:52 +05:00
|
|
|
import logging
|
|
|
|
import logging.handlers
|
|
|
|
|
|
|
|
|
2023-12-01 12:06:03 +05:00
|
|
|
def setup_logging(name):
|
2023-11-30 12:38:52 +05:00
|
|
|
""" Создание логгера """
|
|
|
|
logger = logging.getLogger()
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
""" Создание обработчика для записи логов в файл """
|
|
|
|
|
2023-12-01 12:06:03 +05:00
|
|
|
file_handler = logging.FileHandler(
|
|
|
|
filename=f"{name}.log",
|
2023-12-04 11:04:16 +05:00
|
|
|
mode='w',
|
|
|
|
encoding="UTF-8"
|
2023-12-01 12:06:03 +05:00
|
|
|
#maxBytes=1024*1024,
|
|
|
|
#backupCount=5
|
2023-11-30 12:38:52 +05:00
|
|
|
)
|
|
|
|
file_handler.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
""" Создание обработчика для вывода логов на консоль """
|
|
|
|
|
|
|
|
console_handler = logging.StreamHandler()
|
2023-12-01 12:06:03 +05:00
|
|
|
console_handler.setLevel(logging.INFO)
|
2023-11-30 12:38:52 +05:00
|
|
|
|
|
|
|
""" Форматтер для вывода в файл """
|
|
|
|
|
|
|
|
file_formatter = logging.Formatter(
|
2023-12-01 12:06:03 +05:00
|
|
|
'%(asctime)s [%(levelname)s] [%(name)s] - %(message)s'
|
2023-11-30 12:38:52 +05:00
|
|
|
)
|
|
|
|
|
|
|
|
""" Форматтер для вывода на консоль """
|
|
|
|
|
|
|
|
console_formatter = logging.Formatter(
|
|
|
|
'%(asctime)s [%(levelname)s] [%(name)s] - %(message)s'
|
|
|
|
)
|
|
|
|
|
|
|
|
""" Добавление форматтеров в обработчики """
|
|
|
|
|
|
|
|
file_handler.setFormatter(file_formatter)
|
|
|
|
console_handler.setFormatter(console_formatter)
|
|
|
|
|
|
|
|
""" Добавление обработчиков в логгер """
|
|
|
|
|
|
|
|
logger.addHandler(file_handler)
|
|
|
|
logger.addHandler(console_handler)
|
|
|
|
|
|
|
|
return logger
|