Unable to print logs in default color, when no color is specified explicitly using Colorama
Question: I am trying to get colored logs using colorama and logging module in python.
If I am not giving any color then it should print default terminal color, but
in my logs I am getting color of previously set logs if no color is set
explicitly.

Below is my setup_logging.yml file

    
    
    import os
    import yaml
    import logging.config
    
    
    def setup_logging(
        default_path='logging.yml', default_level=logging.INFO, env_key='LOG_CFG'):
    path = os.path.join('/etc', 'module', default_path)
        value = os.getenv(env_key, None)
    if value:
        path = value
    if os.path.exists(path):
        with open(path, 'rt') as f:
            config = yaml.load(f.read())
        logging.config.dictConfig(config)
    else:
        logging.basicConfig(level=default_level)
    

Logging.yml file

    
    
     version: 1
    
     disable_existing_loggers: True 
    
    
    formatters:
       default:
        format:  "%(asctime)s - %(name)s - %(levelname)s - \n %(message)s"
    handlers:
       console:
        class: logging.StreamHandler
        level: INFO 
        formatter: default
        stream: ext://sys.stdout
    
    info_file_handler:
        class: logging.handlers.RotatingFileHandler
        level: INFO
        formatter: default
        filename: /var/log/jsnapy/test.log
        maxBytes: 10485760 # 10MB
        backupCount: 20
        encoding: utf8
    

I have stripped my code for logging functions:

    
    
     import logging
     import colorama
    
    class Test:
    
    def __init__(self):
        self.logger = logging.getLogger(__name__)
        colorama.init(autoreset=True)
        setup_logging.setup_logging()
    
    def testing(self):
        self.logger.debug(colorama.Fore.RED + "this is a debugging message")
        self.logger.info(colorama.Fore.BLUE+"this is an informational message")
        self.logger.warn(colorama.Fore.BLUE+"this is a warning message")
        self.logger.error(colorama.Fore.YELLOW +  "this is an error message")
        self.logger.critical("this is a critical message")
    
    t = Test()
    t.testing()
    

How to get default color in logs when no color is specified explicitly.


Answer: You need to use `init(autoreset=True)` for that, as written in the official
documentation:

> If you find yourself repeatedly sending reset sequences to turn off color
> changes at the end of every print, then init(autoreset=True) will automate
> that:
    
    
    from colorama import init
    init(autoreset=True)
    print(Fore.RED + 'some red text')
    print('automatically back to default color again')
    

