Answer the question
In order to leave comments, you need to log in
How to save server logs to logs folder?
import logging
import os
# sys.path.append('../logs')
os.path.dirname(os.path.abspath('../logs'))
# создание объект-логге
server_logger = logging.getLogger('server')
# создание объекта форматирования
server_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
# создание файлового обработчика логирования
sfh = logging.FileHandler('server.log', encoding='utf-8')
# подключение формата к обработчику событий
sfh.setFormatter(server_formatter)
# добавление в логгер обработчика событий
server_logger.addHandler(sfh)
# установка уровня важности логирования
server_logger.setLevel(logging.NOTSET)
if __name__ == '__main__':
server_logger.critical('Критическая ошибка')
server_logger.error('Ошибка')
server_logger.debug('Отладочная информация')
server_logger.info('Информационное сообщение')
Answer the question
In order to leave comments, you need to log in
Logging to a file
import logging
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
logging.error('And non-ASCII stuff, too, like Øresund and Malmö')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question