【发布时间】:2016-01-15 02:29:23
【问题描述】:
我创建了一个使用parallel-ssh 模块(AKA pssh)的python 脚本,以便在远程节点上运行各种命令。其中一个命令是puppet agent -t,它使用ANSII 颜色代码输出。
当脚本运行并输出到终端时,着色按预期工作。但是,在脚本日志中,我看到它没有解析 ANSII 代码,而是为每一行得到了一个丑陋的包装器,如下所示:
2016-01-12 20:23:30,748 INFO: [ubuntu01] ESC[1;31mWarning: Setting templatedir is deprecated. See http://links.puppetlabs.com/env-settings-deprecations
2016-01-12 20:23:30,748 INFO: [ubuntu01] (at /usr/lib/ruby/vendor_ruby/puppet/settings.rb:1139:in `issue_deprecation_warning')ESC[0m
2016-01-12 20:23:30,749 INFO: [ubuntu01] ESC[0;32mInfo: Retrieving pluginESC[0m
2016-01-12 20:23:31,984 INFO: [ubuntu01] ESC[0;32mInfo: Caching catalog for ubuntu01.puppetlabESC[0m
2016-01-12 20:23:32,014 INFO: [ubuntu01] ESC[0;32mInfo: Applying configuration version '1452623010'ESC[0m
2016-01-12 20:23:32,083 INFO: [ubuntu01] ESC[mNotice: Finished catalog run in 0.08 secondsESC[0m
2016-01-12 20:23:32,351 INFO: [ubuntu01] * agent is running
2016-01-12 20:23:32,353 INFO: [centos01] ESC[0;32mInfo: Retrieving pluginfactsESC[0m
2016-01-12 20:23:32,353 INFO: [centos01] ESC[0;32mInfo: Retrieving pluginESC[0m
2016-01-12 20:23:33,712 INFO: [centos01] ESC[0;32mInfo: Caching catalog for centos01.puppetlabESC[0m
2016-01-12 20:23:33,838 INFO: [centos01] ESC[0;32mInfo: Applying configuration version '1452623010'ESC[0m
2016-01-12 20:23:34,101 INFO: [centos01] ESC[mNotice: Finished catalog run in 0.27 secondsESC[0m
2016-01-12 20:23:34,421 INFO: [centos01] puppet (pid 2069) is running...
这很令人沮丧,因为它降低了日志的可读性。
我尝试使用在this thread 中找到的re.compile(r'\x1b[^m]*m') 方法修改记录器配置,如下所示:
import logging
import re
ansi_escape = re.compile(r'\x1b[^m]*m')
message = ansi_escape.sub('', '%(message)s')
def set_logger(log_file):
"""Define the logger.."""
try:
logging.basicConfig(filename=log_file, level=logging.INFO,
format='%(asctime)s %(levelname)s: ' + message)
logger = logging.getLogger(__name__)
return logger
except IOError:
print "ERROR: No permissions to access the log file! Please run the script as root user (sudo will also do the trick)..\n"
exit(2)
脚本运行正常,但没有任何更改,而且所有这些 ANSII 代码的日志看起来仍然很混乱。 我认为可能还有其他地方可以为 pssh 记录器设置单独的处理程序,但我找不到它。
任何帮助将不胜感激!
【问题讨论】:
-
您当前的代码替换了日志模板,而不是实际的消息。解决此问题的最简单方法是在记录的消息被记录之前清理它们。
-
你能提供更多细节吗? pssh 模块有它自己的记录器 AFAIK,所以我不确定我是否能够在输出被记录之前对其进行操作。谢谢!