Skip to content

set_loglevel documenation and escape hatch #23681

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 2 commits 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
10 changes: 10 additions & 0 deletions doc/users/next_whats_new/loggers.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
``set_loglevel`` can opt-out of manipulating logging handlers
-------------------------------------------------------------

It is now possible to configure the logging level of the Matplotilb standard
library logger without also implicitly installing a handler via both
`matplotlib.set_loglevel` and `matplotlib.pyplot.set_loglevel` ::

mpl.set_loglevel('debug', ensure_handler=False)
# or
plt.set_loglevel('debug', ensure_handler=False)
24 changes: 20 additions & 4 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,27 +230,43 @@ def _ensure_handler():
return handler


def set_loglevel(level):
def set_loglevel(level, *, ensure_handler=True):
"""
Set Matplotlib's root logger and root logger handler level, creating
the handler if it does not exist yet.
Configure Matplotlib's logging levels.

Matplotlib uses the standard library `logging` framework under the root
logger 'matplotlib'. This is a helper function to:

- set Matplotlib's root logger level
- optionally set the root logger handler's level, creating the handler
if it does not exist yet

Typically, one should call ``set_loglevel("info")`` or
``set_loglevel("debug")`` to get additional debugging information.

Users or applications that are installing their own logging handlers
may want to directly manipulate ``logging.getLogger('matplotlib')`` rather
than use this function.

Parameters
----------
level : {"notset", "debug", "info", "warning", "error", "critical"}
The log level of the handler.

ensure_handler : bool
Copy link
Member

Choose a reason for hiding this comment

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

I guess I am not sure why we would add this. A naive enduser unfamiliar with logging won't know what this means, and a library author familiar with logging should know not to call this, particularly given the change to the docstring above. If we do add this, the docstring below probably needs to be clarified.

Copy link
Contributor

@l-johnston l-johnston Aug 20, 2022

Choose a reason for hiding this comment

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

I agree; a documentation update should be sufficient:

  • Add a new section for discussion of logging within Matplotlib - the two use cases as a library and as an application
  • End users can call set_level()
  • For library developers, state that Matplotlib is using the standard library logging and its singleton logger is called 'matplotlib' and in their library code get the 'matplotlib' logger and add the desired handler.

If True will ensure that there is a `logging.StreamHandler` added to
the matplotlib root logger and that its level matches the logger level.

Notes
-----
The first time this function is called, an additional handler is attached
to Matplotlib's root handler; this handler is reused every time and this
function simply manipulates the logger and handler's level.

"""
_log.setLevel(level.upper())
_ensure_handler().setLevel(level.upper())
if ensure_handler:
_ensure_handler().setLevel(level.upper())


def _logged_cached(fmt, func=None):
Expand Down