我正在使用以下步骤之一运行cloudbuild:
- name: "gcr.io/something"
id: DO-STUFF
entrypoint: python
args: ['some_script.py']
waitFor:
- WHATEVER
在我的some_script.py
内,有一些日志语句:
...
LOGGER = logging.getLogger(__name__)
...
LOGGER.info('Did work.')
...
运行cloudbuild成功运行了脚本,但是在cloudbuild日志中看不到任何日志。我如何才能将它们添加到那里?
答案 0 :(得分:1)
您必须将记录器配置为在标准输出流中打印日志。像这样更新您的代码:
...
LOGGER = logging.getLogger(__name__)
...
import sys
out_hdlr = logging.StreamHandler(sys.stdout)
# Format the output as you want. Let only the message if you don't care about other fields
out_hdlr.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
out_hdlr.setLevel(logging.INFO)
LOGGER.addHandler(out_hdlr)
...
LOGGER.info('Did work.')
...
更新
这是我的确切测试过程
执行script_log.py
import logging
import sys
print("line0")
log = logging.getLogger()
out_hdlr = logging.StreamHandler(sys.stdout)
out_hdlr.setFormatter(logging.Formatter('%(message)s'))
out_hdlr.setLevel(logging.INFO)
log.addHandler(out_hdlr)
log.setLevel(logging.INFO)
log.info("THIS IS AN ERROR")
Cloud Build文件定义(cloudbuild.yaml
文件)
steps:
- name: gcr.io/cloud-builders/gcloud
entrypoint: "bash"
args:
- "-c"
- |
python3 script_log.py
这是命令gcloud builds submit
Operation completed over 1 objects/415.0 B.
BUILD
Already have image (with digest): gcr.io/cloud-builders/gcloud
line0
THIS IS AN ERROR
PUSH
DONE
堆栈驱动程序日志中的日志
唯一的区别是我在getLogger()中没有__name__