#๐Ÿ”’ aligning a logging format string field with ":" included

34 messages ยท Page 1 of 1 (latest)

coral solstice
#

I have a logger format string like this

iso_8601 = '%Y-%m-%dT%H:%M:%S'
fmt = "\033[34m{levelname}\033[0m:  {asctime}   {name} - {message} ({filename}:{lineno})"
formatter = logging.Formatter(fmt, iso_8601, style='{')

I want to align the level name together with the : such that the whole thing is 10 characters wide like

INFO:    ...
WARNING: ...
DEBUG:   ...

How could I do it? I know that I can do format on the level name itself, but I am not sure how with the : included

dusky obsidianBOT
#

@coral solstice

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

thorny minnow
#

Are you allowed to add a library or no?

coral solstice
#

preferably not ๐Ÿ™‚

#

but I would still like to hear it

thorny minnow
#

So what does your code currently output do you have an example?

coral solstice
#

INFO: 2026-03-25T15:22:43 api.main - App is started! (main.py:31)

thorny minnow
#

okay now I see your problem thanks

coral solstice
#

I would want it to be like

INFO:     2026-03-25T15:22:43   api.main - App is started! (main.py:31)
DEBUG:    2026-03-25T15:22:43   api.main - App is started! (main.py:31)
thorny minnow
#
"\033[34m{levelname+":":<10}\033[0m:  {asctime}   {name} - {message} ({filename}:{lineno})"

I have no idea if that will work

topaz ferry
#

It's easy to do without the colon... But the colon makes it tricky

coral solstice
#

I know that's why I asked xd

topaz ferry
thorny minnow
#

oh for some reason i thought it was an f string lol brain fart

topaz ferry
#
>>> "{a+':':12}".format(a="info")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: "a+'"

It's easy to just test such stuff in repl before suggesting

coral solstice
#

aligning a logging format string with ":" included

#

aligning a logging format string field with ":" included

#

I saw that the logs are aligned like that in fastapi so I want to do it myself too

split kayak
#

Hey,

Just use a custom logging.Formatter subclass that overrides format() to inject the padded levelname: before the ANSI codes get in the way

coral solstice
#

I guess so

thorny minnow
#

FINALLY

logging.addLevelName(logging.ERROR, "ERROR:")
fmt = "\033[34m{levelname:<10}\033[0m  {asctime}   {name} - {message} ({filename}:{lineno})"

ERROR: 2026-03-25T15:44:22 test_logger - Error message: Another standard level. (main.py:27)

#

thanks for posting this. I definitely needed to sharpen my formatting skills

neon nebula
#

as vincent suggested this works

import logging

iso_8601 = '%Y-%m-%dT%H:%M:%S'

fmt = "\033[34m{levelname:<9}\033[0m: {asctime}   {name} - {message} ({filename}:{lineno})"

logging.basicConfig(
    level=logging.DEBUG,
    format=fmt,
    datefmt=iso_8601,
    style='{'
)

logger = logging.getLogger("api.main")

logger.debug("Debug message")
logger.info("App is started!")
logger.warning("Something might be wrong")
logger.error("Something failed")
logger.critical("Critical issue!")
#

expected output

thorny minnow
#

That's not what he's looking for

neon nebula
thorny minnow
#
import logging

iso_8601 = '%Y-%m-%dT%H:%M:%S'

logging.addLevelName(logging.ERROR, "ERROR:")
logging.addLevelName(logging.WARNING, "WARNING:")
logging.addLevelName(logging.DEBUG, "DEBUG:")
logging.addLevelName(logging.INFO, "INFO:")
logging.addLevelName(logging.CRITICAL, "CRITICAL:")
fmt = "\033[34m{levelname:<10}\033[0m  {asctime}   {name} - {message} ({filename}:{lineno})"

logging.basicConfig(
    level=logging.DEBUG,
    format=fmt,
    datefmt=iso_8601,
    style='{'
)

logger = logging.getLogger("api.main")

logger.debug("Debug message")
logger.info("App is started!")
logger.warning("Something might be wrong")
logger.error("Something failed")
logger.critical("Critical issue!")
DEBUG:      2026-03-25T15:51:00   api.main - Debug message (main.py:21)
INFO:       2026-03-25T15:51:00   api.main - App is started! (main.py:22)
WARNING:    2026-03-25T15:51:00   api.main - Something might be wrong (main.py:23)
ERROR:      2026-03-25T15:51:00   api.main - Something failed (main.py:24)
CRITICAL:   2026-03-25T15:51:00   api.main - Critical issue! (main.py:25)

#

@coral solstice

coral solstice
#

you can edit the level names? wow

dusky obsidianBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.