Although this is not quite what the OP wanted (each line colored by level), I wanted to share a nice alternative for log output coloring called rich
- an awesome library for various rich text stuff to display in terminal, authored by @will-mcgugan.
Simple example
Activating rich
coloring for Django logs is easy: just use rich.logging.RichHandler
instead of logging.StreamHandler
, e.g.
# project/settings.py
...
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'rich.logging.RichHandler', # <-- this
},
},
'root': {
'handlers': ['console'],
'level': 'INFO',
},
'loggers': {
'django': {
'handlers': ['console'],
'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
'propagate': False,
},
},
}
(this is the modified example from Django logging docs). This will produce terminal logs like these ones:

Changing output format
Format customizations are done as usual via passing the handler arguments. E.g. to turn on rich tracebacks and hide the timestamp column:
LOGGING = {
...
'handlers': {
'console': {
'class': 'rich.logging.RichHandler',
'rich_tracebacks': True,
'show_time': False,
},
},
}
will yield

Changing output colors
Changing the coloring is possible, but not via logging config as multiple colors and different styles are applied to each line; you will have to provide a customized theme for that. Example that changes coloring of INFO
label from default blue to bold magenta:
import rich
import rich.theme
my_theme = rich.theme.Theme({
'logging.level.info': 'bold magenta',
})
rich.reconfigure(theme=my_theme)
LOGGING = {
... # no changes here
}
For more details, see Styles documentation. To inspect available theme keys and default values, issue
$ python -mrich.theme
and look for keys prefixed with log.
or logging.
.
Outro
Note that rich
is so much more than just colored logging, go check it out:
$ python -mpip install rich
$ python -mrich
Specifically for the logging use case, check out the output of
$ python -mrich.logging
to see more rendering examples than in the screenshots.