以下代码用于记录
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
替换此行:
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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。