Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoreset doesn't work inside logging #179

Open
Benjamin-Lee opened this issue Jun 28, 2018 · 3 comments
Open

Autoreset doesn't work inside logging #179

Benjamin-Lee opened this issue Jun 28, 2018 · 3 comments

Comments

@Benjamin-Lee
Copy link

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

@wiggin15
Copy link
Collaborator

wiggin15 commented Jul 1, 2018

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.

@cecilphillip
Copy link

cecilphillip commented Oct 18, 2019

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

@Erimus-Koo
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants