I have this simple Hello World type example in Python3 on a Docker container which is spun on using a Google Cloud Compute VM using Container Optimized OS.
Dockerfile
FROM python:3.11.0a7-bullseye
WORKDIR /application
COPY . .
CMD python3 main.py
main.py
import logging
logging.basicConfig(level=logging.INFO)
def main():
logging.info("THIS IS INFO")
logging.warning("THIS IS WARNING")
logging.error("THIS IS ERROR")
if __name__ == "__main__":
main()
Running the container on my local machine results in expected output:
INFO:root:THIS IS INFO
WARNING:root:THIS IS WARNING
ERROR:root:THIS IS ERROR
But the issue is when I start a Compute VM, the output is seen but the severity is not correct. All statements are treated as Default
.
What I've read so far:
I ended up using https://cloud.google.com/logging/docs/reference/libraries#write_standard_logs because this way I'm still using native Python Logging library and Logging will pick up the severity. By default only INFO
and above will be logged.
# Imports the Cloud Logging client library
import google.cloud.logging
# Instantiates a client
client = google.cloud.logging.Client()
# Retrieves a Cloud Logging handler based on the environment
# you're running in and integrates the handler with the
# Python logging module. By default this captures all logs
# at INFO level and higher
client.setup_logging()
# Imports Python standard library logging
import logging
# The data to log
text = "Hello, world!"
# Emits the data using the standard logging module
logging.warning(text)
print("Logged: {}".format(text))