Description
Hi!
I encountered an issue when trying to dynamically format log file names using fields from the log record (e.g., values from the extra
dict). For example, I expected the following configuration to work:
from loguru import logger
logger.add(
"logs/{extra[type]}.log",
filter=lambda record: "type" in record["extra"],
delay=True
)
logger.bind(type="example").info("Test message")
However, this fails because with KeyError: 'extra'
Exception, in the current implementation of Loguru's file sink, only the "time" variable is available for file name formatting. In the FileSink class, the _create_path()
method is defined as follows:
def _create_path(self):
path = self._path.format_map({"time": FileDateFormatter()})
return os.path.abspath(path)
As shown, the format context only includes "time", and no elements from the log record (such as extra) are provided. This makes it impossible to use dynamic file names based on other record fields.
Question:
Is there any plan to allow additional fields (like extra) in the file name formatting context, or is this behavior by design? What would be the recommended approach to achieve dynamic file naming if support for extra fields is not available?
Related issue: #538