0

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.

screenshot of logging

What I've read so far:

engineer-x
  • 2,173
  • 2
  • 12
  • 25
  • Is there a reason you did not install and set up the Cloud Logging client library for Python within your container? https://cloud.google.com/logging/docs/setup/python#connecting_the_library_to_python_logging – John Hanley May 04 '22 at 06:56
  • From the docs (https://cloud.google.com/logging/docs/setup/python#using_the_python_root_logger) it seems like I can use Python native logging. Which I would rather use since I don't want to log to Cloud Logging when debugging on my local machine. – engineer-x May 27 '22 at 18:14

1 Answers1

0

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))
engineer-x
  • 2,173
  • 2
  • 12
  • 25