You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<SubHeadingcolor="#25c2a0">Learn how to DENUG Django and activate Logging</SubHeading>
8
+
<SubHeadingcolor="#25c2a0">Learn how to DBUG Django and activate Logging</SubHeading>
9
9
10
-
Logging and Debugging are essential aspects of any web application, as it allows developers to record and analyze the execution of their code, making it easier to identify and resolve issues. In a Django application, proper logging can not only save valuable time during development and debugging but also help you ensure the stability and security of your application in production.
10
+
**Logging** and **Debugging** are essential aspects of any web application, as it allows developers to record and analyze the execution of their code, making it easier to identify and resolve issues.
11
+
In a **Django** application, proper logging can not only save valuable time during development and debugging but also help you ensure the stability and security of your application in production.
11
12
12
13
In this article, we will discuss the importance of logging and debugging in Django applications. Our journey will be divided into three key sections:
13
14
14
-
* Configuring Django for Logging
15
+
*✅ Configuring Django for Logging
15
16
16
-
* Using `pdb` in a Django Application
17
-
18
-
* Configuring Django Debug Toolbar
17
+
* ✅ Using `pdb` in a Django Application
19
18
19
+
* ✅ Configuring Django Debug Toolbar
20
20
21
21
So, let's dive in and explore the world of Django logging!
22
22
23
+
<br />
24
+
23
25
## Project Setup
24
26
25
-
For this article, you can find the code on GitHub at [https://github.com/app-generator/sample-django-debug](https://github.com/app-generator/sample-django-debug).
27
+
For this article, you can find the code on GitHub at **[How to DEBUG Django Sample](https://github.com/app-generator/sample-django-debug)**.
26
28
27
29
To make things quicker, you can clone this specific commit so you can follow the tutorial.
Once the project is set up, you can install the dependencies and start the Django project.
@@ -43,9 +44,12 @@ $ python manage.py migrate
43
44
$ python manage.py runserver
44
45
```
45
46
47
+
<br />
48
+
46
49
## Configuring Django for Logging
47
50
48
-
Django natively integrates logging using Python's built-in logging module. This integration allows developers to easily track events, monitor application behavior, and diagnose issues. Django allows you principally to use the default logging system or to create your custom loggers.
51
+
Django natively integrates logging using Python's built-in logging module. This integration allows developers to easily track events, monitor application behavior, and diagnose issues.
52
+
Django allows you principally to use the default logging system or to create your custom loggers.
49
53
50
54
***Default logging settings**: Django provides a default logging configuration that is applied if you don't explicitly configure logging in your `settings.py`. This default configuration logs messages with the `WARNING` level and above to the console. In `DEBUG` mode, Django also logs SQL queries.
51
55
@@ -54,6 +58,8 @@ Django natively integrates logging using Python's built-in logging module. This
54
58
55
59
We will write our configuration for Logging in the next lines.
56
60
61
+
<br />
62
+
57
63
### Custom Logging Settings
58
64
59
65
As stated in the previous section, customizing the `Logging` in Django add the following configuration in `core/settings.py`.
@@ -99,46 +105,45 @@ LOGGING = {
99
105
100
106
Let's explain the code we have above :
101
107
102
-
1. General configuration
103
-
108
+
> 👉 `General configuration`
104
109
105
-
*`version`: `1`
110
+
***version**: `1`
106
111
107
112
* The version key is set to 1, indicating that this is a version 1 schema for logging configuration, which is the default and only version available for the logging configuration in Python's logging module.
108
113
109
-
*`disable_existing_loggers`: `False`
114
+
***disable_existing_loggers**: `False`
110
115
111
116
* This setting controls whether or not to disable all existing loggers when the configuration is applied. By setting it to `False`, the existing loggers are enabled.
112
117
113
118
114
-
1.Formatters
119
+
> 👉 **Formatters**
115
120
116
121
117
-
*`default`
122
+
***default**
118
123
119
124
* A custom formatter with the format string `"%(asctime)s %(name)-12s %(levelname)-8s %(message)s"`. This format includes a timestamp, logger name, log level, and the log message.
120
125
121
-
*`django.server`
126
+
***django.server**
122
127
123
128
* This formatter is the same as the default formatter for the `django.server` logger.
124
129
125
130
126
-
1.Handlers
131
+
> 👉 **Handlers**
127
132
128
133
129
-
*`apps_home`
134
+
***apps_home**
130
135
131
136
* A file handler that logs messages with a log level of `DEBUG` and above to a file named `logs/apps_home.log`.
132
137
133
-
*`console`
138
+
***console**
134
139
135
140
* A stream handler logs messages with a log level of `DEBUG` and above to the console (standard output). This handler uses the `default` formatter defined earlier.
136
141
137
142
138
-
1.Loggers
143
+
> 👉 **Loggers**
139
144
140
145
141
-
*`apps.home`
146
+
***apps.home**
142
147
143
148
* A logger for the `apps.home` module (replace this with the actual name of your application or module). This logger has two handlers, `apps_home` and `console`, which means it will log messages to both the specified log file and the console. The log level is set to `DEBUG`, so it will log messages with a level of `DEBUG` and above. The `propagate` key is set to `False`, which means that the log messages handled by this logger will not be passed to higher-level loggers (e.g., the root logger or the `django` logger).
144
149
@@ -191,7 +196,7 @@ This code is part of a Django view module that demonstrates how to use the custo
191
196
192
197
Visit the project at this link : `http://localhost:8000/test_logs` and you will see the current result
And you can check the `logs/apps_home.log` and you will see the content.
197
202
@@ -203,9 +208,12 @@ And we have just logged our first error. Using the logging feature integrated wi
203
208
204
209
In the next section, we will explore how to use `pdb` for debugging in a Django application.
205
210
206
-
## Using pdb for Debugging in Django Application
211
+
<br />
207
212
208
-
`pdb`, short for "Python Debugger", is a built-in Python module that provides a way to interactively debug your code. You can use `pdb` in a Django application to debug your views, models, or other code within the app. We will use `pdb` in the `test_logs` view by adding `pdb.set_trace()` after the line `users = User.objects.all()`.
213
+
## Using **PDB** for Debugging
214
+
215
+
`pdb`, short for "Python Debugger", is a built-in Python module that provides a way to interactively debug your code.
216
+
You can use `pdb` in a Django application to debug your views, models, or other code within the app. We will use `pdb` in the `test_logs` view by adding `pdb.set_trace()` after the line `users = User.objects.all()`.
209
217
210
218
```python
211
219
...
@@ -216,9 +224,11 @@ def test_logs(request):
216
224
...
217
225
```
218
226
219
-
The line `pdb.set_trace()` is used to create a breakpoint in your Python code when using the `pdb` debugger. When the code execution reaches this line, it will pause and open an interactive debugging session in your terminal or console.
227
+
The line `pdb.set_trace()` is used to create a breakpoint in your Python code when using the `pdb` debugger.
228
+
When the code execution reaches this line, it will pause and open an interactive debugging session in your terminal or console.
220
229
221
-
At this point, you can interact with the debugger using various commands to inspect the variables, step through the code, execute arbitrary code, and more. This helps you identify and fix issues in your code more efficiently by providing an interactive debugging environment.
230
+
At this point, you can interact with the debugger using various commands to inspect the variables, step through the code, execute arbitrary code, and more.
231
+
This helps you identify and fix issues in your code more efficiently by providing an interactive debugging environment.
222
232
223
233
Here are some useful `pdb` commands you can use during the debugging session:
224
234
@@ -239,13 +249,15 @@ Here are some useful `pdb` commands you can use during the debugging session:
239
249
*`h` (help): Show a list of available commands.
240
250
241
251
242
-
Visit this URL again [http://localhost:8000/test\_logs](http://localhost:8000/test_logs) and go on the terminal when you are running the Django server and let's type some commands. You can check the results of these commands typed in the image below.
252
+
Visit this URL again [http://localhost:8000/test\_logs](http://localhost:8000/test_logs) and go on the terminal when you are running the Django server and let's type some commands.
253
+
You can check the results of these commands typed in the image below.
To quit the interactive debugging terminal, just type `q` and Django will continue the execution of the code.
247
258
248
-
Pdb is a great tool you can use in development to leverage your debugging requirements in a Django application. However, it's not recommended to use `pdb.set_trace()` in a live, production environment, as it may cause the application to hang when it reaches the breakpoint. Use `pdb` only during development and testing.
259
+
Pdb is a great tool you can use in development to leverage your debugging requirements in a Django application.
260
+
However, it's not recommended to use `pdb.set_trace()` in a live, production environment, as it may cause the application to hang when it reaches the breakpoint. Use `pdb` only during development and testing.
249
261
250
262
> If you're using Python 3.7 or later, you can use the built-in `breakpoint()` function instead of `pdb.set_trace()`. To do this, simply replace the `pdb.set_trace()` line with:
251
263
>
@@ -255,13 +267,18 @@ Pdb is a great tool you can use in development to leverage your debugging requir
255
267
>
256
268
> By default, this will invoke `pdb`, but it can be customized to use other debuggers if needed.
257
269
258
-
We now understand how to use `pdb` to debug in our Django application. However, you might also want to have real-time data and a better description of errors when building a web application or an APIwith Django. In the next section, we will learn why and how to use `django-debug-toolbar`.
270
+
We now understand how to use `pdb` to debug in our Django application.
271
+
However, you might also want to have real-time data and a better description of errors when building a web application or an APIwith Django.
272
+
In the next section, we will learn why and how to use `django-debug-toolbar`.
273
+
274
+
<br />
259
275
260
-
## Integrating django-debug-toolbar
276
+
## Integrating `django-debug-toolbar`
261
277
262
278
As a developer, you might have already faced issues such as performance issues, SQL queries, template rendering issues, and more, all within the context of your application.
263
279
264
-
[**Django Debug Toolbar**](https://django-debug-toolbar.readthedocs.io/en/latest/) is a third-party package that provides a set of panels displaying various debug information about the current request/response in your Django application. It is an essential tool for Django developers and it can help you with the following issues:
280
+
[**Django Debug Toolbar**](https://django-debug-toolbar.readthedocs.io/en/latest/) is a third-party package that provides a set of panels displaying various debug information about the current request/response in your Django application.
281
+
It is an essential tool for Django developers and it can help you with the following issues:
265
282
266
283
1. **Performance analysis**: It allows you to analyze the performance of your views, identifying potential bottlenecks and slow database queries.
267
284
@@ -302,7 +319,8 @@ INSTALLED_APPS = [
302
319
...
303
320
```
304
321
305
-
The next step is to add the middleware. In the `settings.py`file, add `'debug_toolbar.middleware.DebugToolbarMiddleware'` to the `MIDDLEWARE`list. Ensure it is placed after `'django.middleware.common.CommonMiddleware'`.
322
+
The next step is to add the middleware. In the `settings.py`file, add `'debug_toolbar.middleware.DebugToolbarMiddleware'` to the `MIDDLEWARE`list.
323
+
Ensure it is placed after `'django.middleware.common.CommonMiddleware'`.
306
324
307
325
```python
308
326
...
@@ -315,7 +333,8 @@ MIDDLEWARE = [
315
333
...
316
334
```
317
335
318
-
We can then configure the internal IPs and add some configuration for the js files. The toolbar will only be shown for clients connecting from specific IP addresses. In your settings.py file, set the `INTERNAL_IPS` setting to include your IP address or use `'127.0.0.1'`and`localhost`for local development:
336
+
We can then configure the internal IPs and add some configuration for the js files. The toolbar will only be shown for clients connecting from specific IP addresses.
337
+
In your settings.py file, set the `INTERNAL_IPS` setting to include your IP address or use `'127.0.0.1'`and`localhost`for local development:
319
338
320
339
```python
321
340
INTERNAL_IPS = [
@@ -369,8 +388,17 @@ And we are done.🚀 We have just integrated `django-debug-toolbar` in the Djang
369
388
370
389
Coupled with`pdb`and`logging`, you have a set of powerful tools you can use for debugging in your Django application.
371
390
391
+
<br />
392
+
372
393
## Conclusion
373
394
374
-
In this article, we have talked about `Django Debug Toolbar`, `pdb`, and`logging` which are essential tools for efficient Django application development. Utilizing these tools, you can identify performance bottlenecks, debug code issues, and optimize database queries with ease.
395
+
In this article, we have talked about `Django Debug Toolbar`, `pdb`, and`logging` which are essential tools for efficient Django application development.
396
+
Utilizing these tools, you can identify performance bottlenecks, debug code issues, and optimize database queries with ease.
397
+
398
+
<br />
399
+
400
+
## Resources
375
401
376
-
You can find the code in this article on [https://github.com/app-generator/sample-django-debug](https://github.com/app-generator/sample-django-debug).
402
+
- 👉 [How to DEBUG Django](https://github.com/app-generator/sample-django-debug) - the sample updated for this tutorial
403
+
- 👉 Free [Support](https://appseed.us/support/) via Email & Discord
404
+
- 👉 [Custom Development Services](https://appseed.us/custom-development/) provided by experts
0 commit comments