From 60bba80e416e94ce406a7a00750de92434470ae6 Mon Sep 17 00:00:00 2001 From: Zack Cerza Date: Fri, 9 May 2014 12:55:42 -0500 Subject: [PATCH] Express hostnames as child logger names Signed-off-by: Zack Cerza --- teuthology/orchestra/run.py | 19 +++++++++---------- teuthology/orchestra/test/test_run.py | 18 +++++++++++------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/teuthology/orchestra/run.py b/teuthology/orchestra/run.py index 7ff637c0fb3e9..2fd8f25033249 100644 --- a/teuthology/orchestra/run.py +++ b/teuthology/orchestra/run.py @@ -89,7 +89,7 @@ def execute(client, args, name=None): host = name else: (host, port) = client.get_transport().getpeername() - log.info('Running [{h}]: {cmd!r}'.format(h=host, cmd=cmd)) + log.getChild(host).info(u"Running: {cmd!r}".format(cmd=cmd)) (in_, out, err) = client.exec_command(cmd) @@ -124,7 +124,7 @@ def execute(client, args, name=None): return r -def copy_to_log(f, logger, host, loglevel=logging.INFO): +def copy_to_log(f, logger, loglevel=logging.INFO): """ Interface to older xreadlines api. """ @@ -138,7 +138,7 @@ def copy_to_log(f, logger, host, loglevel=logging.INFO): line = line.rstrip() # Second part of work-around for http://tracker.ceph.com/issues/8313 line = unicode(line, 'utf-8', 'replace').encode('utf-8') - logger.log(loglevel, '[' + host + ']: ' + line) + logger.log(loglevel, line) def copy_and_close(src, fdst): @@ -152,7 +152,7 @@ def copy_and_close(src, fdst): fdst.close() -def copy_file_to(f, dst, host): +def copy_file_to(f, dst): """ Copy file :param f: file to be copied. @@ -163,10 +163,9 @@ def copy_file_to(f, dst, host): # looks like a Logger to me; not using isinstance to make life # easier for unit tests handler = copy_to_log - return handler(f, dst, host) else: handler = shutil.copyfileobj - return handler(f, dst) + return handler(f, dst) class CommandFailedError(Exception): @@ -332,8 +331,8 @@ def run( g_err = None if stderr is not PIPE: if stderr is None: - stderr = logger.getChild('err') - g_err = gevent.spawn(copy_file_to, r.stderr, stderr, name) + stderr = logger.getChild(name).getChild('err') + g_err = gevent.spawn(copy_file_to, r.stderr, stderr) r.stderr = stderr else: assert not wait, \ @@ -342,8 +341,8 @@ def run( g_out = None if stdout is not PIPE: if stdout is None: - stdout = logger.getChild('out') - g_out = gevent.spawn(copy_file_to, r.stdout, stdout, name) + stdout = logger.getChild(name).getChild('out') + g_out = gevent.spawn(copy_file_to, r.stdout, stdout) r.stdout = stdout else: assert not wait, \ diff --git a/teuthology/orchestra/test/test_run.py b/teuthology/orchestra/test/test_run.py index 96cee4417a3ff..534ebc280ace8 100644 --- a/teuthology/orchestra/test/test_run.py +++ b/teuthology/orchestra/test/test_run.py @@ -29,13 +29,15 @@ class TestRun(object): out.expects('xreadlines').with_args().returns(['foo', 'bar']) err.expects('xreadlines').with_args().returns(['bad']) logger = fudge.Fake('logger') + log_host = fudge.Fake('log_host') + logger.expects('getChild').with_args('HOST').returns(log_host) log_err = fudge.Fake('log_err') - logger.expects('getChild').with_args('err').returns(log_err) - log_err.expects('log').with_args(logging.INFO, '[HOST]: bad') + log_host.expects('getChild').with_args('err').returns(log_err) + log_err.expects('log').with_args(logging.INFO, 'bad') log_out = fudge.Fake('log_out') - logger.expects('getChild').with_args('out').returns(log_out) - log_out.expects('log').with_args(logging.INFO, '[HOST]: foo') - log_out.expects('log').with_args(logging.INFO, '[HOST]: bar') + log_host.expects('getChild').with_args('out').returns(log_out) + log_out.expects('log').with_args(logging.INFO, 'foo') + log_out.expects('log').with_args(logging.INFO, 'bar') channel = fudge.Fake('channel') out.has_attr(channel=channel) channel.expects('recv_exit_status').with_args().returns(0) @@ -68,9 +70,11 @@ class TestRun(object): out.expects('read').with_args().returns('') err.expects('xreadlines').with_args().returns(['bad']) logger = fudge.Fake('logger') + log_host = fudge.Fake('log_host') + logger.expects('getChild').with_args('HOST').returns(log_host) log_err = fudge.Fake('log_err') - logger.expects('getChild').with_args('err').returns(log_err) - log_err.expects('log').with_args(logging.INFO, '[HOST]: bad') + log_host.expects('getChild').with_args('err').returns(log_err) + log_err.expects('log').with_args(logging.INFO, 'bad') channel = fudge.Fake('channel') out.has_attr(channel=channel) channel.expects('recv_exit_status').with_args().returns(0) -- 2.39.5