Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make logging output pretty when using backslash in string for Python PEP 8?

I have a CLI script which uses the logging module to print to screen and file. Is it possible to make ouput look better when breaking long lines for PEP8 using \?

logger.warning("SKIPPED File: '%s'; \
    MyFunc() returned no results."
    % (dir_file, ))

                               # You have to scroll to see the result --> 
WARNING----SKIPPED File: 'test-filetypes/client-somefile.txt';                            MyFunc() returned no results.
like image 429
xtian Avatar asked Apr 13 '26 16:04

xtian


2 Answers

Yes, use implicit string concatenation:

logger.warning(
    "SKIPPED File: '%s'; "
    "MyFunc() returned no results.",
    dir_file,
)

Note: I've fixed one other detail here - you shouldn't eagerly format your log strings. Just pass the template variable(s) along as arguments in the logging call.

like image 186
wim Avatar answered Apr 16 '26 04:04

wim


strings right next to each automatically get appended so

logger.warning("SKIPPED File: '%s'; " \
   "MyFunc() returned no results."
    % (dir_file, ))

Should get you what you want.

like image 24
Snark Avatar answered Apr 16 '26 05:04

Snark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!