最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Flask中输出log日志到文件且自定义输出格式

Flask crifan 8124浏览 0评论

折腾:

[未解决]Flask中给app的输出到命令行中的debugger添加函数名

期间去添加额外的file的handler,使得log信息也可以输出到文件

且最好是可以rotate

超过对应的大小后,自动冲掉的。

flask log file

flask log output file

flask 输出日志 到文件

Flask logging example

掌握应用错误 — Flask 0.10 documentation

Flask入门系列(五)–错误处理及消息闪现 – 思诚之道

Python模块学习:logging 日志记录 – Python – 伯乐在线

python 日志模块 logging 详解 – leejun2005的个人页面 – 开源中国社区

%(name)s
Logger的名字
%(levelno)s
数字形式的日志级别
%(levelname)s
文本形式的日志级别
%(pathname)s
调用日志输出函数的模块的完整路径名,可能没有
%(filename)s
调用日志输出函数的模块的文件名
%(module)s
调用日志输出函数的模块名
%(funcName)s
调用日志输出函数的函数名
%(lineno)d
调用日志输出函数的语句所在的代码行
%(created)f
当前时间,用UNIX标准的表示时间的浮 点数表示
%(relativeCreated)d
输出日志信息时的,自Logger创建以 来的毫秒数
%(asctime)s
字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
%(thread)d
线程ID。可能没有
%(threadName)s
线程名。可能没有
%(process)d
进程ID。可能没有
%(message)s
用户输出的消息

记录应用错误 — Flask 0.10.1 文档

格式描述
%(levelname)s消息文本的记录等级 (‘DEBUG’‘INFO’‘WARNING’‘ERROR’‘CRITICAL’).
%(pathname)s发起日志记录调用的源文件的完整路径(如果可用)
%(filename)s路径中的文件名部分
%(module)s模块(文件名的名称部分)
%(funcName)s包含日志调用的函数名
%(lineno)d日志记录调用所在的源文件行的行号(如果可用)
%(asctime)sLogRecord 创建时的人类可读的时间。默认情况下,格式为 “2003-07-08 16:49:45,896” (逗号后的数字时间的毫秒部分)。这可以通过继承 :class:~logging.Formatter,并重载 formatTime() 改变。
%(message)s记录的消息,视为 msg % args

Application Errors — Flask Documentation (0.11)

“Sentry

-》有机会,可以去装一个,这样以后可以去Sentry的后台,管理错误了

-》类似于移动端的app的Bugly,崩溃收集的感觉?

