Friday 16 July 2021

Python console logging and thousand separator while retaining structured logs compatibility

I am outputting large integer numbers, in their billions, to the console log. Currently, I am formatting the thousand separator syntax with f-strings for the sake to make them readable.

logger.info("Items created %s", "{:,}".format(large_number))

Because this is clunky, is there a way to have the log formatted natively format integer numberes using a thousand separator (and default to the US locale, if have to)?

Edit: Note that logger.info(f"Items created {large_number:,}") is an incorrect answer. Because log indexing services like Sentry or Datadog would not be able to parse structured logs. It will also cause the string formatting operation executed unnecessarily on disabled log levels.



from Python console logging and thousand separator while retaining structured logs compatibility

No comments:

Post a Comment