mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Add configurable blacklist filtering for python logger
This commit is contained in:
parent
635036fb62
commit
7be8079bad
2 changed files with 18 additions and 0 deletions
|
@ -710,6 +710,14 @@ DEFAULT_LOG_PATH:
|
|||
ini:
|
||||
- {key: log_path, section: defaults}
|
||||
type: path
|
||||
DEFAULT_LOG_FILTER:
|
||||
name: Name filters for python logger
|
||||
default: []
|
||||
description: List of logger names to filter out of the log file
|
||||
env: [{name: ANSIBLE_LOG_FILTER}]
|
||||
ini:
|
||||
- {key: log_filter, section: defaults}
|
||||
type: list
|
||||
DEFAULT_LOOKUP_PLUGIN_PATH:
|
||||
name: Lookup Plugins Path
|
||||
description: Colon separated paths in which Ansible will search for Lookup Plugins.
|
||||
|
|
|
@ -47,6 +47,14 @@ except NameError:
|
|||
pass
|
||||
|
||||
|
||||
class FilterBlackList(logging.Filter):
|
||||
def __init__(self, blacklist):
|
||||
self.blacklist = [logging.Filter(name) for name in blacklist]
|
||||
|
||||
def filter(self, record):
|
||||
return not any(f.filter(record) for f in self.blacklist)
|
||||
|
||||
|
||||
logger = None
|
||||
# TODO: make this a logging callback instead
|
||||
if C.DEFAULT_LOG_PATH:
|
||||
|
@ -56,6 +64,8 @@ if C.DEFAULT_LOG_PATH:
|
|||
mypid = str(os.getpid())
|
||||
user = getpass.getuser()
|
||||
logger = logging.getLogger("p=%s u=%s | " % (mypid, user))
|
||||
for handler in logging.root.handlers:
|
||||
handler.addFilter(FilterBlackList(C.DEFAULT_LOG_FILTER))
|
||||
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