aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Gareth Coles <[email protected]>2018-03-10 19:13:21 +0000
committerGravatar Gareth Coles <[email protected]>2018-03-10 19:13:21 +0000
commit4075de4b1ee11d0085081316488e6d9dd5f907b4 (patch)
treef281d67326daf1993cb1c4df39d1a5259e50abcf
parentremoves gunicorn config as it is no longer being used (#39) (diff)
Logging with Papertrail
-rw-r--r--pysite/__init__.py19
-rw-r--r--pysite/constants.py5
2 files changed, 22 insertions, 2 deletions
diff --git a/pysite/__init__.py b/pysite/__init__.py
index a87e79d4..fa5aa8b8 100644
--- a/pysite/__init__.py
+++ b/pysite/__init__.py
@@ -1,7 +1,11 @@
# coding=utf-8
import logging
import os
+from logging import StreamHandler
+from logging.handlers import SysLogHandler
+import sys
+from pysite.constants import PAPERTRAIL_ADDRESS, PAPERTRAIL_PORT
# region Logging
# Get the log level from environment
@@ -12,6 +16,17 @@ if hasattr(logging, log_level):
else:
raise RuntimeError("LOG_LEVEL environment variable has an invalid value.")
-# This handler will ensure we log to stdout and stderr
-logging.basicConfig(format='[%(asctime)s] [%(levelname)s] %(message)s', level=log_level)
+logging_handlers = []
+
+if PAPERTRAIL_ADDRESS:
+ logging_handlers.append(SysLogHandler(address=(PAPERTRAIL_ADDRESS, PAPERTRAIL_PORT)))
+
+logging_handlers.append(StreamHandler(stream=sys.stderr))
+
+logging.basicConfig(
+ format="%(asctime)s pd.beardfist.com Site: | %(name)15s | %(levelname)8s | %(message)s",
+ datefmt="%b %d %H:%M:%S",
+ level=log_level,
+ handlers=logging_handlers
+)
# endregion
diff --git a/pysite/constants.py b/pysite/constants.py
index 316d550f..d9b87cb6 100644
--- a/pysite/constants.py
+++ b/pysite/constants.py
@@ -1,6 +1,7 @@
# coding=utf-8
from enum import Enum, IntEnum
+import os
class ErrorCodes(IntEnum):
@@ -43,3 +44,7 @@ ERROR_DESCRIPTIONS = {
418: "I'm a teapot, I can't make coffee. (._.)",
429: "Please don't send us that many requests."
}
+
+# PaperTrail logging
+PAPERTRAIL_ADDRESS = os.environ.get("PAPERTRAIL_ADDRESS")
+PAPERTRAIL_PORT = int(os.environ.get("PAPERTRAIL_PORT", 0))