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

How can I customize exception formatting? #1124

Open
gabriel-tadeu-gt opened this issue Apr 10, 2024 · 2 comments
Open

How can I customize exception formatting? #1124

gabriel-tadeu-gt opened this issue Apr 10, 2024 · 2 comments
Labels
question Further information is requested

Comments

@gabriel-tadeu-gt
Copy link

gabriel-tadeu-gt commented Apr 10, 2024

I've been using the library to write a custom logger and most of the things went smoothly. The only problem I can't find the solution in the documentation is how to modify the exception format (colors, text-decoration) that is given by default.
I would like, for example, to change the colors of excpetion messages, since when printed in other places it's sometimes hard to read due to the lack of contrast with the background. Is there any place I can find information on this?
My current formatter is configured like this:

def formatter(record):
    """Function that defines the format that should be used in the log messages, establishing colors,
    icons and text decorations
    """
    colors_dict = {
        "DEBUG": "white",
        "INFO": "white",
        "SUCCESS": "green",
        "WARNING": "yellow",
        "ERROR": "red",
        "CRITICAL": "red",
    }
    for level, color in colors_dict.items():
        logger.level(name=level, color=f"<bold><{color}>")
    colored_message = format_message(record["message"])
    escaped = colored_message.replace("{", "{{").replace("}", "}}")
    return (
        "{time:DD/MM/YYYY - HH:mm:ss} | "
        + "<level>{level:<9} </>| "
        + f"{escaped}\n"
        + "{exception}"
    )

Any suggestions on how to make the formatter cleaner are also appreciated

@gabriel-tadeu-gt gabriel-tadeu-gt changed the title How can I customize the formatting of exceptions? How can I customize exception formatting? Apr 10, 2024
@Delgan
Copy link
Owner

Delgan commented Apr 10, 2024

The colors aren't publicly customizable. You need to use a third-party library in your formatter function, such as better_exceptions for example.

@gabriel-tadeu-gt
Copy link
Author

Hi, thanks for the fast response. That's unfortunate, but i will take a look at the better exceptions library as you mentioned, thank you 😀

@Delgan Delgan added the question Further information is requested label Apr 11, 2024
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