[Python] Set the formatting style for logging - percent, format style / bracket or f-string

Discussão em 'Python' iniciado por Stack, Março 12, 2025.

  1. Stack

    Stack Membro Participativo

    Python has multiple string formatting styles for arguments:


    • Old-style/percent/%: "The first number is %s" % (1,)


    • New-style/bracket/{: "The first number is {}".format(1)


    • F-string: one = 1; f"the first number is {one}"

    I can format log messages manually at the message creation time: log.error("the first number is {}".format(1)). However this can lead to performance problems and cause bugs when formatting complex objects.

    How do I configure logging to use the style I want in individual logs (e.g. logging.error)?

    Continue reading...

Compartilhe esta Página