python-json-logger icon indicating copy to clipboard operation
python-json-logger copied to clipboard

Logs handled by stdout displayed as error by StackDriver

Open kfcaio opened this issue 4 years ago • 4 comments

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 : )

kfcaio avatar Jun 07 '21 15:06 kfcaio

Hi @kfcaio I'm curious what you ended up doing regarding this issue?

seperman avatar Sep 21 '21 05:09 seperman

@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 avatar Sep 21 '21 13:09 kfcaio

@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 avatar Sep 21 '21 23:09 seperman

@seperman yes, google-cloud-logging was not necessary

kfcaio avatar Sep 22 '21 00:09 kfcaio