关于log打印logging

import logging

logging.basicConfig(
    #level中等级从高到低为critical> error> warning> info> debug> notset,
    #设置为notset/debug则所有等级都可以打印,如果设置为warning则warning含以上可以打印,以下等级不打印.
    level=logging.DEBUG,
    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
    #以下日期格式%a为星期几,小写a为英文简写,A为全称
    datefmt  = '%a %Y-%m-%d %H:%M:%S',
    #datefmt="%a, %d %b %Y %H:%M:%S",
    #设置文件路径及文件名
    filename='D:\\practice\\test.log',
    #a为追加,w为覆盖
    filemode='a'
)

logging.debug("this is debug message")
logging.info("this is debug message")
logging.warning("this is debug message")

#以上输出结果为:
Mon 2019-10-14 12:10:42 Log.py[line:13] - DEBUG: this is debug message
Mon 2019-10-14 12:10:42 Log.py[line:14] - INFO: this is info message
Mon 2019-10-14 12:10:42 Log.py[line:15] - WARNING: this is warning message
#第二种日期格式如下
#Mon, 14 Oct 2019 11:50:46 Log.py[line:12] DEBUG this is debug message

对于日期格式的详细介绍可以参考:
https://blog.csdn.net/z_johnny/article/details/50812878?utm_source=blogxgwz8

python中时间日期格式化符号:
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身

关于删除日志的方法参考:
https://blog.csdn.net/energysober/article/details/53263295

删除日志文件设置:
log_file_handler.suffix = “%Y-%m-%d_%H-%M.log”
log_file_handler.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}.log$")
suffix和extMatch的格式要相对应,Year-4位,m-2位 以此类推

配置logger.conf文件

#logger.conf
############################
[loggers]
keys=root, result, infomation

[logger_root]
level=DEBUG
handlers=infohander, resulthander

[logger_result]
level=CRITICAL
handlers=resulthander, consolehander
qualname=result
propagate=0

[logger_information]
level=DEBUG
handlers=infohander, consolehander
qualname=information
propagate=0
#############################
[handlers]
keys=resulthander, infohander, consolehander

[handler_resulthander]
class=FileHandler
formatter=form01
args=('D:\\practice\\testresult.log', 'w')

[handler_infohander]
class=FileHandler
formatter=form02
args=('D:/practice/testinfo.log', 'w')

[handler_consolehander]
class=StreamHandler
formatter=form02
args=(sys.stderr,)
##############################
[formatters]
keys=form01,form02

[formatter_form01]
format=%(message)s::%(filename)s
datefmt=%Y-%m-%d %H:%M:%S

[formatter_form02]
format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S

调用logger.conf

import logging.config

#导入配置文件logger.conf
logging.config.fileConfig("C:\\test_for_practice\\logger.conf")
#以下result和information说是对应qualname,但是[logger_information]名字不对应也会报错
resulter = logging.getLogger("result")
infor = logging.getLogger("information")

resulter.critical('PASS')
resulter.critical("FAIL")

infor.debug("this is debug message")
infor.info("this is info message")
infor.warning("this is warning message")

def debug(message):
	infor.debug(message)

输出结果:
2019-10-14 17:50:17 testlogger.py[line:8] CRITICAL PASS
2019-10-14 17:50:17 testlogger.py[line:9] CRITICAL FAIL
2019-10-14 17:50:17 testlogger.py[line:11] DEBUG this is debug message
2019-10-14 17:50:17 testlogger.py[line:12] INFO this is info message
2019-10-14 17:50:17 testlogger.py[line:13] WARNING this is warning message
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值