Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use different logger classes depending on prefix of requested logger #1171

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 58 additions & 2 deletions easybuild/tools/build_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
@author: Pieter De Baets (Ghent University)
@author: Jens Timmerman (Ghent University)
"""
import logging
import os
import sys
import tempfile
Expand Down Expand Up @@ -120,12 +121,67 @@ def exception(self, msg, *args):
raise EasyBuildError(newMsg)


class LoggerFactory(object):
"""
Instanciate different logger classes, depending upon requested
logger name.

Loggers can be associated with prefixes; when a logger is
requested, the constructor associated with the longest matching
prefix is used to actually create the class instance.
"""

def __init__(self, loggers):
self._dispatch = {}
self.dispatch('', logging.getLoggerClass())
for prefix, cls in loggers.items():
self.dispatch(prefix, cls)

def dispatch(self, prefix, cls):
"""
Register a logger of class `cls` to be used whenever a name
starting with `prefix` is requested.
"""
self._dispatch[prefix] = cls

def __call__(self, name):
"""
Create a logger with the specified name.

The actual logger constructor is selected among the registered
ones: the constructor associated with the longest matching
prefix is used.
"""
#sys.stderr.write("=== Requested logger for %s\n" % name)
matched = -1
logger = None
for prefix, ctor in self._dispatch.items():
if name.startswith(prefix) and len(prefix) > matched:
matched = len(prefix)
logger = ctor
return logger(name)


# set format for logger
LOGGING_FORMAT = EB_MSG_PREFIX + ' %(asctime)s %(name)s %(levelname)s %(message)s'
fancylogger.setLogFormat(LOGGING_FORMAT)

# set the default LoggerClass to EasyBuildLog
fancylogger.logging.setLoggerClass(EasyBuildLog)
# set the default LoggerClass depending on the caller
logDispatcher = LoggerFactory({
# Ideally, one would use EasyBuildLog for EB and Python's default
# for anything else, but there's no common prefix for EB loggers,
# so let's use `EasyBuildLog` as default, and explicitly list
# exceptions (e.g., GC3Pie)
'': EasyBuildLog,
'gc3': logging.Logger,
})
# `logging.setLoggerClass` insists that the passed callable is a class
# definition and that it derives from `logging.Logger` (or the current
# Logger); I cannot see a way of adapting the log dispatcher to meet
# those requirements, so let us just bypass `logging.setLoggerClass`
# and set the logger class by accessing a private global variable
# directly. Not kosher, but it works.
logging._loggerClass = logDispatcher

# you can't easily set another LoggerClass before fancylogger calls getLogger on import
_init_fancylog = fancylogger.getLogger(fname=False)
Expand Down