Skip to content

Commit

Permalink
Setup structlog for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jpadilla committed Apr 11, 2019
1 parent 0b641bd commit 4c93f8e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .isort.cfg
Expand Up @@ -6,4 +6,4 @@ force_grid_wrap=0
combine_as_imports=True
line_length=88
skip=.venv,migrations
known_third_party=celery,configurations,coreapi,coreschema,debug_toolbar,django,django_extensions,django_filters,django_s3_storage,google,pytest,pytz,requests,rest_framework
known_third_party=celery,configurations,coreapi,coreschema,debug_toolbar,django,django_extensions,django_filters,django_s3_storage,google,pytest,pytz,requests,rest_framework,structlog
10 changes: 5 additions & 5 deletions Pipfile
Expand Up @@ -8,15 +8,15 @@ black = "*"
isort = "==4.3.4"
"flake8" = "*"
pre-commit = "*"
seed-isort-config = "*"
seed-isort-config = "==1.6.0"
pytest-django = "*"

[packages]
dj-database-url = "==0.5.0"
django-configurations = "==2.1"
django-debug-toolbar = "==1.11"
django-extensions = "==2.1.5"
Django = "==2.1.7"
django-extensions = "==2.1.6"
Django = "==2.2"
gunicorn = "==19.9.0"
whitenoise = "==4.1.2"
"psycopg2-binary" = "==2.7.7"
Expand All @@ -32,9 +32,9 @@ coreapi = "==2.3.3"
pygments = "==2.3.0"
markdown = "==3.0.1"
django-cors-headers = "==2.4.0"
django-redis = "*"
django-redis = "==4.10.0"
celery = {extras = ["redis"],version = "==4.2.1"}
seed-isort-config = "==1.6.0"
structlog = "==19.1.0"

[requires]
python_version = "3.7"
Expand Down
50 changes: 18 additions & 32 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 51 additions & 8 deletions contratospr/settings.py
Expand Up @@ -7,10 +7,40 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""
import logging.config
import os

import structlog
from configurations import Configuration, values

LOGGING_CONFIG = None
logging.config.dictConfig(
{
"version": 1,
"disable_existing_loggers": False,
"handlers": {"console": {"class": "logging.StreamHandler"}},
"loggers": {
"": {"level": "WARNING", "handlers": ["console"], "formatter": "default"},
"contratospr": {
"level": "INFO",
"handlers": ["console"],
"propagate": False,
},
"requests": {
"level": "WARNING",
"handlers": ["console"],
"propagate": False,
},
"urllib3": {
"level": "WARNING",
"handlers": ["console"],
"propagate": False,
},
"celery": {"level": "INFO", "handlers": ["console"], "propagate": False},
},
}
)


class Common(Configuration):
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
Expand Down Expand Up @@ -174,9 +204,9 @@ class Development(Common):
INTERNAL_IPS = ["127.0.0.1"]


class Staging(Common):
class Production(Common):
"""
The in-staging settings.
The in-production settings.
"""

# Security
Expand All @@ -190,12 +220,6 @@ class Staging(Common):
SECURE_SSL_REDIRECT = values.BooleanValue(True)
SECURE_PROXY_SSL_HEADER = values.TupleValue(("HTTP_X_FORWARDED_PROTO", "https"))


class Production(Staging):
"""
The in-production settings.
"""

AWS_REGION = values.Value("us-east-1", environ_prefix=None)
AWS_ACCESS_KEY_ID = values.SecretValue(environ_prefix=None)
AWS_SECRET_ACCESS_KEY = values.SecretValue(environ_prefix=None)
Expand All @@ -212,3 +236,22 @@ def CACHES(self):
"OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient"},
}
}


structlog.configure(
processors=[
structlog.stdlib.filter_by_level,
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.UnicodeDecoder(),
structlog.processors.KeyValueRenderer(),
],
context_class=structlog.threadlocal.wrap_dict(dict),
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)

0 comments on commit 4c93f8e

Please sign in to comment.