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