File: configuration.rst

package info (click to toggle)
python-django-guid 3.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 664 kB
  • sloc: python: 1,267; makefile: 16
file content (152 lines) | stat: -rw-r--r-- 4,131 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
*************
Configuration
*************

Once django guid has been installed, add the following to your projects' ``settings.py``:


1. Installed Apps
-----------------

Add :code:`django_guid` to your :code:`INSTALLED_APPS`:

.. code-block:: python

    INSTALLED_APPS = [
        ...
        'django_guid',
    ]



2. Middleware
-------------

Add the :code:`django_guid.middleware.guid_middleware` to your ``MIDDLEWARE``:

.. code-block:: python

    MIDDLEWARE = [
        'django_guid.middleware.guid_middleware',
        ...
     ]


It is recommended that you add the middleware at the top, so that the remaining middleware loggers include the requests GUID.

3. Logging Configuration
------------------------

Add :code:`django_guid.log_filters.CorrelationId` as a filter in your ``LOGGING`` configuration:

.. code-block:: python

    LOGGING = {
        ...
        'filters': {
            'correlation_id': {
                '()': 'django_guid.log_filters.CorrelationId',
                # You can optionally override the record field name where the guid is stored
                'correlation_id_field': 'correlation_id'
            }
        }
    }

Put that filter in your handler:

.. code-block:: python

    LOGGING = {
        ...
        'handlers': {
            'console': {
                'class': 'logging.StreamHandler',
                'formatter': 'medium',
                'filters': ['correlation_id'],
            }
        }
    }

And make sure to add the new ``correlation_id`` filter to one or all of your formatters:

.. code-block:: python

    LOGGING = {
        ...
        'formatters': {
            'medium': {
                'format': '%(levelname)s %(asctime)s [%(correlation_id)s] %(name)s %(message)s'
            }
        }
    }


If these settings were confusing, please have a look in the demo projects'
`settings.py <https://github.com/snok/django-guid/blob/master/demoproj/settings.py>`_ file for a complete example.

4. Django GUID Logger (Optional)
--------------------------------

If you wish to see the Django GUID middleware outputs, you may configure a logger for the module.
Simply add django_guid to your loggers in the project, like in the example below:

.. code-block:: python

    LOGGING = {
        ...
        'loggers': {
            'django_guid': {
                'handlers': ['console', 'logstash'],
                'level': 'WARNING',
                'propagate': False,
            }
        }
    }

This is especially useful when implementing the package, if you plan to pass existing GUIDs to the middleware, as misconfigured GUIDs will not raise exceptions, but will generate warning logs.


5. Django Management commands (Optional)
----------------------------------------

There is no obvious entry point for Django management commands so GUIDs are not added to code within those commands by default.
If you wish to add GUIDs to logs generated from management commands, you can use a custom base class, such as this example:

.. code-block:: python

    from django.core.management import BaseCommand
    from django_guid.context import guid
    from django_guid.utils import generate_guid


    class CommandWithGUID(BaseCommand):

        def handle(self, *args, **options):
            guid.set(generate_guid())
            self.do_task(*args, **options)

        def do_task(self, *args, **options):
            """
            The actual logic of the command. Subclasses must implement
            this method.
            """
            raise NotImplementedError(
                "subclasses of CommandWithGUID must provide a do_task() method"
            )


You can then inherit from this when creating your own management commands and all logs will have a GUID available.
For example, if you have the class above in a file `command.py` in your `home` app:

.. code-block:: python

    from home.command import CommandWithGUID


    class MyCommandWithGUID(CommandWithGUID):

        def do_task(self, *args, **options):
            """
            Add the code for your custom command here
            """
            do_something()