mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-08-04 05:04:22 -07:00
some logging fixes (#56311)
* some logging fixes fixes #25757, #25758, #25761 alternative to #41859 and #25765 * better color handling, courtesy of alikins
This commit is contained in:
parent
484c023316
commit
a7837edcf2
2 changed files with 30 additions and 14 deletions
2
changelogs/fragments/logging_updates.yml
Normal file
2
changelogs/fragments/logging_updates.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- several minor fixes to ansible logging, allow deterministic log name, better level matching, leaner setup.
|
|
@ -34,7 +34,7 @@ from struct import unpack, pack
|
||||||
from termios import TIOCGWINSZ
|
from termios import TIOCGWINSZ
|
||||||
|
|
||||||
from ansible import constants as C
|
from ansible import constants as C
|
||||||
from ansible.errors import AnsibleError
|
from ansible.errors import AnsibleError, AnsibleAssertionError
|
||||||
from ansible.module_utils._text import to_bytes, to_text
|
from ansible.module_utils._text import to_bytes, to_text
|
||||||
from ansible.module_utils.six import with_metaclass
|
from ansible.module_utils.six import with_metaclass
|
||||||
from ansible.utils.color import stringc
|
from ansible.utils.color import stringc
|
||||||
|
@ -58,19 +58,28 @@ class FilterBlackList(logging.Filter):
|
||||||
|
|
||||||
|
|
||||||
logger = None
|
logger = None
|
||||||
# TODO: make this a logging callback instead
|
# TODO: make this a callback event instead
|
||||||
if getattr(C, 'DEFAULT_LOG_PATH'):
|
if getattr(C, 'DEFAULT_LOG_PATH'):
|
||||||
path = 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):
|
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.DEBUG, format='%(asctime)s %(name)s %(message)s')
|
logging.basicConfig(filename=path, level=logging.INFO, format='%(asctime)s p=%(user)s u=%(process)d | %(message)s')
|
||||||
mypid = str(os.getpid())
|
logger = logging.LoggerAdapter(logging.getLogger('ansible'), {'user': getpass.getuser()})
|
||||||
user = getpass.getuser()
|
|
||||||
logger = logging.getLogger("p=%s u=%s | " % (mypid, user))
|
|
||||||
for handler in logging.root.handlers:
|
for handler in logging.root.handlers:
|
||||||
handler.addFilter(FilterBlackList(getattr(C, 'DEFAULT_LOG_FILTER', [])))
|
handler.addFilter(FilterBlackList(getattr(C, 'DEFAULT_LOG_FILTER', [])))
|
||||||
else:
|
else:
|
||||||
print("[WARNING]: log file at %s is not writeable and we cannot create it, aborting\n" % path, file=sys.stderr)
|
print("[WARNING]: log file at %s is not writeable and we cannot create it, aborting\n" % path, file=sys.stderr)
|
||||||
|
|
||||||
|
# map color to log levels
|
||||||
|
color_to_log_level = {C.COLOR_ERROR: logging.ERROR,
|
||||||
|
C.COLOR_WARN: logging.WARNING,
|
||||||
|
C.COLOR_OK: logging.INFO,
|
||||||
|
C.COLOR_SKIP: logging.WARNING,
|
||||||
|
C.COLOR_UNREACHABLE: logging.ERROR,
|
||||||
|
C.COLOR_DEBUG: logging.DEBUG,
|
||||||
|
C.COLOR_CHANGED: logging.INFO,
|
||||||
|
C.COLOR_DEPRECATE: logging.WARNING,
|
||||||
|
C.COLOR_VERBOSE: logging.INFO}
|
||||||
|
|
||||||
b_COW_PATHS = (
|
b_COW_PATHS = (
|
||||||
b"/usr/bin/cowsay",
|
b"/usr/bin/cowsay",
|
||||||
b"/usr/games/cowsay",
|
b"/usr/games/cowsay",
|
||||||
|
@ -161,19 +170,24 @@ class Display(with_metaclass(Singleton, object)):
|
||||||
raise
|
raise
|
||||||
|
|
||||||
if logger and not screen_only:
|
if logger and not screen_only:
|
||||||
msg2 = nocolor.lstrip(u'\n')
|
# We first convert to a byte string so that we get rid of
|
||||||
|
# color and characters that are invalid in the user's locale
|
||||||
|
msg2 = to_bytes(nocolor.lstrip(u'\n'))
|
||||||
|
|
||||||
msg2 = to_bytes(msg2)
|
|
||||||
if sys.version_info >= (3,):
|
if sys.version_info >= (3,):
|
||||||
# Convert back to text string on python3
|
# Convert back to text string on python3
|
||||||
# We first convert to a byte string so that we get rid of
|
|
||||||
# characters that are invalid in the user's locale
|
|
||||||
msg2 = to_text(msg2, self._output_encoding(stderr=stderr))
|
msg2 = to_text(msg2, self._output_encoding(stderr=stderr))
|
||||||
|
|
||||||
if color == C.COLOR_ERROR:
|
lvl = logging.INFO
|
||||||
logger.error(msg2)
|
if color:
|
||||||
else:
|
# set logger level based on color (not great)
|
||||||
logger.info(msg2)
|
try:
|
||||||
|
lvl = color_to_log_level[color]
|
||||||
|
except KeyError:
|
||||||
|
# this should not happen, but JIC
|
||||||
|
raise AnsibleAssertionError('Invalid color supplied to display: %s' % color)
|
||||||
|
# actually log
|
||||||
|
logger.log(lvl, msg2)
|
||||||
|
|
||||||
def v(self, msg, host=None):
|
def v(self, msg, host=None):
|
||||||
return self.verbose(msg, host=host, caplevel=0)
|
return self.verbose(msg, host=host, caplevel=0)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue