Python logging to stackdriver

I recently deployed a python application in google app engine / container engine. When I went to check the logs, everything was logged at the “ERROR” level even though my application uses python logging properly. As far as I know there are 2 ways to fix that:

  1. Use the stackdriver client, which requires an additional dependency and somewhat binds your program to google app engine.
  2. Format the logs in a way that stackdriver can parse them, which is easily configurable.

Since I try to avoid binding my app whenever I can, I chose the latter. This solution is also rather simple. All you need to write the logs in json format with the proper keys.

  • Severity: Indicates the log level
  • Message: The message itself, which will appear as the line label.

Then you can add whatever keys you would like. Here is a working code sample.

 

import logging
import json


class StackdriverFormatter(logging.Formatter):

    def __init__(self, *args, **kwargs):
        super(StackdriverFormatter, self).__init__(*args, **kwargs)

    def format(self, record):
        return json.dumps({
            'severity': record.levelname,
            'message': record.getMessage(),
            'name': record.name
        })

if __name__ == '__main__':
    log = logging.getLogger('')
    sh = logging.StreamHandler()
    sf = StackdriverFormatter()
    sh.setFormatter(sf)
    log.addHandler(sh)
    log.setLevel(logging.DEBUG)
    log.error("This is an error")
    log.warning("This is a warning")
    log.info("I think you got it")

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.