Skip to content

Commit

Permalink
Unify logger.conf and the fallback code
Browse files Browse the repository at this point in the history
- add missing 'leapp' logger to the logger.conf
  - it caused certain commands to get stuck on RHEL 7 when leapp package (with the
    conf file) was installed under certain circumstances, e.g. while running
    `snactor workflow sanity-check ipu` in our devel RHEL 7 vagrant with
    the leapp-repository NFS-synced
- 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 10, 2020
1 parent 15a79b8 commit 7e056e9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
14 changes: 10 additions & 4 deletions etc/leapp/logger.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[loggers]
keys=urllib3,root
keys=urllib3,root,leapp

[formatters]
keys=leapp
Expand All @@ -18,16 +18,22 @@ qualname=urllib3
handlers=stream

[logger_root]
level=DEBUG
handlers=leapp_audit,stream
level=ERROR
handlers=stream

[logger_leapp]
level=NOTSET
handlers=leapp_audit
qualname=compiler.parser

[handler_leapp_audit]
class=leapp.logger.LeappAuditHandler
level=NOTSET
formatter=leapp
args=()

[handler_stream]
class=StreamHandler
level=ERROR
level=NOTSET
formatter=leapp
args=(sys.stderr,)
45 changes: 35 additions & 10 deletions leapp/logger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,37 +52,62 @@ def _remote_emit(self, log_data):


def configure_logger(log_file=None):
"""
Configure loggers as per the description below.
logger: root
level: ERROR
handler: StreamHandler
level: based on the debug/verbose options
logger: leapp
level: NOTSET
handler: LeappAuditHandler
level: NOTSET
handler: FileHandler
level: DEBUG
logger: urllib3
level: WARN
:return: The 'leapp' logger
"""
global _logger
if not _logger:
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

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,
level=logging.ERROR, # The 'root' logger level
format=log_format,
datefmt=log_date_format,
stream=sys.stderr,
stream=sys.stderr # The level of this StreamHandler is NOTSET at this moment
)
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))
# The level of both the 'leapp' logger and the audit handler is NOTSET, i.e. inherited from 'root'
logging.getLogger('leapp').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)

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 logging.getLogger().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')
Expand Down
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 7e056e9

Please sign in to comment.