testlog3.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # -*- coding: utf-8 -*-
  2. import logging
  3. logging.debug('debug级别,一般用来打印一些调试信息,级别最低')
  4. logging.info('info级别,一般用来打印一些正常的操作信息')
  5. logging.warning('waring级别,一般用来打印警告信息')
  6. logging.error('error级别,一般用来打印一些错误信息')
  7. logging.critical('critical级别,一般用来打印一些致命的错误信息,等级最高')
  8. logging.basicConfig(level=logging.DEBUG)
  9. logging.basicConfig(format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s',
  10. level=logging.DEBUG)
  11. logging.basicConfig(format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s',
  12. level=logging.DEBUG,
  13. filename='test.log',
  14. filemode='a')
  15. logger = logging.getLogger('test')
  16. logger.debug('debug级别,一般用来打印一些调试信息,级别最低')
  17. logger.info('info级别,一般用来打印一些正常的操作信息')
  18. logger.warning('waring级别,一般用来打印警告信息')
  19. logger.error('error级别,一般用来打印一些错误信息')
  20. logger.critical('critical级别,一般用来打印一些致命的错误信息,等级最高')
  21. logger = logging.getLogger('test')
  22. stream_handler = logging.StreamHandler()
  23. logger.addHandler(stream_handler)
  24. logger = logging.getLogger('test')
  25. logger.setLevel(level=logging.DEBUG)
  26. formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
  27. stream_handler = logging.StreamHandler()
  28. stream_handler.setLevel(logging.DEBUG)
  29. stream_handler.setFormatter(formatter)
  30. logger.addHandler(stream_handler)