Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a custom log handler #6900

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Conversation

brisvag
Copy link
Contributor

@brisvag brisvag commented May 7, 2024

References and relevant issues

Followup on #6849 (comment)

Description

The idea is to have our own custom handler for log messages so we can then expose the logs as we prefer from the GUI. This will make it easier for users to copy-paste logs even if they didn't manually open from the console, and also easier for developers to quickly check things.

I never really worked with the logging module, so I have a feeling that this is garbage... but oh well, let's see how bad :P

To test, open napari and do some stuff. Then, from the terminal or console run:

napari._LOG_STREAM.logs_at_level(10)

EDIT: Added a very quick-and-dirty GUI for the logger based on the implementation of the About widget. For how bad it is, it's actually ok xD You can open it from the menu `Help -> Show log".

cc @Czaki

@brisvag brisvag requested a review from Czaki May 13, 2024 12:34
@github-actions github-actions bot added the qt Relates to qt label May 13, 2024
self.setObjectName('LogDialog')
self.setWindowTitle(trans._('napari log'))
self.setWindowModality(Qt.WindowModality.ApplicationModal)
self.exec_()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exec should not be called in constructor, but in calling place.

@@ -42,6 +44,8 @@
'viewer': ['Viewer', 'current_viewer'],
}

_LOG_STREAM = _get_custom_log_stream()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that custom log handler should be initialized in napari.qt.qt_event_loop, same as notification manager.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the logger should work regardless of qt right? Also headless it would be useful.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand, it is log handler, not logger. The current code means, that each time one import napari it starts storing all logs in memory, even without a clean way to show them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes... but isn't that what we want? Would be good to be able to get these logs programmatically as well. We can also discard old logs if we go above a certain size.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My personal feeling is that we should do this, like with notifications (warnigs), but I may be wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do what? I'm not sure I follow ^^'

Comment on lines +46 to +48
logger = logging.getLogger('napari')
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be nice to connect to all loggers and be able to filter messages based on logger (so someone could filter logs only from a given plugin logger.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True... I'm not sure how to do that though; I guess we somehow get the root logger? And btw, is there a way to get these logging parameters (level, thread, etc) "raw" instead of formatting to string and then de-formatting?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
qt Relates to qt
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants