Created
June 18, 2020 08:37
-
-
Save jmorenoamor/0c05bad13f9b52cc7f4afac6f671c24c to your computer and use it in GitHub Desktop.
Python logging setup
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
import os | |
import logging | |
# Simple ########################################################################################## | |
logging.basicConfig(level=os.environ.get("LOGLEVEL", "DEBUG"), format=os.environ.get("LOGFORMAT", logging.BASIC_FORMAT)) | |
# One line exceptions ############################################################################# | |
class OneLineExceptionFormatter(logging.Formatter): | |
""" https://www.loggly.com/ultimate-guide/python-logging-basics/ """ | |
# def formatException(self, exc_info): | |
# result = super().formatException(exc_info) | |
# return repr(result) | |
def format(self, record): | |
result = super().format(record) | |
if record.exc_text: | |
result = result.replace("\n", " ").replace(" ", " - ") | |
return result | |
formatter = OneLineExceptionFormatter(os.environ.get("LOGFORMAT", logging.BASIC_FORMAT)) | |
handler = logging.StreamHandler() | |
handler.setFormatter(formatter) | |
logger = logging.getLogger('meross-bridge') | |
logger.setLevel(os.environ.get("LOGLEVEL", "DEBUG")) | |
logger.addHandler(handler) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment