Babken Vardanyan
Babken Vardanyan

Reputation: 15040

Log a string without ending newline

I want to print a debug message (a string) which may or may not contain an ending newline, without the ending newline.

With print it's easy:

print('asdf\n', end='')

However with standard logging library there is no end parameter and the ending newline gets printed:

import logging
logging.warning('asdf\n')

What is the best way to print a message without the ending newline in the logging library function calls?

Upvotes: 1

Views: 3841

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122322

You can strip 0 or more newlines with str.rstrip() before passing it to logging.warning():

logging.warning(message.rstrip('\n'))

Upvotes: 5

Related Questions