Skip to content

Commit

Permalink
logging: port to standard Python logging
Browse files Browse the repository at this point in the history
Use the standard `logging` module to configure logging instead of the
in-house `ipapython.log_manager` module and remove `ipapython.log_manager`.

Disable the logging-not-lazy and logging-format-interpolation pylint
checks.

Reviewed-By: Martin Basti <mbasti@redhat.com>
  • Loading branch information
Jan Cholasta authored and MartinBasti committed Jul 14, 2017
1 parent 4645164 commit f62a0fd
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 1,755 deletions.
13 changes: 9 additions & 4 deletions ipaclient/install/client.py
Expand Up @@ -13,6 +13,8 @@
absolute_import,
)

import logging

import dns
import getpass
import gssapi
Expand Down Expand Up @@ -51,7 +53,7 @@
from ipapython.install import typing
from ipapython.install.core import group, knob, extend_knob
from ipapython.install.common import step
from ipapython.ipa_log_manager import log_mgr, root_logger
from ipapython.ipa_log_manager import root_logger
from ipapython.ipautil import (
CalledProcessError,
dir_exists,
Expand Down Expand Up @@ -3366,9 +3368,12 @@ def uninstall(options):


def init(installer):
try:
installer.debug = log_mgr.get_handler('console').level == 'debug'
except KeyError:
for handler in root_logger.handlers:
if (isinstance(handler, logging.StreamHandler) and
handler.stream is sys.stderr): # pylint: disable=no-member
installer.debug = handler.level == logging.DEBUG
break
else:
installer.debug = True
installer.unattended = not installer.interactive

Expand Down
75 changes: 56 additions & 19 deletions ipalib/plugable.py
Expand Up @@ -24,7 +24,9 @@
you are unfamiliar with this Python feature, see
http://docs.python.org/ref/sequence-types.html
"""
import logging
import operator
import re
import sys
import threading
import os
Expand All @@ -42,7 +44,7 @@
from ipalib.util import classproperty
from ipalib.base import ReadOnly, lock, islocked
from ipalib.constants import DEFAULT_CONFIG
from ipapython import ipautil
from ipapython import ipa_log_manager, ipautil
from ipapython.ipa_log_manager import (
log_mgr,
LOGGING_FORMAT_FILE,
Expand Down Expand Up @@ -439,28 +441,63 @@ def bootstrap(self, parser=None, **overrides):
parser = self.build_global_parser()
self.parser = parser

root_logger = ipa_log_manager.root_logger

# If logging has already been configured somewhere else (like in the
# installer), don't add handlers or change levels:
if log_mgr.configure_state != 'default' or self.env.validate_api:
if root_logger.handlers or self.env.validate_api:
return

log_mgr.default_level = 'info'
log_mgr.configure_from_env(self.env, configure_state='api')
if self.env.debug:
level = logging.DEBUG
else:
level = logging.INFO
root_logger.setLevel(level)

for attr in self.env:
match = re.match(r'^log_logger_level_'
r'(debug|info|warn|warning|error|critical|\d+)$',
attr)
if not match:
continue

value = match.group(1)
try:
level = int(value)
except ValueError:
try:
level = {
'debug': logging.DEBUG,
'info': logging.INFO,
'warn': logging.WARNING,
'warning': logging.WARNING,
'error': logging.ERROR,
'critical': logging.CRITICAL
}[value]
except KeyError:
raise ValueError('unknown log level (%s)' % value)

value = getattr(self.env, attr)
regexps = re.split('\s*,\s*', value)

# Add the regexp, it maps to the configured level
for regexp in regexps:
root_logger.addFilter(ipa_log_manager.Filter(regexp, level))

# Add stderr handler:
level = 'info'
level = logging.INFO
if self.env.debug:
level = 'debug'
level = logging.DEBUG
else:
if self.env.context == 'cli':
if self.env.verbose > 0:
level = 'info'
level = logging.INFO
else:
level = 'warning'

log_mgr.create_log_handlers([dict(name='console',
stream=sys.stderr,
level=level,
format=LOGGING_FORMAT_STDERR)])
level = logging.WARNING
handler = logging.StreamHandler()
handler.setLevel(level)
handler.setFormatter(ipa_log_manager.Formatter(LOGGING_FORMAT_STDERR))
root_logger.addHandler(handler)

# Add file handler:
if self.env.mode in ('dummy', 'unit_test'):
Expand All @@ -475,17 +512,17 @@ def bootstrap(self, parser=None, **overrides):
log.error('Could not create log_dir %r', log_dir)
return

level = 'info'
level = logging.INFO
if self.env.debug:
level = 'debug'
level = logging.DEBUG
try:
log_mgr.create_log_handlers([dict(name='file',
filename=self.env.log,
level=level,
format=LOGGING_FORMAT_FILE)])
handler = logging.FileHandler(self.env.log)
except IOError as e:
log.error('Cannot open log file %r: %s', self.env.log, e)
return
handler.setLevel(level)
handler.setFormatter(ipa_log_manager.Formatter(LOGGING_FORMAT_FILE))
root_logger.addHandler(handler)

def build_global_parser(self, parser=None, context=None):
"""
Expand Down
9 changes: 7 additions & 2 deletions ipapython/admintool.py
Expand Up @@ -22,6 +22,7 @@
Handles common operations like option parsing and logging
"""

import logging
import sys
import os
import traceback
Expand Down Expand Up @@ -230,8 +231,12 @@ def setup_logging(self, log_file_mode='w'):
Logging to file is only set up after option validation and prompting;
before that, all output will go to the console only.
"""
if 'console' in ipa_log_manager.log_mgr.handlers:
ipa_log_manager.log_mgr.remove_handler('console')
root_logger = ipa_log_manager.root_logger
for handler in root_logger.handlers:
if (isinstance(handler, logging.StreamHandler) and
handler.stream is sys.stderr): # pylint: disable=no-member
root_logger.removeHandler(handler)
break

self._setup_logging(log_file_mode=log_file_mode)

Expand Down

0 comments on commit f62a0fd

Please sign in to comment.