1

I'm running a cloudbuild with one of the steps:

- name: "gcr.io/something"
id: DO-STUFF
entrypoint: python
args: ['some_script.py']
waitFor:
  - WHATEVER

Inside my some_script.py I have some log statements:

...
LOGGER = logging.getLogger(__name__)
...
LOGGER.info('Did work.')
...

Running the cloudbuild runs the script successfully, but I see no logs in the cloudbuild logs. How can I get them to be added there?

1 Answer 1

1

You have to configure your logger to print the logs in the stdout stream. Update your code like this:

...
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.')
...

UPDATE

Here my exact test process

Script to execute 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 file definition (cloudbuild.yaml file)

steps:
        - name: gcr.io/cloud-builders/gcloud
          entrypoint: "bash"
          args:
                  - "-c"
                  - |
                          python3 script_log.py

Here the result of the command 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

And the log in the stackdriver logging

enter image description here

The only difference is that I don't have the __name__ in the getLogger()

Sign up to request clarification or add additional context in comments.

1 Comment

Switching from logging to print statements makes them visible in the cloudbuild logs. I do have the __main__ section but I don't think that can cause logging not to work. I want to try that though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.