# How to disable automatic logging into the Python Interactor?

**URL:** <https://discourse.slicer.org/t/how-to-disable-automatic-logging-into-the-python-interactor/23734>\
**Category:** Development\
**Tags:** python\
**Created:** [June 6, 2022, 1:26pm UTC](https://discourse.slicer.org/t/how-to-disable-automatic-logging-into-the-python-interactor/23734 "2022-06-06T13:26:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![CsatiZoltan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/csatizoltan/32/13807_2.png) [@CsatiZoltan](https://discourse.slicer.org/u/CsatiZoltan)\
**Post date:** [June 6, 2022, 1:26pm UTC](https://discourse.slicer.org/t/how-to-disable-automatic-logging-into-the-python-interactor/23734/1 "2022-06-06T13:26:57Z")

</div>

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_](https://www.slicer.org/wiki/Documentation/Nightly/SlicerApplication/ErrorLog) 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.

---

<div class="post-metadata">

**Author:** ![CsatiZoltan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/csatizoltan/32/13807_2.png) [@CsatiZoltan](https://discourse.slicer.org/u/CsatiZoltan)\
**Post date:** [June 7, 2022, 4:19pm UTC](https://discourse.slicer.org/t/how-to-disable-automatic-logging-into-the-python-interactor/23734/2 "2022-06-07T16:19:30Z")

</div>

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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/d/3d9859683d8498d526afadfc99831b553ab633b5.png)

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [June 7, 2022, 4:29pm UTC](https://discourse.slicer.org/t/how-to-disable-automatic-logging-into-the-python-interactor/23734/3 "2022-06-07T16:29:05Z")

</div>

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.

> <https://github.com/Slicer/Slicer/pull/5156>
>
> The motivation of this change is to improve user experience when viewing the Err…or Log Dialog and the slicer log as seen in the Report A Bug Dialog. When there are, for example, 3 errors and 3 warnings, the user will see 3 errors and 3 warnings. This is instead of 6 errors and 6 warnings where half were indicated from python and the other half were indicated from the stream.
> 
> This proposed change only shows python logging output in the console stream while in Developer Mode. For regular users not in developer mode they will no longer see double python logging (one from python and one from the stream).  
> 
> Developers will still have the double logging, but this appears to be desired behavior as they want knowledge of everything from the console stream in addition to everything from the python source. The following code below indicates a method to prevent the double logging, but as it says, it would remove the line that includes the log file name and line number which is arguably the more important of the double logged messages.
> https://github.com/Slicer/Slicer/blob/03113368de2e2afac71ade15ba0058b5e78dc39d/Base/Python/slicer/slicerqt.py#L72-L74
> 
> \### Developer Mode:
> \`\`\`
> \[DEBUG\]\[Qt\] 27.08.2020 22:56:22 \[\] (unknown:0) - Switch to module: "ScreenCapture"
> \[INFO\]\[Python\] 27.08.2020 22:56:25 \[Python\] (C:/Users/james/AppData/Local/NA-MIC/Slicer 4.11.0-2020-08-26/bin/../lib/Slicer-4.11/qt-scripted-modules/ScreenCapture.py:805) - Write C:\\Users\\james\\Documents\\SlicerCapture\\image\_00004.png
> \[INFO\]\[Stream\] 27.08.2020 22:56:25 \[\] (unknown:0) - Write C:\\Users\\james\\Documents\\SlicerCapture\\image\_00004.png
> \`\`\`
> !\[image\](https://user-images.githubusercontent.com/15837524/91516864-8a4bb580-e8ba-11ea-9903-aee2ff855633.png)
> 
> \### Regular Users:
> \`\`\`
> \[DEBUG\]\[Qt\] 27.08.2020 23:13:22 \[\] (unknown:0) - Switch to module: "ScreenCapture"
> \[INFO\]\[Python\] 27.08.2020 23:13:25 \[Python\] (C:/Users/james/AppData/Local/NA-MIC/Slicer 4.11.0-2020-08-26/bin/../lib/Slicer-4.11/qt-scripted-modules/ScreenCapture.py:805) - Write C:\\Users\\james\\Documents\\SlicerCapture\\image\_00005.png
> \`\`\`
> !\[image\](https://user-images.githubusercontent.com/15837524/91517194-3ee5d700-e8bb-11ea-8de5-20def1018c60.png)

> <https://github.com/Slicer/Slicer/pull/5176>
>
> This would supersede https://github.com/Slicer/Slicer/pull/5156 and the changes …proposed here are based on discussions from that PR.
> 
> Requires changes in https://github.com/commontk/CTK/pull/928 where there are changes such as:
> 
> \- a console mode button which allows viewing the log not in table format, but just simple text format. This is supported by selecting all fields when hiding the table. When hiding the table you can still use the filter buttons to view all errors in the simple text format as well.
> \- supporting colored text in the description column and full description fields. Warning (orange), Error and Critical (red)
> 
> \- The ctkErrorLogWidget table view option is shown by default instead of the "Console Mode" option being in the checked state.
> 
> !\[image\](https://user-images.githubusercontent.com/15837524/92498868-259d2e80-f1c9-11ea-9966-eeac289763c9.png)
> 
> \- The error log toolbutton in the status bar now has an icon that reflects the highest logged level state since viewing the log widget.
> \- The error log can now be docked just like the python console and is by default in the bottom area. There is a grabber tool between the python console and error log that allows resizing the two if both are shown.
> \- logging is no longer shown in the python console, but in the log messages area. If someone wants to add a consoleInfoHandler again, they can add that in code.
> \- ~~The log text in the ErrorReport Dialog area is no longer editable. I've made it read-only as I don't think it was intended to be editable there.~~ See 7e6a62dccd1b1f17fdb0b8e2946a75286af6d71b
> \- The log table and log description error are both scrolled to the bottom upon showing the error log. It also scrolls upon adding of a new entry.

---

<div class="post-metadata">

**Author:** ![CsatiZoltan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/csatizoltan/32/13807_2.png) [@CsatiZoltan](https://discourse.slicer.org/u/CsatiZoltan)\
**Post date:** [June 8, 2022, 10:17am UTC](https://discourse.slicer.org/t/how-to-disable-automatic-logging-into-the-python-interactor/23734/4 "2022-06-08T10:17:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [June 8, 2022, 11:58am UTC](https://discourse.slicer.org/t/how-to-disable-automatic-logging-into-the-python-interactor/23734/5 "2022-06-08T11:58:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [October 28, 2022, 9:19pm UTC](https://discourse.slicer.org/t/how-to-disable-automatic-logging-into-the-python-interactor/23734/6 "2022-10-28T21:19:23Z")

</div>

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

> [@New feature: Python console displays more log messages](https://discourse.slicer.org/t/new-feature-python-console-displays-more-log-messages/25739):
>
> Until now, the Python console only displayed Python outputs and Python log messages. Therefore, if a Python command executed a VTK method and that VTK method logged an error or warning, it was not immediately visible for the developer (the developer had to notice the change in the log icon, click on it, and scroll down to see the new message). In latest Slicer Preview Release (Slicer 5.1.0-2022-10-15 and later), the Python console displays log messages from all sources - Python, Qt, VTK, ITK. F…
