折腾:
[未解决]Flask中给app的输出到命令行中的debugger添加函数名
期间去添加额外的file的handler,使得log信息也可以输出到文件
且最好是可以rotate
超过对应的大小后,自动冲掉的。
flask log file
flask log output file
flask 输出日志 到文件
掌握应用错误 — Flask 0.10 documentation
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 | 用户输出的消息 |
”
“
格式 | 描述 |
%(levelname)s | 消息文本的记录等级 (‘DEBUG’, ‘INFO’, ‘WARNING’, ‘ERROR’, ‘CRITICAL’). |
%(pathname)s | 发起日志记录调用的源文件的完整路径(如果可用) |
%(filename)s | 路径中的文件名部分 |
%(module)s | 模块(文件名的名称部分) |
%(funcName)s | 包含日志调用的函数名 |
%(lineno)d | 日志记录调用所在的源文件行的行号(如果可用) |
%(asctime)s | LogRecord 创建时的人类可读的时间。默认情况下,格式为 “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,崩溃收集的感觉?
Format | Description |
%(levelname)s | Text logging level for the message (‘DEBUG’, ‘INFO’, ‘WARNING’, ‘ERROR’, ‘CRITICAL’). |
%(pathname)s | Full pathname of the source file where the logging call was issued (if available). |
%(filename)s | Filename portion of pathname. |
%(module)s | Module (name portion of filename). |
%(funcName)s | Name of function containing the logging call. |
%(lineno)d | Source line number where the logging call was issued (if available). |
%(asctime)s | Human-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)s | The 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
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
[总结]]
最后用代码:
/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日志到文件且自定义输出格式