Skip to content

Custom, styled loglevel prefixes without markup=True #2798

Answered by dionhaefner
dionhaefner asked this question in Q&A
Discussion options

You must be logged in to vote

I solved this for my use case by calling rich.markup.escape inside a custom formatter:

from rich.markup import escape

class PrefixFormatter(logging.Formatter):
    def format(self, record: Any, *args: Any) -> Any:
        record.levelprefix = LEVEL_PREFIX.get(record.levelname, "")
        record.msg = escape(record.msg)
        return super().format(record, *args)

That way you can use markup=True but ignore control codes in messages.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@dionhaefner
Comment options

Answer selected by dionhaefner
@jmw182
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants