学习网站:https://www.cnblogs.com/yuanchenqi/articles/5732581.html

 

logging 模块:

# _author: lily
# _date: 2019/1/14
import logging

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

# logging.basicConfig(level=logging.DEBUG,
#                     format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
#                     datefmt='%a,%d %b %Y %H:%M:%S',
#                     filename='test.log',
#                     filemode='w')
#
# #Mon,14 Jan 2019 23:23:11 learn_of_logging_module.py[line:17] DEBUG debug message
# logging.debug('debug message')
# logging.info('info message')
# logging.error('error message')
# logging.warning('warning message')
# logging.critical('critical message')

logger = logging.getLogger()
# 创建一个handler,用于写入日志文件
fh = logging.FileHandler('test.log')   # 文件对象
# 在创建一个handler,用于输出到控制台
ch = logging.StreamHandler()  # 屏幕对象

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

fh.setFormatter(formatter)
ch.setFormatter(formatter)

logger.addHandler(fh)
logger.addHandler(ch)

logger.setLevel(logging.DEBUG)

logger.debug('debug message')
logger.info('info message')
logger.warning('warning message')
logger.error('error message')
logger.critical('critical message')
View Code

相关文章:

  • 2021-08-14
  • 2021-12-18
  • 2021-06-29
  • 2021-07-29
  • 2021-06-28
  • 2022-12-23
  • 2022-01-27
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
  • 2021-08-19
  • 2021-07-10
  • 2021-07-21
  • 2022-01-09
相关资源
相似解决方案