Added util.configure_logging() function; API.bootstrap() now calls util.configure_logging()

This commit is contained in:
Jason Gerard DeRose
2008-10-28 01:39:02 -06:00
parent 2307d4ddd0
commit 316bd855d5
4 changed files with 59 additions and 2 deletions

View File

@@ -454,7 +454,7 @@ class CLI(object):
def parse(self, cmd): def parse(self, cmd):
parser = self.build_parser(cmd) parser = self.build_parser(cmd)
(kwc, args) = parser.parse_args( (kwc, args) = parser.parse_args(
list(self.cmd_argv), KWCollector() list(self.cmd_argv[1:]), KWCollector()
) )
kw = kwc.__todict__() kw = kwc.__todict__()
try: try:

View File

@@ -87,3 +87,19 @@ DEFAULT_CONFIG = (
('log', None), # Path to log file ('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',
])

View File

@@ -738,6 +738,13 @@ class API(DictProxy):
self.__doing('bootstrap') self.__doing('bootstrap')
self.env._bootstrap(**overrides) self.env._bootstrap(**overrides)
self.env._finalize_core(**dict(constants.DEFAULT_CONFIG)) 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): def load_plugins(self):
""" """

View File

@@ -21,10 +21,13 @@
Various utility functions. Various utility functions.
""" """
import krbV import logging
import os import os
from os import path from os import path
import imp import imp
import krbV
from constants import LOGGING_CONSOLE_FORMAT, LOGGING_FILE_FORMAT
def xmlrpc_marshal(*args, **kw): def xmlrpc_marshal(*args, **kw):
""" """
@@ -99,3 +102,34 @@ def import_plugins_subpackage(name):
for name in find_modules_in_dir(src_dir): for name in find_modules_in_dir(src_dir):
full_name = '%s.%s' % (plugins.__name__, name) full_name = '%s.%s' % (plugins.__name__, name)
__import__(full_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