python-json-logger
python-json-logger copied to clipboard
Logs handled by stdout displayed as error by StackDriver
I read in this tutorial that, by default, the log messages captured in stdout and stderr aren't captured with severity metadata. So, if I set stdout as the only handler of my application, then is it expected that google stackdriver displays every log as error, regardless its severity? I'd like to know if my hypothesis is true and some reference to gcloud docs about handlers. Thanks in advance : )
Hi @kfcaio I'm curious what you ended up doing regarding this issue?
@seperman I ended up creating a class for handling GCP log format in my log configuration file (logging.py):
class StackdriverJsonFormatter(jsonlogger.JsonFormatter, object):
def __init__(self, fmt="%(levelname) %(message)", style='%',
*args, **kwargs):
jsonlogger.JsonFormatter.__init__(self, fmt=fmt, *args, **kwargs)
def process_log_record(self, log_record):
log_record['severity'] = log_record.pop('levelname', None)
return super().process_log_record(log_record)
Then, the only thing to do is referer to it in the default.json file:
{
"DEFAULT": {
"LOGGER": {
"formatters": {
"default": {
"format": "%(asctime)s %(threadName)s %(scriptcalled)s %(filename)s:%(lineno)d %(levelname)-8s %(message)s"
},
"stackdriver": {
"()": "digesto_common.logging.StackdriverJsonFormatter",
"format": "%(asctime)s %(threadName)s %(scriptcalled)s %(filename)s:%(lineno)d %(levelname)-8s %(message)s"
}
},
"loggers": {
"default": {
"level": "DEBUG",
"handlers": [
"stackdriver",
"file"
]
}
},
"handlers": {
"file": {
"formatter": "default",
"class": "logging.handlers.RotatingFileHandler",
"filename": "data.log",
"mode": "w",
"maxBytes": 10000000,
"backupCount": 5,
"level": "DEBUG"
},
"stackdriver": {
"level": "INFO"
}
}
}
},
@kfcaio Thanks for the quick response!
So by using the proper format, you got the logs as strcutured logs in GCP? And didn't need to use the google-cloud-logging library that Google provides?
@seperman yes, google-cloud-logging was not necessary