spacepaste

  1.  
  2. import logging
  3. import warnings
  4. def simple_format_warning(message, category, *args, **kwargs):
  5. return '%s: %s' % (category.__name__, str(message))
  6. warnings.formatwarning = simple_format_warning
  7. logging.basicConfig(
  8. filename="temp.log",
  9. format=(
  10. "%(asctime)s:%(msecs)-0.3f - %(levelname)s"
  11. " - %(name)s - %(module)s:%(lineno)d"
  12. " :: %(message)s"
  13. ),
  14. datefmt="%Y-%m-%d_%H:%M:%S",
  15. )
  16. logger = logging.getLogger(__name__)
  17. logger.propagate = False
  18. formatter = logging.Formatter(
  19. fmt=(
  20. "%(asctime)s:%(msecs)-0.3f - %(levelname)s"
  21. " - %(name)s - %(module)s:%(lineno)d"
  22. " :: %(message)s"
  23. ),
  24. datefmt="%Y-%m-%d_%H:%M:%S",
  25. style="%"
  26. )
  27. handler = logging.FileHandler(filename="temp.log")
  28. handler.setFormatter(formatter)
  29. logger.addHandler(handler)
  30. logging.captureWarnings(True)
  31. warnings.warn("Warning message !!!")
  32. logger.warning("Logging message !!!")
  33. """
  34. In 'temp.log', I get these two lines :
  35. 2018-08-10_19:47:46:107.400 - WARNING - py.warnings - warnings:99 :: UserWarning: Warning message !!!
  36. 2018-08-10_19:47:46:108.400 - WARNING - __main__ - bug_capture_warning:43 :: Logging message !!!
  37. If we look at the first line of log, we see 'warnings:99'
  38. but in my opinion it should be 'temp_code:42'
  39. """
  40.