diff options
Diffstat (limited to 'ipalib')
-rw-r--r-- | ipalib/cli.py | 2 | ||||
-rw-r--r-- | ipalib/constants.py | 16 | ||||
-rw-r--r-- | ipalib/plugable.py | 7 | ||||
-rw-r--r-- | ipalib/util.py | 36 |
4 files changed, 59 insertions, 2 deletions
diff --git a/ipalib/cli.py b/ipalib/cli.py index 3613dfeb7..6407e9e20 100644 --- a/ipalib/cli.py +++ b/ipalib/cli.py @@ -454,7 +454,7 @@ class CLI(object): def parse(self, cmd): parser = self.build_parser(cmd) (kwc, args) = parser.parse_args( - list(self.cmd_argv), KWCollector() + list(self.cmd_argv[1:]), KWCollector() ) kw = kwc.__todict__() try: diff --git a/ipalib/constants.py b/ipalib/constants.py index 25ee6c312..7220561f7 100644 --- a/ipalib/constants.py +++ b/ipalib/constants.py @@ -87,3 +87,19 @@ DEFAULT_CONFIG = ( ('log', None), # Path to log file ) + + +LOGGING_CONSOLE_FORMAT = ' '.join([ + '%(levelname)s', + '%(message)s', +]) + + +# Tab-delimited format designed to be easily opened in a spreadsheet: +LOGGING_FILE_FORMAT = ' '.join([ + '%(created)f', + '%(levelname)s', + '%(message)r', # Using %r for repr() so message is a single line + '%(pathname)s', + '%(lineno)d', +]) diff --git a/ipalib/plugable.py b/ipalib/plugable.py index 8d689f7de..dd74dc08e 100644 --- a/ipalib/plugable.py +++ b/ipalib/plugable.py @@ -738,6 +738,13 @@ class API(DictProxy): self.__doing('bootstrap') self.env._bootstrap(**overrides) self.env._finalize_core(**dict(constants.DEFAULT_CONFIG)) + if self.env.mode == 'unit_test': + return + logger = util.configure_logging( + self.env.log, + self.env.verbose, + ) + object.__setattr__(self, 'log', 'logger') def load_plugins(self): """ diff --git a/ipalib/util.py b/ipalib/util.py index d7e2c2a4b..e65f15ca4 100644 --- a/ipalib/util.py +++ b/ipalib/util.py @@ -21,10 +21,13 @@ Various utility functions. """ -import krbV +import logging import os from os import path import imp +import krbV +from constants import LOGGING_CONSOLE_FORMAT, LOGGING_FILE_FORMAT + def xmlrpc_marshal(*args, **kw): """ @@ -99,3 +102,34 @@ def import_plugins_subpackage(name): for name in find_modules_in_dir(src_dir): full_name = '%s.%s' % (plugins.__name__, name) __import__(full_name) + + +def configure_logging(log_file, verbose): + """ + Configure standard logging. + """ + # Check that directory log_file is in exists: + log_dir = path.dirname(log_file) + if not path.isdir(log_dir): + os.makedirs(log_dir) + + # Set logging level: + level = logging.INFO + if verbose: + level -= 10 + + log = logging.getLogger('ipa') + + # Configure console handler + console = logging.StreamHandler() + console.setLevel(level) + console.setFormatter(logging.Formatter(LOGGING_CONSOLE_FORMAT)) + log.addHandler(console) + + # Configure file handler + file_handler = logging.FileHandler(log_file) + file_handler.setLevel(level) + file_handler.setFormatter(logging.Formatter(LOGGING_FILE_FORMAT)) + log.addHandler(file_handler) + + return log |