支持控制台着色的应用程序的通常规则是在当前控制台是TTY时激活它。
此方法在连续集成服务器(如Jenkins,Travis,...)上中断,因为它们不会为其伪控制台打开TTY。
我想知道是否可以区分这两种情况:
mycommand
在Jenkins下运行 - 我们想启用着色mycommand >> output.log
- 我们不希望在日志文件中以ANSI转义结束。我的旧ANSI检测代码:
import sys
import os
if (hasattr(sys.stderr, "isatty") and sys.stderr.isatty()) or \
('TERM' in os.environ.keys() and os.environ['TERM'] in ['linux']) or \
('PYCHARM_HOSTED' in os.environ.keys()):
coloring = True
为了解决这个问题,我可以改进吗?
答案 0 :(得分:2)
由于您已经在使用环境变量log.info(...)
,我建议您使用类似的CI环境。你特别提到了詹金斯和特拉维斯。这两个系统都设置了可以使用的环境变量。有些可能性......
Jenkins环境变量:
PYCHARM_HOSTED
(Source)
Travis环境变量:
JENKINS_URL Set to the URL of the Jenkins master that's running the build.
BUILD_URL The URL where the results of this build can be found.
BUILD_TAG String of jenkins-${JOB_NAME}-${BUILD_NUMBER}.
(Source)