Python Logging Module
2017-11-20 14:18
369 查看
Main stuff
logger, handler, formatter.How it works
ref:
How Python logging module works | Shut Up and Ship
http://zqpythonic.qiniucdn.com/data/20161124153540/index.html
Simple example
import logging # create logger lgr = logging.getLogger('myapp') lgr.setLevel(logging.DEBUG) # add a file handler fh = logging.FileHandler('myapp.log') fh.setLevel(logging.WARNING) # create a formatter and set the formatter for the handler. frmt = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(frmt) # add the Handler to the logger lgr.addHandler(fh) # You can now start issuing logging statements in your code lgr.debug('debug message') # This won't print to myapp.log lgr.info('info message') # Neither will this. lgr.warning('Checkout this warning.') # This will show up in the log file. lgr.error('An error goes here.') # and so will this. lgr.critical('Something critical happened.') # and this one too.
Levels
CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSETref:
How Python logging module works | Shut Up and Shiphttp://zqpythonic.qiniucdn.com/data/20161124153540/index.html
More to understand the work flow
formatter
Currently, the useful attributes in a LogRecord are described by:attributes | discription |
---|---|
%(name)s | Name of the logger (logging channel) |
%(levelno)s | Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL) |
%(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) |
%(lineno)d | Source line number where the logging call was issued (if available) |
%(funcName)s | Function name |
%(created)f | Time when the LogRecord was created (time.time() return value) |
%(asctime)s | Textual time when the LogRecord was created |
%(msecs)d | Millisecond portion of the creation time |
%(relativeCreated)d | Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded (typically at application startup time) |
%(thread)d | Thread ID (if available) |
%(threadName)s | Thread name (if available) |
%(process)d | Process ID (if available) |
%(message)s | The result of record.getMessage(), computed just as the record is emitted |
cpython: 5c4ca109af1c Lib/logging/__init__.py
https://hg.python.org/cpython/file/5c4ca109af1c/Lib/logging/__init__.py#l399
logging.conf
python 的日志logging模块学习 - dkcndk - 博客园https://www.cnblogs.com/dkblog/archive/2011/08/26/2155018.html
16.7. logging.config — Logging configuration — Python 3.6.3 documentation
https://docs.python.org/3/library/logging.config.html
reference
Python Standard Logging in Sugar - OLPChttp://wiki.laptop.org/go/Python_Standard_Logging_in_Sugar
How Python logging module works | Shut Up and Ship
http://zqpythonic.qiniucdn.com/data/20161124153540/index.html
Python 学习入门(14)—— logging - CSDN博客
http://blog.csdn.net/ithomer/article/details/16985379
相关文章推荐
- python logging之multi-module
- Simple usage of Python's logging module
- Python内置模块logging用法实例分析
- python学习之--windows安装mysql-python module
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- Python中如何解决ImportError:No Module Named arcpy问题
- Python学习笔记 --- ImportError: No module named sqlalchemy
- python module
- Python的Module和Package
- 构造Pythonic C module(续1)
- python logging模块
- Python中logging模块的用法实例
- pyhton 安装pip 以及 numpy (解决python:ModuleNotFoundError:No module named "numpy" 等类似缺包问题
- python 的日志logging模块学习
- python日志输出—logging直接代码配置
- No module named win32api-for python3
- Python模块之logging
- Python Imaging Library: ImageEnhance Module(图像增强模块)
- python logging 日志模块的配置和使用
- 分享一个python下func module的使用脚本