Python日志(logging)模块使用方法简介
A logger is configured to have a log level. This log level describes the severity of the messages that the logger will handle. Python defines the following log levels:
§ DEBUG: Low level system information for debugging purposes
§ INFO: General system information
§ WARNING: Information describing a minor problem that has occurred.
§ ERROR: Information describing a major problem that has occurred.
§ CRITICAL: Information describing a critical problem that has occurred.
Each message that is written to the logger is a Log Record. Each log record also has a log level indicating the severity of that specific message. A log record can also contain useful metadata that describes the event that is being logged. This can include details such as a stack trace or an error code.
When a message is given to the logger, the log level of the message is compared to the log level of the logger. If the log level of the message meets or exceeds the log level of the logger itself, the message will undergo further processing. If it doesn’t, the message will be ignored.
Once a logger has determined that a message needs to be processed, it is passed to a Handler.
(来源:https://docs.djangoproject.com/en/1.4/topics/logging/ )
简单的范例
#-*- coding: utf-8 -*-
import logging
"""logging的简单使用示例
"""
if __name__ == '__main__':
# 最简单的用法,不推荐直接用logging模块
logging.debug(u'这个不会被打印出来')
logging.info(u'这个也不会被打印出来')
logging.warning(u'这个就会了,因为logging默认的级别是WARNING,低于这个等级的信息就会被忽略')
稍微难一点的范例
#-*- coding: utf-8 -*-
import logging
"""logging的使用示例
"""
if __name__ == '__main__':
# python官方文档中提供的一段示例,使用logging模块产生logger对象
FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'
logging.basicConfig(format = FORMAT)
d = { 'clientip' : '192.168.0.1', 'user' : 'fbloggs' }
# 创建一个日志对象
logger = logging.getLogger('tcpserver')
logger.warning('Protocol problem: %s', 'connection reset', extra = d)
# 设置级别
logger.setLevel(logging.DEBUG)
logger.debug('Hello', extra = d)
复杂一点的用法,为logger添加了若干个handler。
#-*- coding: utf-8 -*-
import logging, logging.config
"""logging的使用示例
"""
if __name__ == '__main__':
# 比较复杂的用法
LOGGING = {
# 版本,总是1
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'verbose': {'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'},
'simple': {'format': '%(levelname)s %(message)s'},
'default': {
'format' : '%(asctime)s %(message)s',
'datefmt' : '%Y-%m-%d %H:%M:%S'
}
},
'handlers': {
'null': {
'level':'DEBUG',
'class':'logging.NullHandler',
},
'console':{
'level':'DEBUG',
'class':'logging.StreamHandler',
'formatter': 'default'
},
'file': {
'level': 'INFO',
# TimedRotatingFileHandler会将日志按一定时间间隔写入文件中,并
# 将文件重命名为'原文件名+时间戮'这样的形式
# Python提供了其它的handler,参考logging.handlers
'class': 'logging.handlers.TimedRotatingFileHandler',
'formatter': 'default',
# 后面这些会以参数的形式传递给TimedRotatingFileHandler的
# 构造器
# filename所在的目录要确保存在
'filename' : 'log',
# 每5分钟刷新一下
'when' : 'M',
'interval' : 1,
'encoding' : 'utf8',
}
},
'loggers' : {
# 定义了一个logger
'mylogger' : {
'level' : 'DEBUG',
'handlers' : ['console', 'file'],
'propagate' : True
}
}
}
logging.config.dictConfig(LOGGING)
logger = logging.getLogger('mylogger')
logger.info('Hello')
另外,还可以通过配置文件来配置logging,方法如下:
配置文件(log_conf):
[loggers]
keys=root,mylogger
[handlers]
keys=null,console,file
[formatters]
keys=verbose,simple,default
[formatter_verbose]
format=%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s
datefmt=
class=logging.Formatter
[formatter_simple]
format=%(levelname)s %(message)s
datefmt=
class=logging.Formatter
[formatter_default]
format=%(asctime)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S
class=logging.Formatter
[logger_mylogger]
level=DEBUG
handlers=console,file
propagate=1
qualname=
[logger_root]
level=NOTSET
handlers=
[handler_null]
class=NullHandler
level=DEBUG
args=()
[handler_console]
class=StreamHandler
level=DEBUG
args=()
[handler_file]
class=handlers.TimedRotatingFileHandler
level=INFO
formatter=default
args=('log','M',1,0,'utf8')
Python代码:
#-*- coding: utf-8 -*-
import logging, logging.config
"""logging的使用示例
"""
if __name__ == '__main__':
# 使用配置文件
logging.config.fileConfig('log_conf')
logger = logging.getLogger('mylogger')
logger.info('Hello')
Python日志(logging)模块使用方法简介的更多相关文章
- python的logging模块使用方法
logging模块 logging模块是Python内置的日志模块,用来生成程序的日志.一条日志对应一个事件的发生,一个事件一般包括:事件发生时间.事件发生位置.事件内容.事件严重程度-日志级别.(还 ...
- Python日志(logging)模块,shelve,sys模块
菜鸟学python第十七天 1.logging 模块 logging模块即日志记录模块 用途:用来记录日志 为什么要记录日志: 为了日后复查,提取有用信息 如何记录文件 直接打开文件,往里写东西 直接 ...
- python的日志logging模块性能以及多进程
写在前面: 日志是记录操作的一种好方式.但是日志,基本都是基于文件的,也就是要写到磁盘上的.这时候,磁盘将会成为一个性能瓶颈.对于普通的服务器硬盘(机械磁盘,非固态硬盘),python日志的性能瓶颈是 ...
- python中日志logging模块的性能及多进程详解
python中日志logging模块的性能及多进程详解 使用Python来写后台任务时,时常需要使用输出日志来记录程序运行的状态,并在发生错误时将错误的详细信息保存下来,以别调试和分析.Python的 ...
- Python之logging模块
一.引言 之前在写一些小程序的时候想把日志内容打到文件中,所以就自己写了一个logger.py的程序,如下: #!/usr/bin/python # -*- coding=utf-8 -*- impo ...
- python的logging模块
python提供了一个日志处理的模块,那就是logging 导入logging模块使用以下命令: import logging logging模块的用法: 1.简单的将日志打印到屏幕上 import ...
- python的logging模块之读取yaml配置文件。
python的logging模块是用来记录应用程序的日志的.关于logging模块的介绍,我这里不赘述,请参见其他资料.这里主要讲讲如何来读取yaml配置文件进行定制化的日志输出. python要读取 ...
- python中logging模块的用法
很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...
- python基础--logging模块
很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...
- Python中logging模块的基本用法
在 PyCon 2018 上,Mario Corchero 介绍了在开发过程中如何更方便轻松地记录日志的流程. 整个演讲的内容包括: 为什么日志记录非常重要 日志记录的流程是怎样的 怎样来进行日志记录 ...
随机推荐
- 【细节题 离线 树状数组】luoguP4919 Marisa采蘑菇
歧义差评:但是和题意理解一样了之后细节依然处理了很久,说明还是水平不够…… 题目描述 Marisa来到了森林之中,看到了一排nn个五颜六色的蘑菇,编号从1-n1−n,这些蘑菇的颜色分别为col[1], ...
- 【状态压缩dp】1195: [HNOI2006]最短母串
一个清晰的思路就是状压dp:不过也有AC自动机+BFS的做法 Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T ...
- C语言中sizeof的用法
今天同学问我sizeof可不可以计算结构体的大小,我竟然忘了C语言还有sizeof这个函数,我是多久没有写程序了啊!!!惭愧,上研究生后写嵌入式方面的程序就特别少了,看来以后还要经常来练练手才行.现在 ...
- 【linux】【CPU】【x86】平台说明
节选自 <鸟哥的linux私房菜> http://cn.linux.vbird.org/linux_basic/0520rpm_and_srpm_1.php 操作硬件平台:这是个很好玩的地 ...
- 15.Yii2.0框架where单表查询
目录 新建控制器 HomeController.php 新建model article.php 新建控制器 HomeController.php D:\xampp\htdocs\yii\control ...
- debian安装之后使用android手机上网
安装debian的过程中,没有连接网线.因为路由器在客厅,电脑在卧室,拖条长长的线很不方便. 断网安装完成之后,通过usb连上i9250. 在i9250上,执行以下操作: “设置”--->“更多 ...
- [转载]ExtJs4 笔记(1) ExtJs大比拼JQuery:Dom文档操作
出处:[Lipan] (http://www.cnblogs.com/lipan/) 现在主流的JS框架要数ExtJs和JQuery应用的比较广泛.JQuery属于轻量级的,一般做网站应用比较常见,可 ...
- LRESULT CALLBACK WndProc 窗口程序的 重点
LRESULT CALLBACK WndProc Windows程序所作的一切,都是回应发送给窗口消息处理程序的消息.这是概念上的主要难点之一,在开始写作Windows程序之前,必须先搞清楚. 窗口消 ...
- Leetcode207--->课程表(逆拓扑排序)
题目: 课程表,有n个课程,[0, n-1]:在修一个课程前,有可能要修前导课程: 举例: 2, [[1,0]] 修课程1前需要先修课程0 There are a total of 2 courses ...
- selenium2.53用45以下的火狐别太高
selenium2.53用45以下的火狐别太高在高的火狐需要selenium3