How To Color Python Logging Output?
Without External Module
Create a new custom formatter:
class CustomFormatter(logging.Formatter):
then create variables for the colors. They are created as ASCII code for an escape character followed by appropriate code sequence:
grey = "\\x1b[38;21m"
yellow = "\\x1b[33;21m"
red = "\\x1b[31;21m"
bold_red = "\\x1b[31;1m"
reset = "\\x1b[0m"
format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
You can discover all the possible colors here. Also specify the provided variable format.
After that, create a dictionary and specify a format for each of the log levels - color, your format of the log message, and reset the color at the end. Finally, make your formatter return this custom format:
FORMATS = {
logging.DEBUG: grey + format + reset,
logging.INFO: grey + format + reset,
logging.WARNING: yellow + format + reset,
logging.ERROR: red + format + reset,
logging.CRITICAL: bold_red + format + reset
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)
And the whole formatter:
class CustomFormatter(logging.Formatter):
grey = "\\x1b[38;21m"
yellow = "\\x1b[33;21m"
red = "\\x1b[31;21m"
bold_red = "\\x1b[31;1m"
reset = "\\x1b[0m"
format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
FORMATS = {
logging.DEBUG: grey + format + reset,
logging.INFO: grey + format + reset,
logging.WARNING: yellow + format + reset,
logging.ERROR: red + format + reset,
logging.CRITICAL: bold_red + format + reset
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)
You can later assign this formatter to a handler:
handler.setFormatter(CustomFormatter())
Using External Module
For this, you can use an external module named colorlog
. You can download it
by running this command in the terminal:
pip install colorlog
You can read more about this module here.
After you installed the module, do not forget to import it:
import colorlog
Now using colorlog
, create a handler using the class StreamHandler()
. Then
create a logger and assign this handler to it:
handler = colorlog.StreamHandler()
logger = colorlog.getLogger(__name__)
logger.addHandler(handler)
Now set a format for the handler. At the start, specify the desired color as an attribute and create some logging message:
dashboards with Grafana.
-
How To Disable Logging From The Python Request Library?
You can change the log level of the logger taking care of these messages. Setting the level to WARNING will remove the request messages and keep warnings and errors: python import logging logging.g...
Questions -
How to Log to Stdout with Python?
Using Basic Configuration Python, by default, logs to a console. You can call the function on the module: python import logging logging.warning("Warning.") OUTPUT WARNING:root:Warning. Python alre...
Questions -
How to verify SSL certificates on the command line?
To validate an SSL certificate you can use one of the following approaches, depending on the type of the certificate.
Questions -
How To Write Logs To A File With Python?
Using Basic Configuration You can use basic config. If you configure the attribute filename, logs will be automatically saved to the file you specify. You can also configure the attribute filemode....
Questions
We are hiring.
Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.
Help us in making the internet more reliable.

Help us with developer education and get paid.

Reliability is the
ultimate feature
Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.
Explore Better Stack
