0

I wanted to have colored log files for one of my programs. So I tried utilizing colored logging from one of answers on stackoverflow questions, and it worked great in the python terminal but the log file showed strange characters and no color as shown below.

In Terminal :

Terminal

In Log File :

Log File

I tried many ways , but still am getting the same .

Can anyone help me , Below is my code .

import coloredlogs
import logging

# Create a logger object.
logger = logging.getLogger(__name__)

# Create a filehandler object
fh = logging.FileHandler('spam.log')
fh.setLevel(logging.DEBUG)

# Create a ColoredFormatter to use as formatter for the FileHandler
formatter = coloredlogs.ColoredFormatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)

# Install the coloredlogs module on the root logger
coloredlogs.install(level='DEBUG')

logger.debug("this is a debugging message")
logger.info("this is an informational message")
logger.warning("this is a warning message")
logger.error("this is an error message")
logger.critical("this is a critical message")
Markus
  • 5,976
  • 5
  • 6
  • 21
  • Do not put links to external figures. So please include in the question or describe them. In any case: which terminal are you using? How it is configured? (so what it is the TERM env variable)? Which operating system? It is probably an external factor. – Giacomo Catenazzi Sep 21 '22 at 14:42
  • 2
    Please check if `less -R your_logfile.log` shows the colours as desired. – Markus Sep 21 '22 at 19:38

0 Answers0