Skip to content

Commit

Permalink
fixed #101 logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tomturner committed Oct 13, 2016
1 parent 1fb09d4 commit 7020e63
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
14 changes: 14 additions & 0 deletions django_tenants/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import logging

from django.db import connection


class TenantContextFilter(logging.Filter):
"""
Add the current ``schema_name`` and ``domain_url`` to log records.
Thanks to @regolith for the snippet on https://github.com/bernardopires/django-tenant-schemas/issues/248
"""
def filter(self, record):
record.schema_name = connection.tenant.schema_name
record.domain_url = getattr(connection.tenant, 'domain_url', '')
return True
3 changes: 3 additions & 0 deletions django_tenants/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ def process_request(self, request):

domain = get_object_or_404(get_tenant_domain_model().objects.select_related('tenant'),
domain=hostname)

domain.tenant.domain_url = hostname

This comment has been minimized.

Copy link
@Lobosque

Lobosque Oct 13, 2016

this line raises an AttributeError: can't set attribute error

request.tenant = domain.tenant

connection.set_tenant(request.tenant)

# Content type can no longer be cached as public and tenant schemas
Expand Down
34 changes: 34 additions & 0 deletions docs/use.rst
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,40 @@ The hook for ensuring the ``search_path`` is set properly happens inside the ``D
When set, ``django-tenants`` will set the search path only once per request. The default is ``False``.


Logging
-------

The optional ``TenantContextFilter`` can be included in ``settings.LOGGING`` to add the current ``schema_name`` and ``domain_url`` to the logging context.

.. code-block:: python
# settings.py
LOGGING = {
'filters': {
'tenant_context': {
'()': 'django_tenants.log.TenantContextFilter'
},
},
'formatters': {
'tenant_context': {
'format': '[%(schema_name)s:%(domain_url)s] '
'%(levelname)-7s %(asctime)s %(message)s',
},
},
'handlers': {
'console': {
'filters': ['tenant_context'],
},
},
}
This will result in logging output that looks similar to:

.. code-block:: text
[example:example.com] DEBUG 13:29 django.db.backends: (0.001) SELECT ...
Running in Development
----------------------

Expand Down

0 comments on commit 7020e63

Please sign in to comment.