FormatDescription
%(levelname)sText logging level for the message (‘DEBUG’‘INFO’‘WARNING’‘ERROR’‘CRITICAL’).
%(pathname)sFull pathname of the source file where the logging call was issued (if available).
%(filename)sFilename portion of pathname.
%(module)sModule (name portion of filename).
%(funcName)sName of function containing the logging call.
%(lineno)dSource line number where the logging call was issued (if available).
%(asctime)sHuman-readable time when the LogRecord` was created. By default this is of the form “2003-07-08 16:49:45,896” (the numbers after the comma are millisecond portion of the time). This can be changed by subclassing the formatter and overriding the formatTime() method.
%(message)sThe logged message, computed as msg % args
  • FileHandler – logs messages to a file on the filesystem.
  • RotatingFileHandler – logs messages to a file on the filesystem and will rotate after a certain number of messages.
  • NTEventLogHandler – will log to the system event log of a Windows system. If you are deploying on a Windows box, this is what you want to use.
  • SysLogHandler – sends logs to a UNIX syslog.

 python – Flask logging – Cannot get it to write to a file – Stack Overflow

Logging issue with Flask

Python Web Applications With Flask – Part III – Real Python

How to Handle Errors in Flask | Damyan’s Blog

flask-log 0.1.0 : Python Package Index

flask系统日志写到文件 | 一个爬虫工程师的技术博客

[总结]]

最后用代码:

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/sipevents/config.py

############################################################
# File Log
############################################################
LOG_FILE_FILENAME = “logs/sipevents.log”
# LOG_FILE_FORMAT = “[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s – %(message)s”
LOG_FILE_FORMAT = “[%(asctime)s %(levelname)s %(pathname)s/%(filename)s:%(lineno)d %(module)s %(funcName)s] %(message)s”

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/sipevents/sipevents/__init__.py

import logging
from logging.handlers import RotatingFileHandler
fileHandler = RotatingFileHandler(
    app.config[‘LOG_FILE_FILENAME’],
    maxBytes = 2*1024*1024,
    backupCount = 3,
    encoding = “UTF-8”)
fileHandler.setLevel(logging.DEBUG)
fileLogFormatterStr = app.config[“LOG_FILE_FORMAT”]
fileLogFormatter = logging.Formatter(fileLogFormatterStr)
fileHandler.setFormatter(fileLogFormatter)
app.logger.addHandler(fileHandler)

注:

此处的run.py,根本没改动:

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/sipevents/run.py

from sipevents import app
if __name__ == ‘__main__’:
    app.run(debug=True)

即可输出对应的log内容到文件里,且是我们自定义的格式了:

[2016-09-02 16:41:09,193 DEBUG /root/html/SIPEvents/sipevents/__init__.py/__init__.py:35 __init__ <module>] type(defaultLogger)=<class ‘logging.Logger’>
[2016-09-02 16:41:09,196 DEBUG /root/html/SIPEvents/sipevents/__init__.py/__init__.py:46 __init__ <module>] db=<SQLAlchemy engine=’sqlite:////usr/share/nginx/html/SIPEvents/instance/sipevents.db’>
…[2016-09-02 16:42:09,122 DEBUG /root/html/SIPEvents/sipevents/views.py/views.py:375 views before_request] g=<flask.g of ‘sipevents’>, g.user=None, current_user=<flask_login.AnonymousUserMixin object at 0x7f5b239d5590>
[2016-09-02 16:42:09,123 DEBUG /root/html/SIPEvents/sipevents/views.py/views.py:384 views sipevents] requestMethod=POST
[2016-09-02 16:42:09,124 DEBUG /root/html/SIPEvents/sipevents/views.py/views.py:387 views sipevents] requestData=<xml><ToUserName><![CDATA[gh_ac090a9873a8]]></ToUserName>
<FromUserName><![CDATA[oswjmv-QiQp-_5pFB0thKxfCZ8Tc]]></FromUserName>
<CreateTime>1472805729</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[CLICK]]></Event>
<EventKey><![CDATA[GET_EVENTS_TODAY]]></EventKey>
</xml>
[2016-09-02 16:42:09,124 DEBUG /root/html/SIPEvents/sipevents/views.py/views.py:394 views sipevents] requestArgs=ImmutableMultiDict([(‘nonce’, u’2144301626′), (‘timestamp’, u’1472805729′), (‘signature’, u’31d56fdca3d636bddf72d7446a24c646d7874cc2′), (‘openid’, u’oswjmv-QiQp-_5pFB0thKxfCZ8Tc’)])
[2016-09-02 16:42:09,125 DEBUG /root/html/SIPEvents/sipevents/views.py/views.py:402 views sipevents] signature=31d56fdca3d636bddf72d7446a24c646d7874cc2, timestamp=1472805729, nonce=2144301626, echostr=

-》此处,继续去调整,把配置改为:

LOG_FILE_FORMAT = “[%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(funcName)s] %(message)s”

效果是:

[2016-09-02 16:59:25,938 DEBUG views.py:804 creat_event] requestMethod=GET

对应的是:

%(asctime)s
2016-09-02 16:59:25,938
%(levelname)s
DEBUG
%(filename)s
views.py
%(lineno)d
804
%(funcName)s
creat_event
%(message)s
requestMethod=GET

而前面的:

%(pathname)s
/root/html/SIPEvents/sipevents/views.py
%(module)s
views

转载请注明:在路上 » [已解决]Flask中输出log日志到文件且自定义输出格式

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.199 seconds, using 22.15MB memory