Logging → format decimal places?

going though a chonk of code, replacing print() with logging statements and realized that i’m not sure how to translate the following into a working log statement:

print(f'{inputPort.name} CURRmA PROCESS TIME: {crmTime:.4f}seconds')

i tried checking SO for answers, but there’s such convolution there, i’m coming here for a (hopefully) simpler answer.

this is what i attempted, but it errored out: invalid decimal literal. i knew it was wrong going in, but wanted to know what error it threw so i could look it up and hopefully be led to something that would work… yeeeaaahhhh… no love at all.

logging.info('%s CURRmA PROCESS TIME: %sseconds', inputPort.name, crmTime:.4f)

An easy fix is to just log the same string that you were printing before

logging.info(f'{inputPort.name} CURRmA PROCESS TIME: {crmTime:.4f}seconds')
1 Like

i literally found that out by missing a conversion during a copy/paste. holy crap. thank you @jamestwebber . i looked through the docs, but i’m either blind, or that information is not there. probably blind… thank you for confirming this. makes everything so much easier now… glad i didn’t get to far in. :stuck_out_tongue:

There’s is sometimes a good reason to pass values the other way, if they are expensive to compute (see here). But if you already have crmTime around I think this is fine.

Alternatively, the formatting for the logging message is the same as the % style, you can use %.4f there too.

1 Like

this is fantastically useful information. everything i seem to find on logging is some horribly arcane slog through massive configs and, frankly, if it’s that convoluted, i’m unlikely to use it. this makes logging more approachable. thank you so much.

sidenote: can you customize log levels for a more selective logging setup or does that plunge me into the abyss of configuration madness?