8000 ENH: add a user-friendly verbose interface by jklymak · Pull Request #13129 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

ENH: add a user-friendly verbose interface #13129

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/devel/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,11 @@ Then they will receive messages like::
matplotlib.yourmodulename: 347 - INFO - Here is some information
matplotlib.yourmodulename: 348 - DEBUG - Here is some more detailed information

More straight forward helper methods are available to end-users as well::

import matplotlib.pyplot as plt
plt.set_loglevel(level='info') # or plt.set_loglevel(level='debug')
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
plt.set_loglevel(level='info') # or plt.set_loglevel(level='debug')
plt.set_loglevel('info') # or plt.set_loglevel('debug')

Since there is only one parameter and the function name already tells it, I propose the canonical way of using this is with a positional parameter.


Which logging level to use?
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
12 changes: 9 additions & 3 deletions doc/faq/troubleshooting_faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,16 @@ provide the following information in your e-mail to the `mailing list

python -c "from logging import *; basicConfig(level=DEBUG); from pylab import *; plot(); show()"

If you want to trigger logging in a script, it can be done with helper
functions::

If you want to put the debugging hooks in your own code, then the
most simple way to do so is to insert the following *before* any calls
to ``import matplotlib``::
import matplotlib.pyplot as plt
plt.set_loglevel(level='info') # or plt.set_loglevel(level='debug')
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
plt.set_loglevel(level='info') # or plt.set_loglevel(level='debug')
plt.set_loglevel('info') # or plt.set_loglevel('debug')

See above.



If you want to put full debugging hooks in your own code, including
debug information when matplotlib starts up, then the way to do so is to
insert the following *before* any calls to ``import matplotlib``::

import logging
logging.basicConfig(level=logging.DEBUG)
Expand Down
7 changes: 7 additions & 0 deletions doc/users/next_whats_new/pyplot_verbose.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
:orphan:

New methods for verbose logging
-------------------------------

If more debugging information is required, the user can call
the `.pyplot.set_loglevel` method.
37 changes: 29 additions & 8 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,35 @@ def _check_versions():
sys.argv = ['modpython']


_verbose_msg = """\
matplotlib.verbose is deprecated;
Command line argument --verbose-LEVEL is deprecated.
This functionality is now provided by the standard
python logging library. To get more (or less) logging output:
import logging
logger = logging.getLogger('matplotlib')
logger.set_level(logging.INFO)"""
def set_loglevel(level='info'):
Copy link
Member

Choose a reason for hiding this comment

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

Do we need a default? Just from set_loglevel() I couldn't guess what level is now in use. Having to pass the level makes it explicit.

Copy link
Member Author

Choose a reason for hiding this comment

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

I disagree with this. The only reason it is ambiguous is you suggested we change the name from verbose 😉 The naive user posting on stack overflow is not going to know the names of the logging levels so having an interface where they don’t have to pass an argument is desirable.

"""
Shortcut to set the logging level of the logging module.

Parameters
----------
level : str or bool
Copy link
Member

Choose a reason for hiding this comment

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

IMHO there is no natural mapping from bool to log levels. I would discard the bool option.
set_loglevel(True) is less clear and only slightly less to type compared to set_loglevel('info').

If True set logging level to "INFO" for matplotlib module.
If FALSE set logging level to "WARNING" for matplotlib module (this
is the default level if ``set_loglevel`` is not called). If a string,
must be one of ['warning', 'info', 'debug'], in order of increasing
verbosity.

Notes
-----
This is the same as the standard python `logging` module::

import logging

_log = logging.getLogger(__name__)
_log.setLevel(logging.INFO) # or logging.WARNING, logging.INFO

"""

if level is True:
level = 'info'
if level is False:
level = 'warning'
_set_logger_verbose_level(level_str=level.lower())


def _set_logger_verbose_level(level_str='silent', file_str='sys.stdout'):
Expand Down
26 changes: 26 additions & 0 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1997,6 +1997,32 @@ def colormaps():
return sorted(cm.cmap_d)


def set_loglevel(level='info'):
"""
Shortcut to set the debugging level of the logging module.

Parameters
----------
level : str or bool (optional)
If *True* (default) set logging level to "INFO" for matplotlib module.
If *False* set logging level to "WARNING" for matplotlib module (this
is the default level if ``verbose`` is not called). If a string, must
be one of ['warning', 'info', 'debug'], in order of increasing
verbosity.

Notes
-----
This is the same as the standard python `logging` module::

import logging

_log = logging.getLogger(__name__)
_log.setLevel(logging.DEBUG) # or logging.WARNING, logging.INFO

"""
matplotlib.set_loglevel(level=level)


def _setup_pyplot_info_docstrings():
"""
Generates the plotting docstring.
Expand Down
0