首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在两个日志之间添加新行或分隔符

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

Stack Overflow用户
提问于 2020-03-11 17:53:46
回答 1查看 409关注 0票数 4

下面的代码用于日志记录

代码语言:javascript
复制
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)

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

在评论中的建议之后:

代码语言:javascript
复制
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s \n")

我得到的是:

代码语言:javascript
复制
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)

但我想要的是:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-11 19:15:21

替换此行:

代码语言:javascript
复制
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")

有了这个:

代码语言:javascript
复制
formatter = logging.Formatter("\n%(asctime)s - %(name)s - %(levelname)s - %(message)s\n")
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60633260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档