diff options
author | 2018-03-13 15:08:54 +0000 | |
---|---|---|
committer | 2018-03-13 15:08:54 +0000 | |
commit | 5fe8d6061b73e17ccf4feed85c71fcac0960c119 (patch) | |
tree | 569b781a135f39fdbc76d32338d150fa649eadaf /pysite | |
parent | Remove unused import (diff) |
Datadog TCP logging
Diffstat (limited to 'pysite')
-rw-r--r-- | pysite/__init__.py | 14 | ||||
-rw-r--r-- | pysite/constants.py | 4 | ||||
-rw-r--r-- | pysite/logs.py | 11 |
3 files changed, 27 insertions, 2 deletions
diff --git a/pysite/__init__.py b/pysite/__init__.py index f807eaf4..cc80b075 100644 --- a/pysite/__init__.py +++ b/pysite/__init__.py @@ -5,22 +5,32 @@ from logging import StreamHandler from logging.handlers import SysLogHandler import sys -from pysite.constants import PAPERTRAIL_ADDRESS, PAPERTRAIL_PORT +from logmatic import JsonFormatter + +from pysite.constants import DATADOG_ADDRESS, DATADOG_PORT, PAPERTRAIL_ADDRESS, PAPERTRAIL_PORT +from pysite.logs import NonPicklingSocketHandler # region Logging # Get the log level from environment + log_level = os.environ.get("LOG_LEVEL", "info").upper() if hasattr(logging, log_level): log_level = getattr(logging, log_level) else: - raise RuntimeError("LOG_LEVEL environment variable has an invalid value.") + raise RuntimeError(f"LOG_LEVEL environment variable has invalid value: {log_level}") logging_handlers = [] if PAPERTRAIL_ADDRESS: logging_handlers.append(SysLogHandler(address=(PAPERTRAIL_ADDRESS, PAPERTRAIL_PORT))) +if DATADOG_ADDRESS: + datadog_handler = NonPicklingSocketHandler(host=DATADOG_ADDRESS, port=DATADOG_PORT) + datadog_handler.formatter = JsonFormatter(datefmt="%b %d %H:%M:%S") + + logging_handlers.append(datadog_handler) + logging_handlers.append(StreamHandler(stream=sys.stderr)) logging.basicConfig( diff --git a/pysite/constants.py b/pysite/constants.py index d6ed489f..c84ca245 100644 --- a/pysite/constants.py +++ b/pysite/constants.py @@ -48,3 +48,7 @@ ERROR_DESCRIPTIONS = { # PaperTrail logging PAPERTRAIL_ADDRESS = os.environ.get("PAPERTRAIL_ADDRESS") or None PAPERTRAIL_PORT = int(os.environ.get("PAPERTRAIL_PORT") or 0) + +# DataDog logging +DATADOG_ADDRESS = os.environ.get("DATADOG_ADDRESS") or None +DATADOG_PORT = int(os.environ.get("DATADOG_PORT") or 0) diff --git a/pysite/logs.py b/pysite/logs.py new file mode 100644 index 00000000..301cb98b --- /dev/null +++ b/pysite/logs.py @@ -0,0 +1,11 @@ +# coding=utf-8 +from logging.handlers import SocketHandler + + +class NonPicklingSocketHandler(SocketHandler): + def emit(self, record): + try: + s = self.formatter.format(record).encode() + b"\n" + self.send(s) + except Exception: + self.handleError(record) |