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

the problem with the log color using the coloring function #1205

Open
1liochka1 opened this issue Sep 14, 2024 · 1 comment
Open

the problem with the log color using the coloring function #1205

1liochka1 opened this issue Sep 14, 2024 · 1 comment
Labels
question Further information is requested

Comments

@1liochka1
Copy link

1liochka1 commented Sep 14, 2024

class Color(Enum):
    scarlet = "scarlet"
    pink = "pink"
    orange = "orange"
    blue = "blue"
    purple = "purple"


from colorama import init, Fore, Style
init()

def colorize(color: Color, text: str) -> str:
    match color:
        case Color.scarlet:
            return f"{Fore.RED}{text}{Style.RESET_ALL}"
        case Color.pink:
            return f"{Fore.MAGENTA}{text}{Style.RESET_ALL}"
        case Color.blue:
            return f"{Fore.CYAN}{text}{Style.RESET_ALL}"
        case Color.orange:
            return f"{Fore.YELLOW}{text}{Style.RESET_ALL}"
        case _:
            return f"{Fore.MAGENTA}{text}{Style.RESET_ALL}"

logger.info(f'111111111111111   {colorize(Color.orange, "2222222222222")}    11111111111111111111')

hi, I'm trying to write a part of a function that will color some phrases of my blog and for some reason, when using this function, all the text that comes after using this coloring turns red. Can you help me? thanks

image

@Delgan Delgan added the question Further information is requested label Sep 22, 2024
@Delgan
Copy link
Owner

Delgan commented Sep 22, 2024

Loguru has its own internal management of argument colorization, so it's best not to mix it with another one.

When you do:

logger.info("11111   22222    11111")

Loguru will internally converts the message to (roughly):

f"{Fore.WHITE}11111   22222    11111{Style.RESET_ALL}"

With Fore.WHITE being the color of the current log level.

Now, because your logged message already cotnain Fore color instructions, the result would be:

f"{Fore.WHITE}11111   {Fore.YELLOW}22222{Style.RESET_ALL}    11111{Style.RESET_ALL}"

Loguru didn't expected colors to be part of the message. As a result, {Style.RESET_ALL} is called earlier that it should be.

The {Fore.WHITE} is missing to retrieve the log level color, and you end up with the default color of your console, which is red.

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

No branches or pull requests

2 participants