日志处理之logging模块

本文详细介绍了Python中logging模块的使用方法,包括日志级别的设置、日志输出到文件及屏幕的技术实现,并提供了如何定制日志格式的具体示例。
1 日志级别:
2    'CRITICAL': CRITICAL,
3     'ERROR': ERROR,
4     'WARN': WARNING,
5     'WARNING': WARNING,
6     'INFO': INFO,
7     'DEBUG': DEBUG,
8     'NOTSET': NOTSET,
import logging
logging.warning("user [wy] attempted wrong password more than 3 times")
logging.critical("server is down")
输出:
WARNING:root:user [wy] attempted wrong password more than 3 times
CRITICAL:root:server is down

如何把日志写入到文件中?
import logging
logging.basicConfig(filename="example.log",level=logging.INFO) #filename后面是日志文件名,level为INFO级别,日志文件会记录INFO级别以上的日志
logging.warning("user [wy] attempted wrong password more than 3 times")
logging.info("show this")
logging.debug("this is erro")
logging.critical("server is down")
生成一个新的文件example.log
example.log记录:
WARNING:root:user [wy] attempted wrong password more than 3 times
INFO:root:show this
CRITICAL:root:server is down

日志级别:debug<info<warning<critical

 

 1 怎么在日志中加入时间?
 2 import logging
 3 logging.basicConfig(filename="example.log",level=logging.INFO,format="%(asctime)s %(message)s",datefmt="%m/%d/%Y %I:%M:%S %p") #注意大小写
 4 logging.warning("user [wy] attempted wrong password more than 3 times")
 5 logging.info("show this")
 6 logging.debug("this is erro")
 7 logging.critical("server is down")
 8 
 9 example.log显示:
10 10/20/2016 08:49:32 PM user [wy] attempted wrong password more than 3 times
11 10/20/2016 08:49:32 PM show this
12 10/20/2016 08:49:32 PM server is down

 

1 实际生产中,我们要把日志打印到文件中,也要打印到屏幕上。
2 在这里我们需要了解日志模块的一些复杂的知识了,这里有4个功能:Loggers,Handlers,Filters,Formatters
3 1 Loggers expose the interface that application code directly   #直接调用
4 2 Handlers send the log records (created by loggers) to the appropriate destination #把日志发送到不同的地方(屏幕,本地文件等)
5 3 Filters provide a finer grained facility for determing which log records to output #日志过滤,
6 4 Formatters specify the layout of log records in the final output #字符串格式化

要求:我们要把INFO级别以上的日志打印到屏幕上,把warning级别以上的日志打印到文件中。

import logging
logger = logging.getLogger("TEST-LOG") #获取日志对象
logger.setLevel(logging.DEBUGE) #设置全局日志级别

ch = logging.StreamHandler() #把日志打印到屏幕
ch.setLevel(logging.INFO) #打印到屏幕的日志级别

fh = logging.FileHandler("access.log") #输出到access.log文件中
fh.setLevel(logging.WARNING) #输出到文件中的日志级别

formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") #格式化输出日志
ch.setFormatter(formatter) #打印到屏幕上的日志格式
fh.setFormatter(formatter)#打印到文件中的日志

logger.addHandler(ch) #告诉logger输出日志到具体的handler
logger.addHandler(fh)#告诉logger输出日志到具体的handler  我们可以给打印到屏幕上的日志和打印到文件中的日志定义不同的格式

logger.debug("debug message")
logger.info("info message")
logger.error("error message")
logger.warning("warning message")
logger.critical("critical message")

注意:当全局日志级别高于我们自定义到日志文件中和屏幕上的日志级别时,会按照全局日志级别来打印日志到文件中和屏幕上
当全局日志级别低于我们自定义到日志文件中和屏幕上的日志级别时,会按照我们自定义的日志级别来打印

 

转载于:https://www.cnblogs.com/RomanticYori/p/5982463.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值