diff options
author | Marco Capitani <m.capitani@nextworks.it> | 2017-04-24 15:00:21 +0200 |
---|---|---|
committer | Marco Capitani <m.capitani@nextworks.it> | 2017-04-24 15:00:21 +0200 |
commit | eda14393d20cdd0b9ef9303f024f4c8a0b47eda2 (patch) | |
tree | d5fffe103ad8f39669147b69e59274b0e4731fe8 /rumba/log.py | |
parent | bec81866d4da33405c35c8eb4dc15d4fc5bfbfb7 (diff) | |
download | rumba-eda14393d20cdd0b9ef9303f024f4c8a0b47eda2.tar.gz rumba-eda14393d20cdd0b9ef9303f024f4c8a0b47eda2.zip |
Logging: added set and reset logging level method, small logging modifications.
Diffstat (limited to 'rumba/log.py')
-rw-r--r-- | rumba/log.py | 115 |
1 files changed, 101 insertions, 14 deletions
diff --git a/rumba/log.py b/rumba/log.py index e856b42..0e3f0dc 100644 --- a/rumba/log.py +++ b/rumba/log.py @@ -1,3 +1,22 @@ +# +# Logging wrapper for Rumba +# +# Marco Capitani <m.capitani@nextworks.it> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301 USA import logging import sys @@ -5,21 +24,43 @@ import sys import multiprocessing +loggers_set = set() + + +class RumbaFormatter(logging.Formatter): + """The logging.Formatter subclass used by RUMBA""" + + level_name_table = { + 'CRITICAL': 'CRT', + 'ERROR': 'ERR', + 'WARNING': 'WRN', + 'INFO': 'INF', + 'DEBUG': 'DBG' + } + + def __init__(self): + super(RumbaFormatter, self).__init__( + fmt='{asctime} | {levelname:3.3} | ' + '{name:11.11} | {message}', + style='{', + datefmt='%H:%M:%S') + + def format(self, record): + record.name = record.name.split('.')[-1] + record.levelname = self.level_name_table[record.levelname] + return super(RumbaFormatter, self).format(record) + + def setup(): + """Configures the logging framework with default values.""" handler = logging.StreamHandler(sys.stdout) handler.lock = multiprocessing.RLock() - logging.basicConfig(format='{asctime} | {levelname:3.3} | ' - '{name:11.11} | {message}', - style='{', - datefmt='%H:%M:%S', - handlers=[handler], - level=logging.DEBUG) - logging.addLevelName(logging.CRITICAL, 'CRT') - logging.addLevelName(logging.ERROR, 'ERR') - logging.addLevelName(logging.WARNING, 'WRN') - logging.addLevelName(logging.INFO, 'INF') - logging.addLevelName(logging.DEBUG, 'DBG') + handler.setLevel(logging.DEBUG) + formatter = RumbaFormatter() + handler.setFormatter(formatter) + logging.basicConfig(handlers=[handler], level=logging.DEBUG) logging.getLogger('').setLevel(logging.ERROR) + logging.getLogger('rumba').setLevel(logging.DEBUG) # Used for the first call, in order to configure logging @@ -40,6 +81,52 @@ _get_logger = _get_logger_with_setup def get_logger(name): - logger = _get_logger(name.split('.')[-1]) - logger.setLevel(logging.DEBUG) - return logger + """ + Returns the logger named <name>. + <name> should be the module name, for consistency. If setup has not been + called yet, it will call it first. + :param name: the name of the desired logger + :return: The logger + """ + return _get_logger(name) + + +def set_logging_level(level, name=None): + """ + Set the current logging level to <level> for logger named <name>. + If name is not specified, sets the logging level for all rumba loggers. + Accepted levels are: + DEBUG == 10, + INFO == 20, + WARNING == 30, + ERROR == 40, + CRITICAL == 50, + NOTSET == 0 + (resets the logger: its level is set to the default or its parents' level) + :param level: the desired logging level. + :param name: The name of the logger to configure + """ + if name is None: + if level == 'NOTSET' or level == 0: + set_logging_level(logging.INFO) + return + name = 'rumba' + if (level == 'NOTSET' or level == 0) and name == '': + set_logging_level(logging.ERROR, '') + return + logger = get_logger(name) + loggers_set.add(logger) + logger.setLevel(level) + + +def reset_logging_level(): + """ + Resets the current logging levels to the defaults. + Defaults are: rumba -> INFO, + everything else -> ERROR + """ + # Un-sets every logger previously set + for logger in loggers_set: + logger.setLevel(logging.NOTSET) + set_logging_level(logging.INFO) + set_logging_level(logging.ERROR, '') |