Replace "print" logging with the "logging" module

This commit rewrites the make-shift "log" methods to use the logging
framework without requiring changes to the callers.  This will be done
in a latter commit to keep things reviewable.

Signed-off-by: Niels Thykier <niels@thykier.net>
ubuntu/rebased
Niels Thykier 7 years ago
parent e5d790f592
commit 5e825043d3

@ -179,6 +179,7 @@ does for the generation of the update excuses.
* The excuses are written in an HTML file. * The excuses are written in an HTML file.
""" """
import logging
import optparse import optparse
import os import os
import sys import sys
@ -253,6 +254,32 @@ class Britney(object):
the information needed by the other methods of the class. the information needed by the other methods of the class.
""" """
# setup logging - provide the "short level name" (i.e. INFO -> I) that
# we used to use prior to using the logging module.
old_factory = logging.getLogRecordFactory()
short_level_mapping = {
'CRITIAL': 'F',
'INFO': 'I',
'WARNING': 'W',
'ERROR': 'E',
'DEBUG': 'N',
}
def record_factory(*args, **kwargs):
record = old_factory(*args, **kwargs)
try:
record.shortlevelname = short_level_mapping[record.levelname]
except KeyError:
record.shortlevelname = record.levelname
return record
logging.setLogRecordFactory(record_factory)
logging.basicConfig(format='{shortlevelname}: [{asctime}] - {message}', style='{',
datefmt="%Y-%m-%dT%H:%M:%S%z")
self.logger = logging.getLogger()
# parse the command line arguments # parse the command line arguments
self.policies = [] self.policies = []
self._hint_parser = HintParser(self) self._hint_parser = HintParser(self)
@ -416,6 +443,11 @@ class Britney(object):
help="Do not compute which packages can migrate.") help="Do not compute which packages can migrate.")
(self.options, self.args) = parser.parse_args() (self.options, self.args) = parser.parse_args()
if self.options.verbose:
self.logger.setLevel(logging.INFO)
else:
self.logger.setLevel(logging.WARNING)
# integrity checks # integrity checks
if self.options.nuninst_cache and self.options.print_uninst: # pragma: no cover if self.options.nuninst_cache and self.options.print_uninst: # pragma: no cover
self.log("nuninst_cache and print_uninst are mutually exclusive!", type="E") self.log("nuninst_cache and print_uninst are mutually exclusive!", type="E")
@ -529,8 +561,12 @@ class Britney(object):
`Error'. Warnings and errors are always printed, and information is `Error'. Warnings and errors are always printed, and information is
printed only if verbose logging is enabled. printed only if verbose logging is enabled.
""" """
if self.options.verbose or type in ("E", "W"): level = {
print("%s: [%s] - %s" % (type, time.asctime(), msg)) 'I': logging.INFO,
'W': logging.WARNING,
'E': logging.ERROR,
}.get(type, logging.INFO)
self.logger.log(level, msg)
def _load_faux_packages(self, faux_packages_file): def _load_faux_packages(self, faux_packages_file):
"""Loads fake packages """Loads fake packages
@ -2771,6 +2807,7 @@ class Britney(object):
self.log('> %s' % stat, type="I") self.log('> %s' % stat, type="I")
else: else:
self.log('Migration computation skipped as requested.', type='I') self.log('Migration computation skipped as requested.', type='I')
logging.shutdown()
if __name__ == '__main__': if __name__ == '__main__':

@ -1,4 +1,5 @@
import json import json
import logging
import os import os
import time import time
from abc import abstractmethod from abc import abstractmethod
@ -30,6 +31,8 @@ class BasePolicy(object):
self.suite_info = suite_info self.suite_info = suite_info
self.applicable_suites = applicable_suites self.applicable_suites = applicable_suites
self.hints = None self.hints = None
logger_name = ".".join((self.__class__.__module__, self.__class__.__name__))
self.logger = logging.getLogger(logger_name)
# FIXME: use a proper logging framework # FIXME: use a proper logging framework
def log(self, msg, type="I"): def log(self, msg, type="I"):
@ -41,8 +44,12 @@ class BasePolicy(object):
`Error'. Warnings and errors are always printed, and information is `Error'. Warnings and errors are always printed, and information is
printed only if verbose logging is enabled. printed only if verbose logging is enabled.
""" """
if self.options.verbose or type in ("E", "W"): level = {
print("%s: [%s] - %s" % (type, time.asctime(), msg)) 'I': logging.INFO,
'W': logging.WARNING,
'E': logging.ERROR,
}.get(type, logging.INFO)
self.logger.log(level, msg)
def register_hints(self, hint_parser): # pragma: no cover def register_hints(self, hint_parser): # pragma: no cover
"""Register new hints that this policy accepts """Register new hints that this policy accepts

Loading…
Cancel
Save