loguru icon indicating copy to clipboard operation
loguru copied to clipboard

Feature Request: Support for Abbreviated Logger Names in Format ({name:abbrev})

Open raulcarlomagno opened this issue 2 months ago • 2 comments

Hi.

In large Python applications, the full module path printed by the {name} token in the Loguru format can become excessively long (e.g., src.common.database.services.user_retrieval). This consumes valuable terminal space and makes log lines difficult to scan quickly.

The current workaround requires manual effort in every module to logger.bind(short_name=...) and then manually define a custom format string using {extra[short_name]}, which defeats Loguru's goal of easy configuration.

I propose adding a precision/abbreviation modifier to the {name} token in the format string, similar to how Logback/log4j implement %logger{n} in Java. This would allow users to specify the abbreviation style directly in the format string, eliminating the need for logger.bind() in every file. Proposed Syntax: {name:short} (or {name:1}): Abbreviate all package segments except the last one. Input: src.common.db.resources Output: s.c.d.resources {name:2}: Show the first letter of all segments except the last two. Input: src.common.db.resources Output: s.c.db.resources {name:all_short} (or {name:all_1}): Show the first letter of every segment. Input: src.common.db.resources Output: s.c.d.r

raulcarlomagno avatar Oct 09 '25 13:10 raulcarlomagno

Hi.

I understand the rationale behind your suggestion, but it would involve changing name from a str to a custom type and implementing a separate, documented format specification. However, I prefer to stick to the formatting specifications already popularized by Python. I do not want to introduce another mini-language. It would be nice if Python could support truncating in their string formatting specs, but despite opened discussions, nothing is planned in this regard to my knowledge.

That being said, you seem to assume that the current workaround requires binding the logger in each module, but another solution is possible. You can patch() the logger globally using configure(), ensuring that every log undergoes the same pre-processing step, and allowing you to use your abbreviation in the format.

Here is an example:

from loguru import logger
import sys


def abbreviate_name(record):
    *prefixes, final = record["name"].split(".")
    short_name = ".".join([*[p[0] for p in prefixes], final])
    record["extra"]["short_name"] = short_name


logger.configure(
    patcher=abbreviate_name,
    handlers=[
        {
            "sink": sys.stderr,
            "format": "{extra[short_name]} | {message}",
            "level": "DEBUG",
        }
    ],
)

While it does require manually implementing a function, this solution appears to be the most effective: it is sufficiently generic to let users abbreviate names according to their exact preferences. With this approach, you do not need to modify the logger of each of your modules.

Delgan avatar Oct 09 '25 14:10 Delgan

thank you for your quick response, it helped me, at the end i did this as you suggested

logger.configure(patcher=abbreviate_name)
logger.remove()
logger.add(sys.stderr, enqueue=True, format=CUSTOM_LOGURU_FORMAT)

raulcarlomagno avatar Oct 10 '25 09:10 raulcarlomagno