python之logging模块
Posted 贺言
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python之logging模块相关的知识,希望对你有一定的参考价值。
1.import logging
logging.basicConfig(
level=logging.DEBUG,
filename=‘logging.log‘,
filecode=‘w‘,
format=‘%(asctime)s %(filename)s [%(lineno)d] [%(message)s]‘
)
logging.debug(‘hello world‘)
logging.error(‘niuqichongtian‘)
logging.warning(‘You gotta do‘)
-----------------------------------------------------------------------------------------------------------------------------
2020-02-14 16:36:46,151 s1.py [311] [hello world]
2020-02-14 16:36:46,151 s1.py [312] [niuqichongtian]
2020-02-14 16:36:46,152 s1.py [313] [You gotta do]
2.import logging
def logger():
logger=logging.getLogger()
f=logging.FileHandler(‘test_1.log‘)
c=logging.StreamHandler()
fm=logging.Formatter(‘%(asctime)s %(message)s‘)
f.setFormatter(fm)
c.setFormatter(fm)
logger.addHandler(f)
logger.addHandler(c)
logger.setLevel(‘DEBUG‘)
return logger
logger=logger()
logger.debug(‘debug‘)
logger.error(‘error‘)
logger.critical(‘critical‘)
logger.warning(‘warning‘)
logger.info(‘info‘)
以上是关于python之logging模块的主要内容,如果未能解决你的问题,请参考以下文章