Guest User

https://stackoverflow.com/questions/28208949/log-stack-trace

a guest
Apr 10th, 2015
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import logging
  3. import logging.config
  4. import traceback
  5. import warnings
  6.  
  7. class Formatter(logging.Formatter):
  8.     def format(self, record):
  9.         record.stack_info = ''.join(traceback.format_stack())
  10.         return super().format(record)
  11.  
  12. def configure_logging():
  13.     warning_logger_name = 'py.warnings'
  14.    
  15.     logging.captureWarnings(True)
  16.    
  17.     logging.config.dictConfig({
  18.         'formatters': {
  19.             'f': {
  20.                 '()': '__main__.Formatter',
  21.                 'format': '%(name)s\n'
  22.                           '%(pathname)s\n'
  23.                           '%(lineno)d\n'
  24.                           '%(message)s',
  25.             },
  26.         },
  27.         'handlers': {
  28.             'h': {
  29.                 'level':'WARN',
  30.                 'class':'logging.StreamHandler',
  31.                 'formatter':'f',
  32.             },
  33.         },
  34.         'loggers': {
  35.             warning_logger_name: {
  36.                 'handlers': ['h'],
  37.             },
  38.         },
  39.         'version': 1,
  40.     })
  41.  
  42. def main():
  43.     f1()
  44.  
  45. def f1():
  46.     f2()
  47.  
  48. def f2():
  49.     f3()
  50.  
  51. def f3():
  52.     warnings.warn('f3() warning')
  53.  
  54. if __name__ == '__main__':
  55.     configure_logging()
  56.     main()
Add Comment
Please, Sign In to add comment