โš ๏ธ Warning: This post is over a year old, the information may be out of date.

๐Ÿ“ Django logger (my personal favourite format)

๐Ÿ“… | โฐ 1 minutes

Personally, I do lot of programming using JAVA and log4j is my favourite package that I use on my project.. but sometimes I also use PYTHON and been using DJANGO framework because of it simplicity and easier to deploy.

When I use DJANGO, I still prefer to have logger on each of importance line of code (LoC) so I can trace back what actually happened when the code are executed.

So, let share with you my simple, minimal and favourite format for DJANGO logging

# File : <project>/settings.py
# Logging
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
        },
    },
    'formatters': {
        'verbose': {
            'format': '{asctime} | [{levelname}] | {name} | {message} - {filename}:{lineno}@{funcName} | [{module}/{process:d}/{'
                      'thread:d}] - ({pathname})',
            'datefmt': '%Y-%m-%d %H:%M:%S',
            'style': '{',
        },
    },
    'root': {
        'handlers': ['console'],
        'level': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG')
    },
}

Then you use logger, you will getting something like below printed on console

2023-03-09 22:38:22 | [INFO] | django.utils.autoreload | Watching for file changes with StatReloader - autoreload.py:677@run_with_reloader | [autoreload/19692/16896] - (D:\PROJECT\WMS\robbi-hcom\venv\Lib\site-packages\django\utils\autoreload.py)
2023-03-09 22:41:09 | [INFO] | polls.views | Hello, world! - views.py:14@index | [views/19692/15420] - (D:\PROJECT\WMS\robbi-hcom\pearson\polls\views.py)
2023-03-09 22:42:23 | [INFO] | polls.views | someone vote on [<WSGIRequest: POST '/polls/1/vote/'>/1] - views.py:40@vote | [views/19692/15420] - (D:\PROJECT\WMS\robbi-hcom\pearson\polls\views.py)

Cool isn’t? Hope my sharing is helpfull for the readers.

Posted by: Hugo

Edit

Have some thoughts, discussion or feedback on this post?

๐Ÿ’ฌย Send me an email

What is webmention? How to send interactions!

Below you can find all of webmention with this page. Which means, you also can mentioned this URL on any website that support WebMention. Have you written a response to this post? Let me know the URL:

Do you use a website that don't have WebMention capabilities? You can just use Comment Parade!