python - 使用 ubuntu-latest 在 github 运行器中基于 Python 的彩色日志记录
问题描述
我在 python 中使用这个库,使用这个库https://coloredlogs.readthedocs.io/en/latest/index.html
import coloredlogs, logging
logger = logging.getLogger(__name__)
coloredlogs.install(level='DEBUG', logger=logger)
logger.debug("test")
上面的代码在我的本地按预期工作并test
打印出来green.
但是,当我在这样的 github 运行器上运行它时
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Create workflow
run: |
python scripts/my-logging-file.py
runner 上生成的输出没有任何颜色。是否有任何特定的环境。需要设置或任何其他更改的变量?
解决方案
根据kosta的评论,降级以coloredlogs==12.0
解决下面描述的问题。
coloredlogs
默认情况下使用自动检测,在 linux 上,仅当输出是 TTY 时才启用彩色输出。根据文档,它应该可以isatty = True
通过**kw
tocoloredlogs.install
强制彩色输出。
但是文档和代码不一致,看起来代码有错误并且强制彩色输出不起作用。请参阅https://github.com/xolox/python-coloredlogs/issues/84。
import coloredlogs, logging
logger = logging.getLogger(__name__)
coloredlogs.install(level='DEBUG', logger=logger, isatty=true)
logger.debug("test")
coloredlogs.install(level=None, **kw)
isatty
–True
使用ColoredFormatter
,False
使用正常Formatter
(默认使用自动检测terminal_supports_colors()
)。
https://coloredlogs.readthedocs.io/en/latest/api.html?highlight=isatty#coloredlogs.install
terminal_supports_colors
humanfriendly
如果传递的流是 TTY,则来自并返回 true。在 GitHub Actions 运行器中,输出被重定向。
humanfriendly.terminal.terminal_supports_colors(stream=None)
检查流是否连接到支持 ANSI 转义序列的终端。
参数: stream – 要检查的流(类似文件的对象,默认为sys.stdout
)。
返回:True
如果终端支持 ANSI 转义序列,否则为 False。
源代码
推荐阅读
- php - 删除子数组中的 JSON 项
- regex - 替换管道字符“|”之间的字符串 和“_”使用 sed
- javascript - Firebase Google 登录卡在 /__/auth/handler 路由上
- azure - QnAMaker 日志的洞察查询中的元数据
- sql - 值在逗号分隔的字符串中的查询条件
- python - Tensorflow Keras 方法 get_weights 和 set_weights 有对应的 PyTorch 吗?
- r - 具有 na.action=na.fail 的 lm 模型中的错误,这是 MuMIn 疏浚功能所必需的
- flutter - 具有相同名称的颤振合并数组
- git - 我的 GitHub 不允许我推送,而是要求我提供登录凭据并使其无效,即使我的密码正确也是如此
- r - ggradar 变量的长名称 - 更改标签