You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi! I'm looking to yield a very customized JSON format for each log record. I've been trying to use a custom sink or format function, but there are some issue with it:
When using the sink function, you get only the formatted log message as a string or the (not entirely raw) log record as a JSON-string (with serialized=True)
When using a format function, you get the log record but my issue is that the message is already formatted to replace parameters in the text.
I'd like to be able to receive the un-formatted log message and arguments.
This almost works, but the arguments in the message are already formatted at this point.
logger.remove()
logger.add(sys.stderr, format=JsonFormatter())
logger.info(
"Hello, World! My name is {name}",
name=safe("John Smith"),
password="JaneSmith1234",
)
Is there any way to prevent the message from being already formatted? Something like a "message formatter" function I can pass (and make it a no-op)?
Can a formatter just output the fully formatted text instead of the log format to use with str.format()?
The text was updated successfully, but these errors were encountered:
NiklasRosenstein
changed the title
Sink function to receive raw record?
Sink function to receive raw record, or formatter to return formatted record?
Feb 10, 2025
Is there any way to prevent the message from being already formatted? Something like a "message formatter" function I can pass (and make it a no-op)?
If you mean the message such as "Hello, World! My name is {name}" (ignoring the formatting of name), then this is not possible. The message formatting precedes everything else, and although I thought about making this configurable in the past, this would be technically challenging and not worth it in my opinion.
Besides, as @mrkmcnamee pointed out, keyword-arguments formatting will likely become deprecated.
Can a formatter just output the fully formatted text instead of the log format to use with str.format()?
No, but you can achieve something similar by assigning your formatted string to an extra attribute and returning it as the format:
Hi! I'm looking to yield a very customized JSON format for each log record. I've been trying to use a custom sink or format function, but there are some issue with it:
serialized=True
)message
is already formatted to replace parameters in the text.I'd like to be able to receive the un-formatted log message and arguments.
I've found a bit of a dumb workaround:
This almost works, but the arguments in the message are already formatted at this point.
str.format()
?The text was updated successfully, but these errors were encountered: