@@ -207,48 +207,43 @@ def _check_versions():
207207 sys .argv = ['modpython' ]
208208
209209
210- _verbose_msg = """\
211- matplotlib.verbose is deprecated;
212- Command line argument --verbose-LEVEL is deprecated.
213- This functionality is now provided by the standard
214- python logging library. To get more (or less) logging output:
215- import logging
216- logger = logging.getLogger('matplotlib')
217- logger.set_level(logging.INFO)"""
210+ # The decorator ensures this always returns the same handler (and it is only
211+ # attached once).
212+ @functools .lru_cache ()
213+ def _ensure_handler ():
214+ """
215+ The first time this function is called, attach a `StreamHandler` using the
216+ same format as `logging.basicConfig` to the Matplotlib root logger.
217+
218+ Return this handler every time this function is called.
219+ """
220+ handler = logging .StreamHandler ()
221+ handler .setFormatter (logging .Formatter (logging .BASIC_FORMAT ))
222+ _log .addHandler (handler )
223+ return handler
218224
219225
220- def _set_logger_verbose_level ( level_str = 'silent' , file_str = 'sys.stdout' ):
226+ def set_loglevel ( level ):
221227 """
222- Use a --verbose-LEVEL level to set the logging level:
228+ Sets the Matplotlib's root logger and root logger handler level, creating
229+ the handler if it does not exist yet.
223230
231+ Typically, one should call ``set_loglevel("info")`` or
232+ ``set_loglevel("debug")`` to get additional debugging information.
233+
234+ Parameters
235+ ----------
236+ level : {"notset", "debug", "info", "warning", "error", "critical"}
237+ The log level of the handler.
238+
239+ Notes
240+ -----
241+ The first time this function is called, an additional handler is attached
242+ to Matplotlib's root handler; this handler is reused every time and this
243+ function simply manipulates the logger and handler's level.
224244 """
225- levelmap = {'silent' : logging .WARNING , 'helpful' : logging .INFO ,
226- 'debug' : logging .DEBUG , 'debug-annoying' : logging .DEBUG ,
227- 'info' : logging .INFO , 'warning' : logging .WARNING }
228- # Check that current state of logger isn't already more verbose
229- # than the requested level. If it is more verbose, then leave more
230- # verbose.
231- newlev = levelmap [level_str ]
232- oldlev = _log .getEffectiveLevel ()
233- if newlev < oldlev :
234- _log .setLevel (newlev )
235- std = {
236- 'sys.stdout' : sys .stdout ,
237- 'sys.stderr' : sys .stderr ,
238- }
239- if file_str in std :
240- fileo = std [file_str ]
241- else :
242- fileo = sys .stdout
243- try :
244- fileo = open (file_str , 'w' )
245- # if this fails, we will just write to stdout
246- except IOError :
247- _log .warning ('could not open log file "{0}" for writing. '
248- 'Check your matplotlibrc' .format (file_str ))
249- console = logging .StreamHandler (fileo )
250- console .setLevel (newlev )
251- _log .addHandler (console )
245+ _log .setLevel (level .upper ())
246+ _ensure_handler ().setLevel (level .upper ())
252247
253248
254249def _logged_cached (fmt , func = None ):
0 commit comments