diff options
author | SVN-Git Migration <python-modules-team@lists.alioth.debian.org> | 2015-10-08 09:28:03 -0700 |
---|---|---|
committer | SVN-Git Migration <python-modules-team@lists.alioth.debian.org> | 2015-10-08 09:28:03 -0700 |
commit | 0703ed57b9dd3cec45974abc255e5a2f89e3b49a (patch) | |
tree | 5c16375757be87004fab4333ac55d766009b6866 /kiwi/log.py | |
parent | 5d183b3376d1d9250985468c05678e4c40e0bc61 (diff) |
Imported Upstream version 1.9.13
Diffstat (limited to 'kiwi/log.py')
-rw-r--r-- | kiwi/log.py | 191 |
1 files changed, 116 insertions, 75 deletions
diff --git a/kiwi/log.py b/kiwi/log.py index dc0920c..ee5f690 100644 --- a/kiwi/log.py +++ b/kiwi/log.py @@ -18,97 +18,122 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # USA # -# Author(s): Adriano Monteiro <adriano@globalret.com.br> -# Johan Dahlin <jdahlin@async.com.br> +# Author(s): Johan Dahlin <jdahlin@async.com.br> # +""" +Extension to the logging module + +This module defines a couple of extensions to the logging module included +in the python standard distribution. + +It creates an additional logging handler that print log records on the +standard output. This handler is only showing records which has a level +set to logging.WARNING or higher by default. +The messages printed by this handler can be modified by using the environment +variable called KIWI_LOG. + +The syntax for the string which KIWI_LOG points to is the following:: + + domain ':' level [, domain ':', level] + +domain can contain wildcards such as * and ? +level is an integer 1-5 which defines the minimal level: + + - B{5}: DEBUG + - B{4}: INFO + - B{3}: WARNING + - B{2}: ERROR + - B{1}: CRITICAL + +Examples:: + + KIWI_LOG="stoq*:5" + +will print all the messages in a domain starting with stoq with DEBUG or higher:: + + KIWI_LOG="kiwi*:4,stoq.*:5" + +will print all the messages with INFO or higher in all domains starting with kiwi, +and all the messages in the stoq.* domains which are DEBUG or higher + +Inspiration for the syntax is taken from the U{debugging facilities<http://gstreamer.freedesktop.org/data/doc/gstreamer/head/gstreamer/html/gstreamer-GstInfo.html#id2857358>} of the +U{GStreamer<http://www.gstreamer.net>} multimedia framework. +""" + import fnmatch import logging import os -import sys + +# Globals + +_console = None +_filter = None class LogError(Exception): pass -class Formatter(logging.Formatter): - def format(self, record): - # 1: format - # 2-6: logging module - # 7: log (log.info/log.warn etc) - # 8: callsite - frame = sys._getframe(8) - filename = os.path.basename(frame.f_code.co_filename) - record.msg = '%s:%d %s' % (filename, frame.f_lineno, record.msg) - return logging.Formatter.format(self, record) - -class Logger(logging.Logger): - log_domain = 'default' - def __init__(self, name=None, level=logging.NOTSET): - """Initializes Log module, creating log handler and defining log - level. level attribute is not mandatory. It defines from which level - messages should be logged. Logs with lower level are ignored. - - logging default levels table: - - Level - - logging.NOTSET - - logging.DEBUG - - logging.INFO - - logging.WARNING - - logging.ERROR - - logging.CRITICAL - """ - if not name: - name = Logger.log_name - logging.Logger.__init__(self, name, get_log_level(name)) - - stream_handler = logging.StreamHandler(sys.stdout) - - # Formater class define a format for the log messages been - # logged with this handler - # The following format string - # ("%(asctime)s (%(levelname)s) - %(message)s") will result - # in a log message like this: - # 2005-09-07 18:15:12,636 (WARNING) - (message!) - format_string = ("%(asctime)s %(message)s") - stream_handler.setFormatter(Formatter(format_string, - datefmt='%T')) - self.addHandler(stream_handler) +class Logger(object): + # Backwards compatibility, we should probably replace the callsites + # with import logging; logging.getLogger(name) + def __new__(self, name): + return logging.getLogger(name) +class _Logger(logging.Logger): def __call__(self, message, *args, **kwargs): self.info(message, *args, **kwargs) -_log_levels = {} -_default_level = logging.WARNING +logging.setLoggerClass(_Logger) -def set_log_level(name, level): +class ReversedGlobalFilter(logging.Filter): """ - @param name: logging category - @param level: level + It's like a reversed filter, the default behavior + is to not show the message, you need to add custom filters for all + the records you wish to see """ - global _log_levels - _log_levels[name] = level + def __init__(self): + logging.Filter.__init__(self) + self.filters = [] + + def add_filter(self, f, level=logging.DEBUG): + self.filters.append((f, level)) + + def filter(self, record): + for f, level in self.filters: + if (record.levelno >= level and + fnmatch.fnmatch(record.name, f)): + return True + + return False -def get_log_level(name): +def set_log_file(filename, mask=None): """ - @param name: logging category - @returns: the level + @param filename: + @param mask: optional """ - global _log_levels, _default_level - - for category in _log_levels: - if fnmatch.fnmatch(name, category): - level = _log_levels[category] - break - else: - level = _default_level - return level + file_handler = logging.FileHandler(filename, 'w') + file_handler.setFormatter(logging.Formatter( + '%(asctime)s %(name)-18s %(levelname)-8s %(message)s', + datefmt='%F %T')) + root = logging.getLogger() + root.addHandler(file_handler) + + if mask: + file_filter = ReversedGlobalFilter() + file_filter.add_filter(mask, logging.DEBUG) + file_handler.addFilter(file_filter) -def _read_log_level(): - global _default_level +def set_log_level(name, level): + """ + @param name: logging category + @param level: level + """ + global _filter + _filter.add_filter(name, level) +def _read_log_levels(console_filter): log_levels = {} - # bootstrap issue, cannot depend on environ + # bootstrap issue, cannot depend on kiwi.environ log_level = os.environ.get('KIWI_LOG') if not log_level: return log_levels @@ -130,13 +155,29 @@ def _read_log_level(): level = 50 - (level * 10) - if name == '*': - _default_level = level - continue - log_levels[name] = level + console_filter.add_filter(name, level) + +def _create_console(): + global _filter, _console + + console = logging.StreamHandler() + console.setFormatter(logging.Formatter( + "%(asctime)s %(message)s", datefmt='%T')) + root = logging.getLogger() + root.addHandler(console) + root.setLevel(logging.DEBUG) + + console_filter = ReversedGlobalFilter() + # Always display warnings or higher on the console + console_filter.add_filter('*', logging.WARNING) + console.addFilter(console_filter) + + _read_log_levels(console_filter) - return log_levels + # Set globals + _filter = console_filter + _console = console -_log_levels = _read_log_level() +_create_console() kiwi_log = Logger('kiwi') |