This question already has an answer here:
I would like to cancel newline when I do for example log.info(“msg”). When we do “print” it just
print msg,
So I need something like coma for logging.
I sow this question Suppress newline in Python logging module but can anybody give me reference or a simple example like “Hello world” Thanks!
This is not possible (without some serious hacking into the logging module). If you must have this functionality, build up the logging string in parts and log it ony when you are ready to display a log message with newline.
Here is what I answered for a similar question:
The new line,
\n
, is inserted inside theStreamHandler
class'semit(...)
method.If you're really set on fixing this behaviour, then here's an example of how I solved this by monkey patching the
emit(self, record)
method inside the logging.StreamHandler class.Here is the custom implementation of
emit()
that omits line breaks:Then you would make a custom logging class (in this case, subclassing from
TimedRotatingFileHandler
).Some people might argue that this type of solution is not Pythonic, or whatever. It might be so, so be careful.
Also, be aware that this will globally patch
SteamHandler.emit(...)
, so if you are using multiple logging classes, then this patch will affect the other logging classes as well!Hope that helps.