Skip to content
This repository was archived by the owner on Nov 5, 2025. It is now read-only.

Commit 4c93f8e

Browse files
committed
Setup structlog for logging
1 parent 0b641bd commit 4c93f8e

File tree

4 files changed

+75
-46
lines changed

4 files changed

+75
-46
lines changed

.isort.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ force_grid_wrap=0
66
combine_as_imports=True
77
line_length=88
88
skip=.venv,migrations
9-
known_third_party=celery,configurations,coreapi,coreschema,debug_toolbar,django,django_extensions,django_filters,django_s3_storage,google,pytest,pytz,requests,rest_framework
9+
known_third_party=celery,configurations,coreapi,coreschema,debug_toolbar,django,django_extensions,django_filters,django_s3_storage,google,pytest,pytz,requests,rest_framework,structlog

Pipfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ black = "*"
88
isort = "==4.3.4"
99
"flake8" = "*"
1010
pre-commit = "*"
11-
seed-isort-config = "*"
11+
seed-isort-config = "==1.6.0"
1212
pytest-django = "*"
1313

1414
[packages]
1515
dj-database-url = "==0.5.0"
1616
django-configurations = "==2.1"
1717
django-debug-toolbar = "==1.11"
18-
django-extensions = "==2.1.5"
19-
Django = "==2.1.7"
18+
django-extensions = "==2.1.6"
19+
Django = "==2.2"
2020
gunicorn = "==19.9.0"
2121
whitenoise = "==4.1.2"
2222
"psycopg2-binary" = "==2.7.7"
@@ -32,9 +32,9 @@ coreapi = "==2.3.3"
3232
pygments = "==2.3.0"
3333
markdown = "==3.0.1"
3434
django-cors-headers = "==2.4.0"
35-
django-redis = "*"
35+
django-redis = "==4.10.0"
3636
celery = {extras = ["redis"],version = "==4.2.1"}
37-
seed-isort-config = "==1.6.0"
37+
structlog = "==19.1.0"
3838

3939
[requires]
4040
python_version = "3.7"

Pipfile.lock

Lines changed: 18 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contratospr/settings.py

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,40 @@
77
For the full list of settings and their values, see
88
https://docs.djangoproject.com/en/2.1/ref/settings/
99
"""
10+
import logging.config
1011
import os
1112

13+
import structlog
1214
from configurations import Configuration, values
1315

16+
LOGGING_CONFIG = None
17+
logging.config.dictConfig(
18+
{
19+
"version": 1,
20+
"disable_existing_loggers": False,
21+
"handlers": {"console": {"class": "logging.StreamHandler"}},
22+
"loggers": {
23+
"": {"level": "WARNING", "handlers": ["console"], "formatter": "default"},
24+
"contratospr": {
25+
"level": "INFO",
26+
"handlers": ["console"],
27+
"propagate": False,
28+
},
29+
"requests": {
30+
"level": "WARNING",
31+
"handlers": ["console"],
32+
"propagate": False,
33+
},
34+
"urllib3": {
35+
"level": "WARNING",
36+
"handlers": ["console"],
37+
"propagate": False,
38+
},
39+
"celery": {"level": "INFO", "handlers": ["console"], "propagate": False},
40+
},
41+
}
42+
)
43+
1444

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

176206

177-
class Staging(Common):
207+
class Production(Common):
178208
"""
179-
The in-staging settings.
209+
The in-production settings.
180210
"""
181211

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

193-
194-
class Production(Staging):
195-
"""
196-
The in-production settings.
197-
"""
198-
199223
AWS_REGION = values.Value("us-east-1", environ_prefix=None)
200224
AWS_ACCESS_KEY_ID = values.SecretValue(environ_prefix=None)
201225
AWS_SECRET_ACCESS_KEY = values.SecretValue(environ_prefix=None)
@@ -212,3 +236,22 @@ def CACHES(self):
212236
"OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient"},
213237
}
214238
}
239+
240+
241+
structlog.configure(
242+
processors=[
243+
structlog.stdlib.filter_by_level,
244+
structlog.stdlib.add_logger_name,
245+
structlog.stdlib.add_log_level,
246+
structlog.stdlib.PositionalArgumentsFormatter(),
247+
structlog.processors.TimeStamper(fmt="iso"),
248+
structlog.processors.StackInfoRenderer(),
249+
structlog.processors.format_exc_info,
250+
structlog.processors.UnicodeDecoder(),
251+
structlog.processors.KeyValueRenderer(),
252+
],
253+
context_class=structlog.threadlocal.wrap_dict(dict),
254+
logger_factory=structlog.stdlib.LoggerFactory(),
255+
wrapper_class=structlog.stdlib.BoundLogger,
256+
cache_logger_on_first_use=True,
257+
)

0 commit comments

Comments
 (0)