Skip to content

Instantly share code, notes, and snippets.

@jmorenoamor
Created June 18, 2020 08:37
Show Gist options
  • Save jmorenoamor/0c05bad13f9b52cc7f4afac6f671c24c to your computer and use it in GitHub Desktop.
Save jmorenoamor/0c05bad13f9b52cc7f4afac6f671c24c to your computer and use it in GitHub Desktop.
Python logging setup
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