Delgan / loguru

Python logging made (stupidly) simple
MIT License
18.69k stars 682 forks source link

Loguru 'success' level with Rich #1149

Closed teddyrasmussen closed 1 week ago

teddyrasmussen commented 3 weeks ago

I am using loguru to log errors/info/whatever in a project, but using it with the RichHandler for easier formatting. However, the loguru logger has a success mode (loguru.logger.success) that Rich doesn't seem to support.

Reproduceable code:

from loguru import logger
from rich.logging import RichHandler

logger.configure(
    handlers=[
        {
            "sink": RichHandler(),
            "format": "{name}:{function}:{line} - {message}",
        }
    ]
)

logger.success("This is a success message")

However, RichHandler doesn't seem to support this:

Screenshot 2024-06-10 at 6 20 35 PM

Is there any way around this?

Delgan commented 2 weeks ago

Hi.

Since Rich is based on standard logging module, and because standard logging module does not have a "success" level, you'll need to add it by yourself using logging.addLevelName() at the beginning of your script:

logging.addLevelName(25, "SUCCESS")

Then if you want to customize the color, check out this ticket probably: https://github.com/Textualize/rich/issues/1161

teddyrasmussen commented 2 weeks ago

That works, thank you so much!!

Delgan commented 1 week ago

Great, closing the ticket then. :+1: