display - Fix up tracebacks on 3rd party loggers when log path is set (#65582)
This commit is contained in:
parent
09fca101b7
commit
b782227642
2 changed files with 21 additions and 2 deletions
4
changelogs/fragments/logging-traceback.yaml
Normal file
4
changelogs/fragments/logging-traceback.yaml
Normal file
|
@ -0,0 +1,4 @@
|
|||
bugfixes:
|
||||
- display logging - Fix issue where 3rd party modules will print tracebacks when attempting to log information when ``ANSIBLE_LOG_PATH`` is set - https://github.com/ansible/ansible/issues/65249
|
||||
- display logging - Re-added the ``name`` attribute to the log formatter so that the source of the log can be seen
|
||||
- display logging - Fixed up the logging formatter to use the proper prefixes for ``u=user`` and ``p=process``
|
|
@ -57,15 +57,30 @@ class FilterBlackList(logging.Filter):
|
|||
return not any(f.filter(record) for f in self.blacklist)
|
||||
|
||||
|
||||
class FilterUserInjector(logging.Filter):
|
||||
"""
|
||||
This is a filter which injects the current user as the 'user' attribute on each record. We need to add this filter
|
||||
to all logger handlers so that 3rd party libraries won't print an exception due to user not being defined.
|
||||
"""
|
||||
username = getpass.getuser()
|
||||
|
||||
def filter(self, record):
|
||||
record.user = FilterUserInjector.username
|
||||
return True
|
||||
|
||||
|
||||
logger = None
|
||||
# TODO: make this a callback event instead
|
||||
if getattr(C, 'DEFAULT_LOG_PATH'):
|
||||
path = C.DEFAULT_LOG_PATH
|
||||
if path and (os.path.exists(path) and os.access(path, os.W_OK)) or os.access(os.path.dirname(path), os.W_OK):
|
||||
logging.basicConfig(filename=path, level=logging.INFO, format='%(asctime)s p=%(user)s u=%(process)d | %(message)s')
|
||||
logger = logging.LoggerAdapter(logging.getLogger('ansible'), {'user': getpass.getuser()})
|
||||
logging.basicConfig(filename=path, level=logging.DEBUG,
|
||||
format='%(asctime)s p=%(process)d u=%(user)s n=%(name)s | %(message)s')
|
||||
|
||||
logger = logging.getLogger('ansible')
|
||||
for handler in logging.root.handlers:
|
||||
handler.addFilter(FilterBlackList(getattr(C, 'DEFAULT_LOG_FILTER', [])))
|
||||
handler.addFilter(FilterUserInjector())
|
||||
else:
|
||||
print("[WARNING]: log file at %s is not writeable and we cannot create it, aborting\n" % path, file=sys.stderr)
|
||||
|
||||
|
|
Loading…
Reference in a new issue