loguru
loguru copied to clipboard
How to use loguru with google.cloud.logging
Hi,
I enjoy using loguru due to its simplicity. Unfortunately, I am currently running into the problem of how to combine loguru with the google.cloud.logging function to have correct logging in GCP Cloud Functions.
The described way is this: https://cloud.google.com/logging/docs/reference/libraries
Any ideas how to combine loguru with this?
Hi.
This is surely possible, but I'm unable to install google-cloud-logging
to test my hypothesis.
You can for example use a custom function propagating logs to GCP as a Loguru sink:
from google.cloud import logging
from loguru import logger
logging_client = logging.Client()
gcp_logger = logging_client.logger("gcp-logger")
def log_to_gcp(message):
gcp_logger.log_text(message, severity=message.record["level"].name)
logger.add(log_to_gcp)
According to documentation, you can also maybe call client.setup_logging()
that will configure a standard handler compatible with standard logging
. You can retrieve it and add it as a Loguru sink, as Loguru supports standard handlers as well.
@fabianmax , please have a look into issue https://github.com/Delgan/loguru/issues/812#issuecomment-1485272193. Maybe it helps.
Here's a trick i found, which work https://stackoverflow.com/a/77275130/7052018
_GCP_LOG_FORMAT = (
"{level:<.1}{time:MMDD HH:mm:ss.SSSSSS} {process} {name}:{line}] {message} | {extra}"
)
logger.remove()
logger.add(
sys.stdout,
format=_GCP_LOG_FORMAT,
level=log_level,
colorize=True,
)