Python outputs log logging to both console and file

From , 3 Years ago, written in Python, viewed 232 times.
URL https://pastebin.vip/view/7070f908
  1. #-*- coding:utf-8 -*-
  2. import logging
  3.  
  4. # 配置日志信息
  5. logging.basicConfig(level=logging.DEBUG,
  6.                     format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
  7.                     datefmt='%m-%d %H:%M',
  8.                     filename='myapp.log',
  9.                     filemode='w')
  10. # 定义一个Handler打印INFO及以上级别的日志到sys.stderr
  11. console = logging.StreamHandler()
  12. console.setLevel(logging.INFO)
  13. # 设置日志打印格式
  14. formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
  15. console.setFormatter(formatter)
  16. # 将定义好的console日志handler添加到root logger
  17. logging.getLogger('').addHandler(console)
  18.  
  19. logging.info('Jackdaws love my big sphinx of quartz.')
  20.  
  21. logger1 = logging.getLogger('myapp.area1')
  22. logger2 = logging.getLogger('myapp.area2')
  23.  
  24. logger1.debug('Quick zephyrs blow, vexing daft Jim.')
  25. logger1.info('How quickly daft jumping zebras vex.')
  26. logger2.warning('Jail zesty vixen who grabbed pay from quack.')
  27. logger2.error('The five boxing wizards jump quickly.')
  28. #//python/5820

Reply to "Python outputs log logging to both console and file"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website