loguru icon indicating copy to clipboard operation
loguru copied to clipboard

Setting the logger max length for prints

Open LC117 opened this issue 1 year ago • 1 comments

Hi, when logging with loguru, messages are truncated by default after 128 symbols, see: https://github.com/Delgan/loguru/blob/633016db07e5dff63bc05dd3c4d5aa81b6190700/loguru/_better_exceptions.py#L346 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!

LC117 avatar Dec 06 '24 11:12 LC117

Hi @LC117. Thanks for your suggestion.

This sounds similar to https://github.com/Delgan/loguru/issues/173. See also related PR: https://github.com/Delgan/loguru/pull/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 avatar Dec 06 '24 11:12 Delgan