# How To Disable Logging While Running Django Unit Tests?

## By Disabling Tests At The Start Of The Application

Suppose you want to do it the quick way. In that case, the following line of
code will disable any log messages less severe or equal to `CRITICAL`:

```python
logging.disable(logging.CRITICAL)
```

## In settings.py Globally

You can also paste these lines of code in your `settings.py` file, so you do not
need to paste the first line multiple times:

```python
import sys
import logging

if len(sys.argv) > 1 and sys.argv[1] == 'test':
    logging.disable(logging.CRITICAL)
```

`sys.argv` list contains the command-line arguments passed to the script. If
some tests are found, logging will be disabled.

## Using Filters

While this will not disable the logging globally, you will have more options to
go with while developing your application. Create your filter in
the `settings.py` file:

```python
from logging import Filter

class NotInTestingFilter(Filter):

    def filter(self, record):
        from django.conf import settings

        return not settings.TESTING_MODE
```

In the same file, create a variable in which you will say to the program if you
are running in the testing mode:

```python
TESTING_MODE = True
```

Then add this filter to your logging configuration:

```python
'filters': {
        'testing': {
            '()': NotInTestingFilter
        }
    },
```

And assign it to a handler:

```python
'filters': ['testing']
```

[summary]
## 🔭 Want to centralize and monitor your python logs?
Go to [Logtail](https://betterstack.com/logtail/) and start your log management in 5 minutes.
[/summary]
![Better Uptime Dashboard](https://imagedelivery.net/xZXo0QFi-1_4Zimer-T0XQ/08b5b6cb-f57b-4ff4-cf1d-b303b8a94e00/public =1247x768)