0

I would like to log a number formatted with commas, but using the delayed interpolation method.

some_num = 1234567

# This does the formatting like I want:
logger.info(f'Number = {some_num:,}'
>> Number = 1,234,567

# But I want to use this syntax:
logger.info(f'Number = %d', some_num)
>> Number = 1234567

Is there a way to use the ("%d", variable) and add optional formatting options?

The reason I'd prefer to use that method (instead of str.format or f-strings) is because it'd be consistent with the logging in the rest of the application, and which, if the logging level is set higher than a given log message, then the message will not interpolate the variables.

Edit

To be pedantic, I don't want to use f-strings, nor the old '%s' % my_var form, or str.format. I want to use logging's placeholders (%s, %d, %i, etc.) but be able to add commas to numbers.

Example:

# What I'm trying to do
logger.info('Number = %d:,', some_num)
>> 1,234,567
vvvvv
  • 25,404
  • 19
  • 49
  • 81
rodrigo-silveira
  • 12,607
  • 11
  • 69
  • 123
  • Have a look at @Mike DeSimoe's answer in https://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators – cup Dec 06 '20 at 06:37
  • Does this answer your question? [How to print number with commas as thousands separators?](https://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators) – Klaus D. Dec 06 '20 at 06:46
  • You're question needs a little updating, I think. You say you *don't* want to use f-strings, but your intended version does use an f-string. (Also aside: your first example is missing a closing bracket). Since you don't want to use f-strings or `.format`, I think what you mean by your intended example is to use the old-style `%` operator formatting (i.e. `"Number = %d" % some_num`)? – costaparas Dec 06 '20 at 06:57
  • @costaparas Edited my question. – rodrigo-silveira Dec 06 '20 at 07:32
  • Thanks for the clarification @rodrigo-silveira. These requirements are extremely limiting. I doubt there exists a clean solution. Seems like the cleanest is to have a function do the conversion as done [here](https://stackoverflow.com/a/1823101/14722562) and then call it each time you do the logging. – costaparas Dec 06 '20 at 09:18
  • Are you aware that you can change the `style` of the logging formatting? – blues Dec 06 '20 at 15:58
  • @blues I am aware of that, but wasn't aware that I could have the style format arbitrary numbers in the log message. Can you post an example? – rodrigo-silveira Dec 07 '20 at 04:57
  • Well, the main point would be to change the `style` setting to f strings, which support that. – blues Dec 07 '20 at 07:24
  • Gotcha. Thanks. Doesn't look like that's what I'm looking for. The reason I don't want to use fstrings is that if I'm logging a lot of variables (say, an entire object) with logger.debug but the log level is set to something higher (meaning my debug log message will not get logged), I don't want to take the time to create the long string with interpolated values that will not even get logged. Using `logger.debug('object=%s, number=%d', my_obj, my_num) does what I need - minus the numeric formatting – rodrigo-silveira Dec 07 '20 at 17:23

0 Answers0