[关闭]
@hainingwyx 2018-08-30T18:51:16.000000Z 字数 687 阅读 1048

logging 模块

Python


  1. import logging
  2. import threading
  3. def get_logger():
  4. logger = logging.getLogger("threading_example")
  5. logger.setLevel(logging.DEBUG)
  6. fh = logging.FileHandler("threading.log")
  7. fmt = '%(asctime)s - %(threadName)s - %(levelname)s - %(message)s'
  8. formatter = logging.Formatter(fmt)
  9. fh.setFormatter(formatter)
  10. logger.addHandler(fh)
  11. return logger
  12. def doubler(number, logger):
  13. """
  14. 可以被线程使用的一个函数
  15. """
  16. logger.debug('doubler function executing')
  17. result = number * 2
  18. logger.debug('doubler function ended with: {}'.format(
  19. result))
  20. if __name__ == '__main__':
  21. logger = get_logger()
  22. thread_names = ['Mike', 'George', 'Wanda', 'Dingbat', 'Nina']
  23. for i in range(5):
  24. my_thread = threading.Thread(
  25. target=doubler, name=thread_names[i], args=(i,logger))
  26. my_thread.start()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注