Как добавить новую строку или разделитель между двумя журналами - PullRequest
4 голосов
/ 11 марта 2020

Приведенный ниже код предназначен для ведения журнала

app = flask.Flask(__name__)

if app.debug is not True:
    import logging
    from logging.handlers import RotatingFileHandler
    file_handler = RotatingFileHandler('error.log', maxBytes=1024 * 1024 * 100, backupCount=20)
    file_handler.setLevel(logging.ERROR)
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    file_handler.setFormatter(formatter)

    app.logger.addHandler(file_handler)

Как добавить разделитель (новую строку или группу дефисов) после каждого нового созданного журнала?

После предложения в комментариях :

    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s \n")

Я получил как:

2020-03-11 15:36:31,289 - testapp - ERROR - Exception on /test/3456789876543333334567-letter-words [GET]   
**newline added here**
Traceback (most recent call last):
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
2020-03-11 15:36:31,289 - testapp - ERROR - Exception on /test/3456789876543333334567-letter-words [GET]   
**newline added here**
Traceback (most recent call last):
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)

Но я хотел что-то вроде:

2020-03-11 15:36:31,289 - testapp - ERROR - Exception on /test/3456789876543333334567-letter-words [GET]    
Traceback (most recent call last):
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
**newline to be added here**
2020-03-11 15:36:31,289 - testapp - ERROR - Exception on /test/3456789876543333334567-letter-words [GET]   
Traceback (most recent call last):
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)

1 Ответ

3 голосов
/ 11 марта 2020

Заменить эту строку:

formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")

на эту:

formatter = logging.Formatter("\n%(asctime)s - %(name)s - %(levelname)s - %(message)s\n")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...