[Python] How to configure python simple "logging" with different colors for different levels

Discussão em 'Python' iniciado por Stack, Fevereiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    I have created simple python script:

    import logging
    import sys

    if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG)
    print("This is a normal print statement", file=sys.stdout)
    logging.debug("debug message")
    logging.info("info message")
    logging.warning("warning message")


    I would expect that the color for the "INFO" level will be same as sysout or at least some neuetral color. But the output of the run of my script is:

    [​IMG]

    I would like the "INFO" logs to be in at least same color as the one coming from "print". I have tried to add config like this:

    logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")


    But this did not help. I do not want to setup "loggers" for my project because using simple "logging" is more convienient for me.

    In the real project which I am developing I have several classes where I use all the levels and many log messages, so this RED info logs makes the whole logs very unreadable.

    I cannot believe that its normal for the INFO log to be red, but as you see I dont have any special configs. How I can easily configre the "logging" to have proper colouring?

    Continue reading...

Compartilhe esta Página