logging模块2

logging模块 --logging模块提供通用的日志系统

日志级别

日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET)
WARNING是默认级别

基本用法

logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critial message')
输出:
WARNING:root:warning message
ERROR:root:error message
CRITICAL:root:critial message

可以使用logging.basicConfig()修改logging模块的默认行为

filename:用指定的文件名创建FiledHandler(后边会具体讲解handler的概念),这样日志会被存储在指定的文件中。
filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”。
format:指定handler使用的日志显示格式。
datefmt:指定日期时间格式。
level:设置rootlogger(后边会讲解具体概念)的日志级别
stream:用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件(f=open('test.log','w')),默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。

format参数中可能用到的格式化串:

%(name)s Logger的名字  
%(levelno)s 数字形式的日志级别  
%(levelname)s 文本形式的日志级别  
%(pathname)s 调用日志输出函数的模块的完整路径名,可能没有  
%(filename)s 调用日志输出函数的模块的文件名  
%(module)s 调用日志输出函数的模块名  
%(funcName)s 调用日志输出函数的函数名  
%(lineno)d 调用日志输出函数的语句所在的代码行  
%(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示  
%(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数  
%(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒 
%(thread)d 线程ID。可能没有  
%(threadName)s 线程名。可能没有  
%(process)d 进程ID。可能没有  
%(message)s用户输出的消息  

例子:

#!/usr/bin/python3
# -*- config:utf-8 -*-
import logging

logging.basicConfig(
    level = logging.DEBUG,    #设置输出的log级别,默认warning
    filename = ('log.log'),   #设置日志输出文件,若不设置则默认输出到终端
    filemode = 'w',           #指定打开文件的模式,如果指定了文件名(如果未指定filemode,它的默认值是' a ')
    format = "%(asctime)s %(levelname)s [%(lineno)d] %(message)s"          
)

logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')

结果:
[root@localhost test]# cat log.log
2020-05-25 01:24:20,321 DEBUG [12] debug message
2020-05-25 01:24:20,321 INFO [13] info message
2020-05-25 01:24:20,322 WARNING [14] warning message
2020-05-25 01:24:20,322 ERROR [15] error message
2020-05-25 01:24:20,323 CRITICAL [16] critical message

进阶用法

例:

#!/usr/bin/python3
# -*- config:utf-8 -*-

'''
将日志同时输出到控制台和文件中
'''
import logging

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)    #设置全局日志级别

ch = logging.StreamHandler()      #设置默认Handler
fh = logging.FileHandler('log2.log', mode='w', encoding='utf-8')  #设置文件Handler

ch.setLevel(logging.INFO)        #设置控制台日志级别
fh.setLevel(logging.DEBUG)       #设置文件日志级别

cmt = logging.Formatter("%(asctime)s %(levelname)s %(message)s")        #设置控制台日志输出格式
fmt = logging.Formatter("%(asctime)s %(levelname)s [%(filename)s %(lineno)d] %(message)s")    #设置文件日志输出格式

ch.setFormatter(cmt)    #将日志格式添加至Handler
fh.setFormatter(fmt)

logger.addHandler(ch)    #讲Handler添加到logger
logger.addHandler(fh)

#测试
logger.debug('debug message')
logger.info('info message')
logger.warning('warning message')
logger.error('error message')
logger.critical('critical message')

结果:
控制台:
2020-05-26 06:39:48,535 INFO info message
2020-05-26 06:39:48,535 WARNING warning message
2020-05-26 06:39:48,535 ERROR error message
2020-05-26 06:39:48,535 CRITICAL critical message


文件:
[root@localhost test]# cat log2.log
2020-05-26 06:39:48,534 DEBUG [log2.py 28] debug message
2020-05-26 06:39:48,535 INFO [log2.py 29] info message
2020-05-26 06:39:48,535 WARNING [log2.py 30] warning message
2020-05-26 06:39:48,535 ERROR [log2.py 31] error message
2020-05-26 06:39:48,535 CRITICAL [log2.py 32] critical message

[参考](https://www.cnblogs.com/ranxf/p/7794240.html)

posted @ 2020-05-25 23:07  静心&得意  阅读(135)  评论(0编辑  收藏  举报