log.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU Lesser General Public License as published by
  9. # the Free Software Foundation; either version 2.1 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. '''Qubes logging routines
  22. See also: :py:attr:`qubes.vm.qubesvm.QubesVM.log`
  23. '''
  24. import logging
  25. import sys
  26. FORMAT_CONSOLE = '%(name)s: %(message)s'
  27. FORMAT_LOG = '%(asctime)s %(message)s'
  28. FORMAT_DEBUG = '%(asctime)s ' \
  29. '[%(processName)s %(module)s.%(funcName)s:%(lineno)d] %(name)s: %(message)s'
  30. formatter_console = logging.Formatter(FORMAT_CONSOLE)
  31. formatter_log = logging.Formatter(FORMAT_LOG)
  32. formatter_debug = logging.Formatter(FORMAT_DEBUG)
  33. def enable():
  34. '''Enable global logging
  35. Use :py:mod:`logging` module from standard library to log messages.
  36. >>> import qubes.log
  37. >>> qubes.log.enable() # doctest: +SKIP
  38. >>> import logging
  39. >>> logging.warning('Foobar') # doctest: +SKIP
  40. '''
  41. if logging.root.handlers:
  42. return
  43. handler_console = logging.StreamHandler(sys.stderr)
  44. handler_console.setFormatter(formatter_console)
  45. logging.root.addHandler(handler_console)
  46. logging.root.setLevel(logging.INFO)
  47. def enable_debug():
  48. '''Enable debug logging
  49. Enable more messages and additional info to message format.
  50. '''
  51. enable()
  52. logging.root.setLevel(logging.DEBUG)
  53. for handler in logging.root.handlers:
  54. handler.setFormatter(formatter_debug)