created |
modified |
tags |
type |
status |
|
2024-09-04 11:46 |
gcp |
google-cloud |
python |
compute-engine |
vm |
virtual-machine |
|
|
|
I was struggling to get the native python logging to appear properly in Cloud Logging (from a Docker container running on the VM). Here is what fixed it for me:
import logging
import google.cloud.logging # pip install google-cloud-logging
gcp_logging_client = google.cloud.logging.Client()
gcp_logging_handler = google.cloud.logging.handlers.CloudLoggingHandler(
gcp_logging_client
)
gcp_logging_handler.setFormatter(
logging.Formatter(
f"%(name)s - %(levelname)s - %(message)s"
)
)
logger = logging.getLogger(__name__)
logger.addHandler(gcp_logging_handler)
logger.setLevel(logging.INFO)
logger.info("Started process")
- Links to references (source material) go here
- Links to other notes which are directly related go here