logging.rst 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. =======
  2. Logging
  3. =======
  4. OpenLEADR uses the standard Python Logging facility. Following the `Python guidelines <https://docs.python.org/3/howto/logging.html#configuring-logging-for-a-library>`_, no default handlers are configured, but you can easily get going if you want to:
  5. Log to standard output
  6. ----------------------
  7. To print logs to your standard output, you can use the following convenience method:
  8. .. code-block:: python3
  9. import openleadr
  10. import logging
  11. openleadr.enable_default_logging(level=logging.INFO)
  12. Which is the same as:
  13. .. code-block:: python3
  14. import logging
  15. logger = logging.getLogger('openleadr')
  16. handler = logging.StreamHandler()
  17. handler.setLevel(logging.INFO)
  18. logger.addHandler(handler)
  19. Setting the logging level
  20. -------------------------
  21. You can set different logging levels for the logging generated by OpenLEADR:
  22. .. code-block:: python3
  23. import logging
  24. logger = logging.getLogger('openleadr')
  25. logger.setLevel(logging.WARNING)
  26. The different `logging levels <https://docs.python.org/3/library/logging.html#levels>`_ are:
  27. .. code-block:: python3
  28. logging.DEBUG
  29. logging.INFO
  30. logging.WARNING
  31. logging.ERROR
  32. logging.CRITICAL
  33. Redirecting logs to a different file
  34. ------------------------------------
  35. To write logs to a file:
  36. .. code-block:: python3
  37. import logging
  38. logger = logging.getLogger('openleadr')
  39. handler = logging.FileHandler('mylogfile.txt')
  40. logger.addHandler(logger)
  41. More info
  42. ---------
  43. Detailed info on logging configuration can be found on `the official Python documentation <https://docs.python.org/3/library/logging.html>`_.