开发者社区> 问答> 正文

如何在两个日志之间添加新行或分隔符

以下代码用于记录

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)

问题来源:stackoverflow

展开
收起
is大龙 2020-03-24 17:52:54 465 0
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")
    

    回答来源:stackoverflow

    2020-03-24 17:53:03
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
PostgresChina2018_赖思超_PostgreSQL10_hash索引的WAL日志修改版final 立即下载
Kubernetes下日志实时采集、存储与计算实践 立即下载
日志数据采集与分析对接 立即下载