Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set log severity on google cloud without using google-cloud-logging library

I am trying to correctly output logs on my service running on google cloud, and for the most part they are correctly identified (DEBUG and INFO logs, being sent to stdout, are marked as info, whereas WARNING, ERROR, and CRITICAL logs are sent to stderr and are marked as error). Now, I am trying to get the exact severity out of them, without needing to use the google-cloud-logging library. Is there a way where I can accomplish this?

Here an example of what I currently obtain is shown, with severity (icon on the left) matching whether the log comes from stdout or stderr.

google cloud logging example

This is what I'm trying to obtain, but without using the google-cloud-logging library

expected log severities

Edit:

my logs are written to the output streams in json format, by using the python-json-logger library for python. My google cloud logs have their information stored as in the picture below. We are not using fluentd for log parsing.

current log parsing structure

like image 846
HitLuca Avatar asked May 19 '26 17:05

HitLuca


2 Answers

After some research and help from @SerhiiRohoza It doesn't seem you can, so in order to set the severity on google cloud you need to add the google-cloud-logging library to your project and set it up as described on the documentation.

like image 77
HitLuca Avatar answered May 21 '26 05:05

HitLuca


I also ran in this issues, where GCP would parse my log levels incorrectly.

Here's a simple trick that worked for me:

_GCP_LOGGING_FORMAT = "{levelname:.1}{asctime}.{msecs}000 {process} {name}:{lineno}] {message}"
_DATE_FMT = "%m%d %H:%M:%S"

logging.basicConfig(format=_GCP_LOGGING_FORMAT, datefmt=_DATE_FMT, style="{")

E.g.:

D1011 16:28:57.528787 1 <module>:89] Time to process request: 1234

The :.1 tells python to make this value 1 character wide.

If you are using loguru, it's equivalent will be:

_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,
)
like image 27
Aditya Verma Avatar answered May 21 '26 05:05

Aditya Verma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!