Last active
August 19, 2020 12:24
-
-
Save peakBreaker/cd9d84f9692e72aa0f2b0714227fb1ba to your computer and use it in GitHub Desktop.
My python logging for stackdriver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Logging setup | |
To use this module, just import it to set up the logger. It should persist from import across the runtime | |
""" | |
import logging | |
# Three of my favourite formats for stackdriver | |
FORMAT1 = '{"severity": "%(levelname)s", "message": "%(message)s", "component": "%(component)s"}' | |
FORMAT2 = '{"severity": "%(levelname)s", "message": "[%(asctime)s] p%(process)s {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s"}' | |
FORMAT3 = '{"severity": "%(levelname)s", "message": "{%(pathname)s:%(lineno)d} - %(message)s"}' | |
# I prefer format3, as stackdriver had alot built in for time etc already | |
logging.basicConfig(format=FORMAT3, level='INFO') # Level is 'WARNING' by default | |
if __name__ == '__main__': | |
logger = logging.getLogger(__name__) | |
logger.info('Hello info world') | |
logger.warning('Hello warning world') | |
logger.error('Hello error world') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment