【问题标题】:How to add a new line or a separator between two logs如何在两个日志之间添加新行或分隔符
【发布时间】:2020-03-11 09:53:45
【问题描述】:

以下代码用于记录

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)

如何在创建每个新日志后添加分隔符(一个新行或一组连字符)?

在 cmets 中提出建议后:

    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)

【问题讨论】:

  • 您是否尝试在logging.Formatter() 字符串输入的末尾添加\n
  • 我有这个要添加一个\n,但不知道我可以在哪里添加它
  • @Arnaud 添加后没有效果 mate ......
  • 这能回答你的问题吗? How to insert newline in python logging?
  • 不,我已经看到并尝试过相同的,....

标签: python-3.x flask


【解决方案1】:

替换这一行:

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

用这个:

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

【讨论】:

  • 哇,谢谢伙计....想不到在开头添加一个'\n'实际上可以节省!!
猜你喜欢
  • 2015-12-08
  • 2022-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-24
  • 2012-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多