question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Django Logging Formatting Issue

See original GitHub issue

Hi @willmcgugan,

I would just like to ask about configuration for the logging formatting/handling within Django.

Following your post on your blog, I have attempted to configure my own logging within my Django project.

There are 2 major differences between what your blog post, and indeed the project’s README displays when compared to my implementation.

For reference, your blog post’s screenshot:

image

The project’s README screenshot (albeit not Django):

image

My implementation displays as:

django-logging-with-rich

There are 2 issues with the way my logging is formatted:

  1. You will see that my logging does not have the colorised formatting.

  2. The width is somewhat constrained even though the width of my iTerm terminal window is plenty wide enough. Even when I widen it further, it remains constrained in its width.

It’s worth mentioning that my project is in Docker, so I don’t know if that’s an issue. I have also tested reviewing the development server output within the stock terminal app in Mac OS which gave me the same result.

The timed grouping (with just the time element) and the warning levels both look good so it appears the Rich config is somewhat working.

@willmcgugan For reference, do you have an exact commit reference that points exactly to the settings.py file you referred to in your blog post? Should it matter if it’s in Docker instead of just Django’s local development server?

I don’t know whether this is a config issue from my side, or whether there is a bug or missing feature from Rich itself.

My full logging config in my settings.py file is:

...

    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,

        'filters': {
            'skip_static_requests': {
                '()': 'django.utils.log.CallbackFilter',
                'callback': skip_static_requests,
            }
        },

        'formatters': {
            'rich': {'datefmt': '[%X]'},
        },

        'handlers': {
            'console': {
                'class': 'rich.logging.RichHandler',
                'formatter': 'rich',
                'level': 'INFO',
                'filters': ['skip_static_requests'],
            }
        },

        'loggers': {
            'django': {
                'handlers': ['console'],
                'level': 'INFO',
                'propagate': False,
            },
        }
    }
...

Please could you advise?

Many thanks,

Wayne

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
WayneLambertcommented, Aug 19, 2020

OK, following a bit more research, I found a solution to the issue.

There is a variable in zsh (and other Unix shells) called COLUMNS. From this discovery, I then amended the Dockerfile responsible for the dev build and added a step within it’s build process:

...
# Set the COLUMNS variable for the terminal's output width
ENV COLUMNS 164
...

Someone else may need to customise that value for their desired window width.

After the rebuild, bringing the container (dev server) back up, and widening the iTerm window, it gave me the following:

django-logging-with-rich6

Perfect!

For anyone wanting to use Django, Docker, and Rich together, I have recorded the overall solution to the issue in a commit.

Thanks for your help, @willmcgugan

1reaction
WayneLambertcommented, Aug 19, 2020

Hi Will,

Further to above and your original answer, I did some research and found the following StackOverflow post.

It suggested that I need the following key adding to my web service (for my setup) within my docker-compose.yml file:

tty: true

Following that addition, then rebuilding my images and bring up the containers again, I now have…

django-logging-with-rich3

As you can see, now I have colour, however the width still seems to be constrained to 80 chars.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Logging - Django documentation
Ultimately, a log record needs to be rendered as text. Formatters describe the exact format of that text. A formatter usually consists of...
Read more >
Logging formatters in django - python - Stack Overflow
The format string passed in the original logging call. Merged with args to produce message, or an arbitrary object (see Using arbitrary ...
Read more >
Django Logging | Tutorial & Best Practices - ContainIQ
Logging Formatters​​ Formatter objects to change them into a string-based log entry. Django leverages the potential of logging by using the Python logging...
Read more >
DJANGO LOGGING #2. JSON FORMATTING AND CUSTOM ...
Andrey Ivanov - PythonUse my discount link for OKEX crypto exchange: https://www.okx.com/join/PYTHONANDREYMy UDEMY courses: ...
Read more >
Python and Django Logging in Plain English
Python loggers are organized in a parent-child structure with a hierarchy similar to module namespacing. Every application has a main logger ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found