colorama icon indicating copy to clipboard operation
colorama copied to clipboard

Autoreset doesn't work inside logging

Open Benjamin-Lee opened this issue 6 years ago • 3 comments

I have to use

logging.info(Fore.GREEN + f"Protocol received: {protocol}" + Style.RESET_ALL)

because

init(autoreset=True)
...
logging.info(Fore.GREEN + f"Protocol received: {protocol}")

does not actually auto reset

Benjamin-Lee avatar Jun 28 '18 06:06 Benjamin-Lee

Can you please post a full snippet with your logging configuration and where you see the colors after the log?

This works correctly for me ('b' is printed without color):

import logging
import colorama
colorama.init(autoreset=True)
logging.basicConfig(level=logging.INFO)
logging.info(colorama.Fore.RED + "a")
print('b')

Make sure the steam you log into is wrapped. By default, colorama wraps stdout and stderr, so if you log into a different stream (e.g. a file), colorama will not be involved other than outputting the color sequences.

wiggin15 avatar Jul 01 '18 12:07 wiggin15

I ran into a similar issue. The issues turns up depending on what the target it. it works fine for console output. If you do something like set a file name, then coloring gets a little weird

import logging
from colorama import Fore, Style, init, deinit

init(autoreset=True)

logging.basicConfig(filename='test.log', level=logging.DEBUG)

logger = logging.getLogger('dev')
logger.setLevel(logging.DEBUG)

logger.debug(Fore.GREEN + 'This is a debug message')
logger.info(Fore.BLUE + 'This is an info message')
logger.warning(Fore.YELLOW + 'This is a warning message')
logger.error(Fore.LIGHTRED_EX + 'This is an error message')

If you remove the filename attribute and just log to the console, it looks fine. I guess colorlog would be a better alternative. it uses colorama under the covers

cecilphillip avatar Oct 18 '19 20:10 cecilphillip

I've tried @wiggin15 's code.

Under Mac terminal, it always looks good.

In windows, it still display right. But, if add some format of logging, like:

logging.basicConfig(level=logging.INFO,
                    format=('[%(asctime)s] %(message)s'),
                    datefmt='%m-%d %H:%M:%S')

Display would be wrong, like ' [31ma', has not been transform.

"coloredlogs" changes too many for me, so is that some way to fix it?

Erimus-Koo avatar Nov 07 '19 11:11 Erimus-Koo