Django Logging with VS Code

Rather than rehash the excellent official documentation on Django Logging I will demonstrate how you can add logging to an existing Django project.

settings.py

Add the following to the settings.py.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'simple': {
            'format': '{asctime} {levelname} {message}',
            'style': '{',
            'datefmt': "%d/%m/%Y %H:%M:%S"
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
    },
    'root': {
        'handlers': ['console'],
        'level': 'INFO',
    },
}

This is an example based on one used in the documentation and I have found it easier to understand by starting at the bottom. root is the catch-all logger that will handle any requests for logging. Other loggers can be added to facilitate different logging for a particular area within the project, in this example I will only be using root to keep things simple. root has two key values, handlers and level. handlers determine what happens to the logger message such sending it to the console or writing it to file. Here the message will be sent to the console handler. Every logging message has a log level which has to be equal or greater than the level for the logging message to be passed to a handler.

The console key within handlers has a key for a formatter named simple which is used to format the log message. This is defined in the top section and formats each message to include a date and time, the level such as INFO, WARNING or DEBUG and the logging message. An example of a message using this formatter is shown below:

08/12/2021 12:01:00 DEBUG useful debug message

views.py

Add the highlighted lines to a views.py file

import logging

logger = logging.getLogger(__name__)

def dpb(crs_station_code):
        
        logger.info('crs_station_code is ' + crs_station_code)
        ...

After importing logging, the next step is to get a instance of a logger. At this point logger is configured and ready to use so the final step is to add logging messages. In this example I have added a call to logger.info to capture the value of the parameter passed to the method, dpd.

Output

In VS Code console logging can be viewed using the integrated terminal.

Acknowledgements

Django documentation on Logging

This Stackoverflow question and the many answers.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.