How to disable automatic logging into the Python Interactor?

My intention is to

  • log into a file in great detail and verbose formatting (timestamp, logger name, etc.).
  • log into the Python Interactor in less verbose mode (less verbose formatting, only higher logging levels, etc.).

This can be achieved by defining two handlers in the logging module of Python: a FileHandler and a StreamHandler. I tested logging into a file; it works as intended outside 3D Slicer. Before adding the possibility of logging into the console, I tried it out in 3D Slicer.

I noticed that my INFO-level messages automatically appear in the Python Interactor, even though I only log into a file. This is not the intended behaviour for me as

  • I don’t want my logs appearing automatically in the Python Interactor.
  • I will define my own formatting to be shown in the Python Interactor using a StreamHandler object.

It seems that the logs I send to the file are automatically redirected to the Python Interactor, filtered (DEBUG level messages are not shown for instance) and reformatted (keeps the message only) by Slicer. How can I switch this behaviour off?

I also noted that my logs appear in the Error Log window too (this time, the level is indicated as well). It is less of a problem for me, although, if possible, I would like to switch off the automatic redirection here as well.

What I want to achieve:

  • my logging into a file (using FileHandler) is not shown in the Python Interactor (preferably neither in the Error Log window)
  • my logs with a StreamHandler are shown in the Python Interactor.

In the Error Log, I see that each logging to the file results in two log events in Slicer. It demonstrates that the above-mentioned redirection happens.

SlicerLog

Related to some of this double logging I have work-in-progress PRs from awhile ago that I haven’t returned to, but there was a lot of discussion on them on improved logging.

Thank you for the PR links, good to see that I am not the only one annoyed by the double logging. Is this improvement still on the table for a future Slicer release?

Yeah improvement in that area is still possible, but it’s not currently in a specific Slicer GitHub milestone. For that specific work I did it in my personal free time so when I have some more of it I may pick it back up again.

1 Like

To follow up on this thread, please see the following thread for an update about python logging