pytest笔记(8)——日志
pytest框架——日志:
导入库:import logging
指定日志级别:
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='[%Y-%m_%d %H:%M:%S]',
filename='./my.log',
filemode='a')
输出日志到控制台:
import logging
初始化Logger对象
logger = logging.getLogger(__name__)
logger.setLevel(level = logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console = logging.StreamHandler()
console.setFormatter(formatter)
logger.addHandler(console)
module_logger = logging.getLogger('commlog.sub')
module_logger.info("Start print log")
module_logger.debug("Do something")
module_logger.warning("Something maybe fail.")
module_logger.info("Finish")
公共日志模块:用的时候直接调用就可以
import logging
logger = logging.getLogger('commlog')
logger.setLevel(level=logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler = logging.FileHandler('log1.txt')
handler.setLevel(logging.INFO)
handler.setFormatter(formatter)
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
console.setFormatter(formatter)
logger.addHandler(handler)
logger.addHandler(console)
引用:
from test_log import comm_log
import logging
module_logger = logging.getLogger('commlog.sub')
module_logger.info('this is another module using logging')
module_logger.warning('另一个模块打印的警告')
配置文件实现公用化的日志:log.conf
[loggers]
keys=root
[logger_root]
level=DEBUG
handlers=hand01
[handlers]
keys=hand01
[handler_hand01]
class=StreamHandler
level=WARNING
formatter=form01
args=(sys.stderr,)
[formatters]
keys=form01,form02
[formatter_form01]
format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
datefmt=%a, %d %b %Y %H:%M:%S
[formatter_form02]
format=%(name)-12s: %(levelname)-8s %(message)s
datefmt=%a, %d %b %Y %H:%M:%S
配置文件的调用:
import logging
import logging.config
logging.config.fileConfig('配置文件名称')
loggers = logging.getLogger('root')
loggers.debug('这是debug日志')
既可以输出到控制台也可以输出到日志:log.conf
[loggers]
keys=root,main
[handlers]
keys=consoleHandler,fileHandler
[formatters]
keys=fmt
[logger_root]
level=DEBUG
handlers=consoleHandler
[logger_main]
level=DEBUG
qualname=main
handlers=fileHandler
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=fmt
args=(sys.stdout,)
[handler_fileHandler]
class=logging.handlers.RotatingFileHandler
level=DEBUG
formatter=fmt
args=('test.log','a',20000,5,)
[formatter_fmt]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
调用:
import logging
import logging.config
logging.config.fileConfig('配置文件名称')
loggers = logging.getLogger('root')
loggers_file = logging.getLogger('main')
=========================================日志模块小结==================================================
一、基本语法
初始化
基本配置:
导入库:import logging
指定日志级别:
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='[%Y-%m_%d %H:%M:%S]',
filename='./my.log',
filemode='a')
二、日志设置
脚本内嵌
公共模块日志文件
公共日志模块:用的时候直接调用就可以
import logging
logger = logging.getLogger('commlog')
logger.setLevel(level=logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler = logging.FileHandler('log1.txt')
handler.setLevel(logging.INFO)
handler.setFormatter(formatter)
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
console.setFormatter(formatter)
logger.addHandler(handler)
logger.addHandler(console)
引用:
from test_log import comm_log
import logging
module_logger = logging.getLogger('commlog.sub')
module_logger.info('this is another module using logging')
module_logger.warning('另一个模块打印的警告')
公共配置日志文件
既可以输出到控制台也可以输出到日志:log.conf
[loggers]
keys=root,main
[handlers]
keys=consoleHandler,fileHandler
[formatters]
keys=fmt
[logger_root]
level=DEBUG
handlers=consoleHandler
[logger_main]
level=DEBUG
qualname=main
handlers=fileHandler
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=fmt
args=(sys.stdout,)
[handler_fileHandler]
class=logging.handlers.RotatingFileHandler
level=DEBUG
formatter=fmt
args=('test.log','a',20000,5,)
[formatter_fmt]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
调用:
import logging
import logging.config
logging.config.fileConfig('配置文件名称')
loggers = logging.getLogger('root')
loggers_file = logging.getLogger('main')
三、调用方法
当前脚本包含日志对象及及方法
调用公共文件
调用公共配置
四、结合框架
==================================================结束====================================================
|