Skip to content

Commit

Permalink
Unify logger.conf and the fallback code
Browse files Browse the repository at this point in the history
- use logging.Formatter.converter = time.gmtime for both the file config and fallback
  config to have the same (UTC) time, in both cases
- unify log levels
  • Loading branch information
bocekm committed Mar 16, 2020
1 parent 430ac84 commit 12baf34
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 24 deletions.
3 changes: 2 additions & 1 deletion leapp/libraries/stdlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ def run(args, split=False, callback_raw=_console_logging_handler, callback_lineb
"""
Run a command and return its result as a dict.
The execution of the program and it's results are captured by the audit.
The execution of the command and its result is captured by the audit. The CalledProcessError is raised when
the command exits with non-zero code.
:param args: Command to execute
:type args: list or tuple
Expand Down
71 changes: 49 additions & 22 deletions leapp/logger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from leapp.utils.actorapi import get_actor_api, RequestException
from leapp.utils.audit import Audit

_logger = None
_leapp_logger = None


class LeappAuditHandler(logging.Handler):
Expand Down Expand Up @@ -52,39 +52,66 @@ def _remote_emit(self, log_data):


def configure_logger(log_file=None):
global _logger
if not _logger:
"""
Configure loggers as per the description below.
logger: root
level: DEBUG
handler: StreamHandler
level: based on the debug/verbosity options
handler: LeappAuditHandler
level: DEBUG
logger: urllib3
level: WARN
logger: leapp
level: NOTSET
handler: FileHandler
level: DEBUG
:return: The 'leapp' logger
"""
global _leapp_logger
if not _leapp_logger:
_leapp_logger = logging.getLogger('leapp')

log_format = '%(asctime)s.%(msecs)-3d %(levelname)-8s PID: %(process)d %(name)s: %(message)s'
log_date_format = '%Y-%m-%d %H:%M:%S'
path = os.getenv('LEAPP_LOGGER_CONFIG', '/etc/leapp/logger.conf')
logging.Formatter.converter = time.gmtime

root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)

path = os.getenv('LEAPP_LOGGER_CONFIG', '/etc/leapp/logger.conf')
if path and os.path.isfile(path):
logging.config.fileConfig(path)
else: # Fall back logging configuration
logging.Formatter.converter = time.gmtime
logging.basicConfig(
level=logging.ERROR,
format=log_format,
datefmt=log_date_format,
stream=sys.stderr,
)
stderr_handler = logging.StreamHandler(sys.stderr)
stderr_handler.setFormatter(logging.Formatter(fmt=log_format, datefmt=log_date_format))
stderr_handler.setLevel(logging.ERROR)
root_logger.addHandler(stderr_handler)

logging.getLogger('urllib3').setLevel(logging.WARN)
handler = LeappAuditHandler()
handler.setFormatter(logging.Formatter(fmt=log_format, datefmt=log_date_format))
logging.getLogger('leapp').addHandler(handler)

audit_handler = LeappAuditHandler()
audit_handler.setFormatter(logging.Formatter(fmt=log_format, datefmt=log_date_format))
audit_handler.setLevel(logging.DEBUG)
root_logger.addHandler(audit_handler)

if log_file:
file_handler = logging.FileHandler(os.path.join(get_config().get('logs', 'dir'), log_file))
file_handler.setFormatter(logging.Formatter(fmt=log_format, datefmt=log_date_format))
file_handler.setLevel(logging.DEBUG)
logging.getLogger('leapp').addHandler(file_handler)
_leapp_logger.addHandler(file_handler)

if is_verbose():
for handler in logging.getLogger().handlers:
if isinstance(handler, logging.StreamHandler):
handler.setLevel(logging.DEBUG if is_debug() else logging.INFO)
for handler in root_logger.handlers:
if isinstance(handler, logging.StreamHandler):
if is_debug():
handler.setLevel(logging.DEBUG)
elif is_verbose():
handler.setLevel(logging.INFO)
else:
handler.setLevel(logging.ERROR)

_logger = logging.getLogger('leapp')
_logger.info('Logging has been initialized')
_leapp_logger.info('Logging has been initialized')

return _logger
return _leapp_logger
2 changes: 1 addition & 1 deletion leapp/snactor/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def last_snactor_context(connection=None):
Retrieves the last snactor-run context from the database. It generates a new one if none has been found.
:param connection: Database connection to use instead of the default connection.
:returns: String representing the latest snactor-run context uuid.
:return: String representing the latest snactor-run context uuid.
"""
with get_connection(db=connection) as db:
cursor = db.execute('''
Expand Down

0 comments on commit 12baf34

Please sign in to comment.