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

Setting the logger max length for prints #1252

Open
LC117 opened this issue Dec 6, 2024 · 1 comment
Open

Setting the logger max length for prints #1252

LC117 opened this issue Dec 6, 2024 · 1 comment
Labels
feature Request for adding a new feature

Comments

@LC117
Copy link

LC117 commented Dec 6, 2024

Hi,
when logging with loguru, messages are truncated by default after 128 symbols, see:

v = v[: max_length - 3] + "..."

While using e.g. @logger.catch(reraise=True) often relevant information is truncated.

Currently, the only way I found to adjust the maximum message length in loguru requires modifying internal attributes, e.g.:

logger._core.handlers[0]._exception_formatter._max_length = 200

This hacky approach relies on internal implementation details.
It would be great to have an officially supported way to set the maximum message length via the public API, such as a configuration parameter when adding a handler or as part of the logger.add() method. Or does this somehow already exist?

For example:
logger.add(sys.stdout, format="{message}", max_message_length=200)

Thanks!

@Delgan
Copy link
Owner

Delgan commented Dec 6, 2024

Hi @LC117. Thanks for your suggestion.

This sounds similar to #173.
See also related PR: #1224.

So far, my recommendations for your use case have been to let you define a custom exception formatter: Customizing the formatting of exceptions.

I feel that implementing a new max_message_length argument makes the logger.add() method increasingly complex for little gain.
However, I also think that Loguru could benefit from being more easily configurable when it comes to exception formatting. I'm probably planning to change the API so that, for example, you can configure the max length if desired.
For example:

exception_formatter = logger.traceback(diagnose=True, backtrace=False, width=200)
logger.add("file.log", exception_formatter=exception_formatter)

The above solution make the API compatible with other formatters such as Rich for example.

@Delgan Delgan added the feature Request for adding a new feature label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Request for adding a new feature
Projects
None yet
Development

No branches or pull requests

2 participants