Output Colored Python Logging or Message in Terminal: A Step Guide – Python Tutorial

By | September 1, 2020

In this tutorial, we will introduce how to ouput colored python logging or message. We will use python coloredlogs library to implement it.

Activate python 3.5

We have installed python 3.5 and 3.7 in our computer. We will implement this tutorial in python 3.5.

activate py3.5

In order to understand how to install or activate python version using anaconda, you can read:

Install and Use Both Python 2 and Python 3 in Windows with Anaconda – Python Tutorial

Install coloredlogs

You can use pip install command to install.

pip install -i https://mirrors.aliyun.com/pypi/simple/ coloredlogs

python pip install coloredlogs

After we have installed python coloredlogs, we can use it to output a colored logging or message.

Import libraries

We need use python coloredlogs and logging libraries.

import coloredlogs, logging

Get a logging object and output colored logging

logger = logging.getLogger(__name__)
coloredlogs.install(level='DEBUG', logger=logger)

Then we can use logger object to output colored logging in terminal.

logger.debug('debug message')
logger.info('info message')
logger.warning('warning message')
logger.error('error message')
logger.critical('critical message')

Run this code, you will get:

Output Colored Python Logging or Message in Terminal

However, if we do not use coloredlogs, you will get:

Output Python Logging or Message without color in Terminal

Moreover, if you want to save python logging into a file, you can refer this tutorial:

Save Python Message into a Log File with logging – Deep Learning Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *