issue #552: include process identity in log messages.

pull/564/head
David Wilson 6 years ago
parent 2226c23c91
commit 7dacb68eeb

@ -40,6 +40,25 @@ except ImportError:
display = Display() display = Display()
#: The process name set via :func:`set_process_name`.
_process_name = None
#: The PID of the process that last called :func:`set_process_name`, so its
#: value can be ignored in unknown fork children.
_process_pid = None
def set_process_name(name):
"""
Set a name to adorn log messages with.
"""
global _process_name
_process_name = name
global _process_pid
_process_pid = os.getpid()
class Handler(logging.Handler): class Handler(logging.Handler):
""" """
Use Mitogen's log format, but send the result to a Display method. Use Mitogen's log format, but send the result to a Display method.
@ -65,7 +84,12 @@ class Handler(logging.Handler):
if mitogen_name in self.NOISY_LOGGERS and record.levelno >= logging.WARNING: if mitogen_name in self.NOISY_LOGGERS and record.levelno >= logging.WARNING:
record.levelno = logging.DEBUG record.levelno = logging.DEBUG
s = '[pid %d] %s' % (os.getpid(), self.format(record)) if _process_pid == os.getpid():
process_name = _process_name
else:
process_name = '?'
s = '[%-4s %d] %s' % (process_name, os.getpid(), self.format(record))
if record.levelno >= logging.ERROR: if record.levelno >= logging.ERROR:
display.error(s, wrap_text=False) display.error(s, wrap_text=False)
elif record.levelno >= logging.WARNING: elif record.levelno >= logging.WARNING:

@ -185,19 +185,21 @@ class MuxProcess(object):
cls.profiling = os.environ.get('MITOGEN_PROFILING') is not None cls.profiling = os.environ.get('MITOGEN_PROFILING') is not None
if cls.profiling: if cls.profiling:
mitogen.core.enable_profiling() mitogen.core.enable_profiling()
if _init_logging:
ansible_mitogen.logging.setup()
cls.original_env = dict(os.environ) cls.original_env = dict(os.environ)
cls.child_pid = os.fork() cls.child_pid = os.fork()
if _init_logging:
ansible_mitogen.logging.setup()
if cls.child_pid: if cls.child_pid:
save_pid('controller') save_pid('controller')
ansible_mitogen.logging.set_process_name('top')
ansible_mitogen.affinity.policy.assign_controller() ansible_mitogen.affinity.policy.assign_controller()
cls.child_sock.close() cls.child_sock.close()
cls.child_sock = None cls.child_sock = None
mitogen.core.io_op(cls.worker_sock.recv, 1) mitogen.core.io_op(cls.worker_sock.recv, 1)
else: else:
save_pid('mux') save_pid('mux')
ansible_mitogen.logging.set_process_name('mux')
ansible_mitogen.affinity.policy.assign_muxprocess() ansible_mitogen.affinity.policy.assign_muxprocess()
cls.worker_sock.close() cls.worker_sock.close()
cls.worker_sock = None cls.worker_sock = None

@ -108,6 +108,7 @@ def wrap_worker__run(*args, **kwargs):
if mitogen.core._profile_hook.__name__ != '_profile_hook': if mitogen.core._profile_hook.__name__ != '_profile_hook':
signal.signal(signal.SIGTERM, signal.SIG_IGN) signal.signal(signal.SIGTERM, signal.SIG_IGN)
ansible_mitogen.logging.set_process_name('task')
ansible_mitogen.affinity.policy.assign_worker() ansible_mitogen.affinity.policy.assign_worker()
return mitogen.core._profile_hook('WorkerProcess', return mitogen.core._profile_hook('WorkerProcess',
lambda: worker__run(*args, **kwargs) lambda: worker__run(*args, **kwargs)

Loading…
Cancel
Save