diff --git a/doc/apidoc/index.rst b/doc/apidoc/index.rst index 572c5eb8..4b1aa224 100644 --- a/doc/apidoc/index.rst +++ b/doc/apidoc/index.rst @@ -16,6 +16,7 @@ Contents: qubes-events qubes-plugins qubes-ext + qubes-log Indices and tables ================== diff --git a/doc/apidoc/qubes-log.rst b/doc/apidoc/qubes-log.rst new file mode 100644 index 00000000..f6fa3e7e --- /dev/null +++ b/doc/apidoc/qubes-log.rst @@ -0,0 +1,8 @@ +:py:mod:`qubes.log` Logging routines +==================================== + +.. automodule:: qubes.log + :members: + :show-inheritance: + +.. vim: ts=3 sw=3 et diff --git a/doc/apidoc/qubes.rst b/doc/apidoc/qubes.rst index d886ddab..e2ae68b3 100644 --- a/doc/apidoc/qubes.rst +++ b/doc/apidoc/qubes.rst @@ -2,5 +2,7 @@ =============================== .. automodule:: qubes + :members: + :show-inheritance: .. vim: ts=3 sw=3 et diff --git a/qubes/log.py b/qubes/log.py new file mode 100644 index 00000000..1e43f748 --- /dev/null +++ b/qubes/log.py @@ -0,0 +1,70 @@ +#!/usr/bin/python2 -O +# -*- coding: utf-8 -*- + +'''Qubes logging routines + +See also: :py:attr:`qubes.vm.qubesvm.QubesVM.logger` +''' + +import logging +import os +import sys + +FORMAT_CONSOLE = '%(message)s' +FORMAT_LOG = '%(asctime)s %(message)s' +FORMAT_DEBUG = '%(asctime)s [%(processName)s %(module)s.%(funcName)s:%(lineno)d] %(name)s: %(message)s' +LOGPATH = '/var/log/qubes' +LOGFILE = os.path.join(LOGPATH, 'qubes.log') + +formatter_console = logging.Formatter(FORMAT_CONSOLE) +formatter_log = logging.Formatter(FORMAT_LOG) +formatter_debug = logging.Formatter(FORMAT_DEBUG) + +def enable(): + '''Enable global logging + + Use :py:mod:`logging` module from standard library to log messages. + + >>> import qubes.log + >>> qubes.log.enable() # doctest: +SKIP + >>> import logging + >>> logging.warning('Foobar') # doctest: +SKIP + ''' + if logging.root.handlers: + return + + handler_console = logging.StreamHandler(sys.stderr) + handler_console.setFormatter(formatter_console) + logging.root.addHandler(handler_console) + + handler_log = logging.FileHandler('log', 'a', encoding='utf-8') + handler_log.setFormatter(formatter_log) + logging.root.addHandler(handler_log) + + logging.root.setLevel(logging.INFO) + +def enable_debug(): + '''Enable debug logging + + Enable more messages and additional info to message format. + ''' + + enable() + logging.root.setLevel(logging.DEBUG) + + for handler in logging.root.handlers: + handler.setFormatter(formatter_debug) + +def get_vm_logger(vmname): + '''Initialise logging for particular VM name + + :param str vmname: VM's name + :rtype: :py:class:`logging.Logger` + ''' + + logger = logging.getLogger('vm.' + vmname) + handler = logging.FileHandler(os.path.join(LOGPATH, 'vm', vmname + '.log')) + handler.setFormatter(formatter_log) + logger.addHandler(handler) + + return logger