Skip to content

gh-146292: Add colour to http.server logs#146293

Open
hugovk wants to merge 2 commits intopython:mainfrom
hugovk:3.15-colour-http.server
Open

gh-146292: Add colour to http.server logs#146293
hugovk wants to merge 2 commits intopython:mainfrom
hugovk:3.15-colour-http.server

Conversation

@hugovk
Copy link
Member

@hugovk hugovk commented Mar 22, 2026

Taking inspiration from the make dev logs for the new blog (https://github.com/python/python-insider-blog):

Image

Let's add colour to the http.server log output:

Image

As usual, this respects the environment variables to control colour and the output stream's capabilities.


📚 Documentation preview 📚: https://cpython-previews--146293.org.readthedocs.build/

Co-authored-by: Brian Schubert <[email protected]>
import threading
from unittest import mock
from io import BytesIO, StringIO
lazy from _colorize import get_theme
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think lazy imports are necessary.

elif code_int >= 400:
code_color = t.status_error
else:
code_color = t.status_ok
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking I would prefer that we also distinguish 300 and 100 codes as the former are redirections and the latter are informational.

Informational are less important in some sense and thus it makes sense to have another color (i would suggest white for informational and something else for 3xx, like lightblue).

I would have expected 2xx to be green though

serving: str = ANSIColors.GREEN
size: str = ANSIColors.GREY
status_ok: str = ANSIColors.BLUE
status_error: str = ANSIColors.YELLOW
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you name this client error instead of just error please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants