Python: Logging模块实例详解
来源:互联网 发布:win10虚拟网卡mac地址 编辑:程序博客网 时间:2024/06/04 19:50
Logging 模块
import logging
Quick Start
导入模块后直接logging.waring()
,logging.error()
简单粗暴地调用即可。默认的level
是DEBUG
,所以warning
会打印出信息,info
级别更低,不会输出信息。如果你不知道level
等参数的意义请后面解释,淡定,继续往下看。
如果不特别配置,logging
模块将日志打印到屏幕上(stdout)。
#!/usr/local/bin/python# -*- coding:utf-8 -*-import logginglogging.warning('Watch out!') # print message to consolelogging.info('I told you so') # will not print anything
Log
写入文件
更常见的情形是把信息记录在log
文件里。需要用logging.basicConfig()
设置文件名以及level
等参数,常见的level
见下表。
如果设置level
为INFO
,那么DEBUG
级别的信息就不会输出。常见的函数接口有debug()
, info()
, warning()
, error()
and critical()
,分别对应log
不同严重级别的信息。
注意把下面代码写入脚本(直接在terminal
里不会生成文件),比如test_log.py
。
import logginglogging.basicConfig(filename='example.log',level=logging.DEBUG,filemode='w')# filemode = 'w' 每次运行,重写loglogging.debug('This message should go to the log file')logging.info('So should this')logging.warning('And this, too')
cat example.log
DEBUG:root:This message should go to the log fileINFO:root:So should thisWARNING:root:And this, too
改变Log
输出格式
通过format
参数,可以定制写入log
文件的格式。
import logginglogging.basicConfig(format='%(levelname)s:%(message)s',level=logging.DEBUG)logging.debug('This message should appear on the console')logging.info('So should this')logging.warning('And this, too')
DEBUG:This message should appear on the console
INFO:So should this
WARNING:And this, too
记录时间
通过datafmt
参数,可以格式化输出log
的时间。
import logginglogging.basicConfig(format='%(asctime)s %(message)s',datefmt='%m/%d/%Y %I:%M:%S %p')logging.warning('is when this event was logged.')
07/16/2016 12:10:35 AM is when this event was logged.
更丰富的Log
控制
上面的代码大部分是利用默认配置,其实我们自定义更多。比如把输出到terminal
和log.txt
文件里。
首先理解几个概念是有用的。
- Logger
记录器,暴露了应用程序代码能直接使用的接口。
- Handler
处理器,将(记录器产生的)日志记录发送至合适的目的地。
- Filter
过滤器,提供了更好的粒度控制,它可以决定输出哪些日志记录。
- Formatter
格式化器,指明了最终输出中日志记录的布局。
首先,创建一个logger
,记录器,然后给其添加不同的handler
,输出到不同的渠道,比如下面这个例子就会生成log.txt
文件,并同时输出在terminal
里。
import logging# create logger with name# if not specified, it will be rootlogger = logging.getLogger('my_logger')logger.setLevel(logging.DEBUG)# create a handler, write to log.txt# logging.FileHandler(self, filename, mode='a', encoding=None, delay=0)# A handler class which writes formatted logging records to disk files.fh = logging.FileHandler('log.txt')fh.setLevel(logging.DEBUG)# create another handler, for stdout in terminal# A handler class which writes logging records to a streamsh = logging.StreamHandler()sh.setLevel(logging.DEBUG)# set formatterformatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')fh.setFormatter(formatter)sh.setFormatter(formatter)# add handler to loggerlogger.addHandler(fh)logger.addHandler(sh)# log itlogger.debug('Debug')logger.info('Info')
2016-07-18 21:43:14,648 - my_logger - DEBUG - Debug2016-07-18 21:43:14,650 - my_logger - INFO - Info
Ref:
- 官方文档
- Python Module of the Week
- Good logging practice in python
- Python: Logging模块实例详解
- python logging模块实例
- Python logging模块详解
- Python logging模块详解
- Python logging模块详解
- Python logging模块详解
- Python logging模块详解
- Python logging模块详解
- Python logging模块详解
- Python logging模块详解
- Python logging模块详解
- Python logging模块详解
- Python logging模块详解
- Python logging模块详解
- Python logging模块详解
- Python logging模块详解
- python logging模块详解
- Python logging 模块详解
- 使用Bootstrap tab页切换的使用
- 设计四大基本原则——对比、重复、对齐、亲密度
- 产品营销难点:因为你只考虑推广而没考虑用户
- SVN禁止删除和强制注释操作的钩子脚本 (Linux)
- git使用记录
- Python: Logging模块实例详解
- Windows 10
- 解决net start mysql 提示:服务名无效 请键入NET HELPING 2185以获得更多的帮助的问题
- 快速排序 (Java)
- 字符串排序
- Linux里设置环境变量的方法(export PATH)
- Python——pymssql安装
- VR一体机上场景中闪烁阴影问题
- Codevs 2611 观光旅游(floyed最小环)