1

When I have to logging messages like this:

logging.info('An operation will be performed...')
some code here
logging.info('DONE')

I want DONE to be on the same line as the previous message. Is it possible to implement such behavior in the logging package?

Additionally, I see in my current project that logging sometime output messages on the same line, and sometime not. I have a hard time to understand what decides those operations.

freude
  • 3,632
  • 3
  • 32
  • 51
  • 2
    Possible duplicate of [How can I suppress newline in Python logging module.](https://stackoverflow.com/questions/12699645/how-can-i-suppress-newline-in-python-logging-module) – n8sty Nov 09 '18 at 05:34
  • Possible duplicate of [Suppress newline in Python logging module](https://stackoverflow.com/questions/7168790/suppress-newline-in-python-logging-module) – Kevin J. Chase Nov 09 '18 at 14:11

1 Answers1

3

While you could possibly do this by creating your own handler (see the answers to this question), it's not really a good idea in terms of readability.

You can get the same result without doing anything hacky; for example:

log_statement = 'An operation will be performed... %s'

try:
    if True:
        result = 'TRUE'
    else:
        result = 'UNREACHABLE'
except Exception as e:
    result = str(e)

logging.info(log_statement, result)

Additionally, I see in my current project that logging sometime output messages on the same line, and sometime not. I have a hard time to understand what decides those operations.

It seems likely the behavior you're seeing is not exactly what you think it is, but without code examples, there's no way for us to tell.

kungphu
  • 4,592
  • 3
  • 28
  • 37