Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd77e94f

Browse files
committed
Change verbose to standard logging library
Change verbose to standard logging libraryPEP8 fixes and formatting of log messages@anntzerPEP8 fixes and formatting of log messages@anntzerSmall fixes as suggested by @aantzer and@tacaswellDocumentationFix on mergeNumber small fixesSmall fix for docsSmall fix for error in animate.pyfixed import order and small doc fixSmall doc fixChanged log. to _log.Fix on mergeNumber small fixesSmall fix for docsSmall fix for error in animate.pyfixed import order and small doc fixSmall doc fixChanged log. to _log.Changed log. to _log.fixed fontsgahgahgahgahgahgahPEP8 doc fixrevert _base.pyfix missing _logFixes for@efiringFixes for@anntzerminorDoc link fixes@anntzerSmall doc change
1 parentc465993 commitd77e94f

25 files changed

+434
-242
lines changed

‎.appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ after_test:
144144
# After the tests were a success, build packages (wheels and conda)
145145

146146
# Build the wheel with the static libs
147-
# Hide the output, the copied files really clutter thebuild log...
147+
# Hide the output, the copied files really clutter thebuild_log...
148148
-'%CMD_IN_ENV% python setup.py bdist_wheel > NUL:'
149149

150150
# And now the conda build after a cleanup...

‎doc/devel/contributing.rst

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ repository <https://github.com/matplotlib/matplotlib/>`__ on GitHub,
171171
then submit a "pull request" (PR).
172172

173173
The best practices for using GitHub to make PRs to Matplotlib are
174-
documented in the:ref:`development-workflow` section.
174+
documented in the:ref:`development-workflow` section.
175175

176176
A brief overview is:
177177

@@ -437,6 +437,71 @@ forced to use ``**kwargs``. An example is
437437
elif len(args) == 1:
438438
...etc...
439439

440+
.. _using_logging:
441+
442+
Using logging for debug messages
443+
--------------------------------
444+
445+
Matplotlib uses the standard python `logging` library to write verbose
446+
warnings, information, and
447+
debug messages. Please use it! In all those places you write:func:`print()`
448+
statements to do your debugging, try using:func:`log.debug()` instead!
449+
450+
451+
To include `logging` in your module, at the top of the module, you need to
452+
``import logging``. Then calls in your code like::
453+
454+
_log = logging.getLogger(__name__) # right after the imports
455+
456+
# code
457+
# more code
458+
_log.info('Here is some information')
459+
_log.debug('Here is some more detailed information')
460+
461+
will log to a logger named ``matplotlib.yourmodulename``.
462+
463+
If an end-user of your module sets up `logging` to display at levels
464+
more verbose than `logger.WARNING` in their code as follows::
465+
466+
import logging
467+
Format = '%(name)s:%(lineno)5d - %(levelname)s - %(message)s'
468+
logging.basicConfig(level=logging.DEBUG,
469+
format=Format)
470+
import matplotlib.pyplot as plt
471+
472+
Then they will receive messages like::
473+
474+
matplotlib.backends: 89 - INFO - backend MacOSX version unknown
475+
matplotlib.yourmodulename: 347 - INFO - Here is some information
476+
matplotlib.yourmodulename: 348 - DEBUG - Here is some more detailed information
477+
478+
Which logging level to use?
479+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
480+
481+
There are five levels at which you can emit messages.
482+
`logging.critical` and `logging.error`
483+
are really only there for errors that will end the use of the library but
484+
not kill the interpreter. :`logging.warning()` overlaps with the
485+
``warnings`` library. The
486+
`logging tutorial<https://docs.python.org/3/howto/logging.html#logging-basic-tutorial>`_
487+
suggests that the difference
488+
between `logging.warning` and `warnings.warn` is that
489+
`warnings.warn` be used for things the user must change to stop
490+
the warning, whereas `logging.warning` can be more persistent.
491+
492+
By default, `logging` displays all log messages at levels higher than
493+
`logging.WARNING` to `sys.stderr`.
494+
495+
Calls to `logging.info` are not displayed by default. They are for
496+
information that the user may want to know if the program behaves oddly.
497+
For instance, if an object isn't drawn because its position is ``NaN``,
498+
that can usually be ignored, but a mystified user could set
499+
``logging.basicConfig(level=logging.INFO)`` and get an error message that
500+
says why.
501+
502+
`logging.debug` is the least likely to be displayed, and hence can
503+
be the most verbose.
504+
440505
.. _custom_backend:
441506

442507
Developing a new backend

‎doc/faq/troubleshooting_faq.rst

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,35 @@ provide the following information in your e-mail to the `mailing list
114114
the error will help you find a bug in *your* code that is causing the
115115
problem.
116116

117-
* You can getveryhelpful debugging output from matlotlib byrunning your
118-
script with a ``verbose-helpful`` or ``--verbose-debug`` flags and posting
119-
the verbose output the lists::
117+
* You can get helpful debugging output from matlotlib byusing thelogging_
118+
library in your code and posting theverbose output the lists. For a
119+
command-line version of this, try::
120120

121-
python simple_plot.py --verbose-helpful > output.txt
121+
python -c "from logging import *; basicConfig(level=DEBUG); from pylab import *; plot(); show()"
122+
123+
124+
If you want to put the debugging hooks in your own code, then the
125+
most simple way to do so is to insert the following *before* any calls
126+
to ``import matplotlib``::
127+
128+
import logging
129+
logging.basicConfig(level=logging.DEBUG)
130+
131+
import matplotlib.pyplot as plt
132+
133+
Note that if you want to use `logging` in your own code, but do not
134+
want verbose Matplotlib output, you can set the logging level
135+
for Matplotlib independently::
136+
137+
import logging
138+
# set DEBUG for everything
139+
logging.basicConfig(level=logging.DEBUG)
140+
logger = logging.getLogger('matplotlib')
141+
# set WARNING for Matplotlib
142+
logger.setLevel(logging.WARNING)
143+
144+
The `logging` module is very flexible, and can be a valuable tool in chasing
145+
down errors.
122146

123147
If you compiled Matplotlib yourself, please also provide:
124148

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Logging for debug output
2+
------------------------
3+
4+
Matplotlib has in the past (sporadically) used an internal
5+
verbose-output reporter. This version converted those calls to using the
6+
standard pythonlogging_ library.
7+
8+
Support for the old ``rcParams`` ``verbose.level`` and ``verbose.fileo`` is
9+
dropped.
10+
11+
The command-line options ``--verbose-helpful`` and ``--verbose-debug`` are
12+
still accepted, but deprecated. They are now equivalent to setting
13+
``logging.INFO`` and ``logging.DEBUG``.
14+
15+
The logger's root name is ``matplotlib`` and can be accessed from programs
16+
as::
17+
18+
import logging
19+
mlog = logging.getLogger('matplotlib')
20+
21+
Instructions for basic usage are in:ref:`troubleshooting-faq` and for
22+
developers in:ref:`contributing`.
23+
24+
.. _logging:https://docs.python.org/3/library/logging.html

‎lib/matplotlib/__init__.py

Lines changed: 114 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
importio
113113
importinspect
114114
importitertools
115+
importlogging
115116
importlocale
116117
importos
117118
importre
@@ -123,7 +124,8 @@
123124
# definitions, so it is safe to import from it here.
124125
from .importcbook
125126
frommatplotlib.cbookimport (
126-
_backports,mplDeprecation,dedent,get_label,sanitize_sequence)
127+
_backports,mplDeprecation,dedent,get_label,sanitize_sequence,
128+
deprecated)
127129
frommatplotlib.compatimportsubprocess
128130
frommatplotlib.rcsetupimportdefaultParams,validate_backend,cycler
129131

@@ -137,6 +139,8 @@
137139
__version__=str(get_versions()['version'])
138140
delget_versions
139141

142+
_log=logging.getLogger(__name__)
143+
140144
__version__numpy__=str('1.7.1')# minimum required numpy version
141145

142146
__bibtex__=r"""@Article{Hunter:2007,
@@ -160,6 +164,9 @@
160164
ifnot (_python27or_python34):
161165
raiseImportError("Matplotlib requires Python 2.7 or 3.4 or later")
162166

167+
if_python27:
168+
_log.addHandler(logging.NullHandler())
169+
163170

164171
defcompare_versions(a,b):
165172
"return True if a is greater than or equal to b"
@@ -215,7 +222,76 @@ def _is_writable_dir(p):
215222
"""
216223
returnos.access(p,os.W_OK)andos.path.isdir(p)
217224

225+
_verbose_msg="""\
226+
Command line argument --verbose-LEVEL is deprecated.
227+
This functionality is now provided by the standard
228+
python logging library. To get more (or less) logging output:"
229+
import logging
230+
logger = logging.getLogger('matplotlib')
231+
logger.set_level(logging.INFO)"""
232+
233+
234+
def_set_logger_verbose_level(level_str='silent',file_str='sys.stdout'):
235+
"""
236+
Use a --verbose-LEVEL level to set the logging level:
218237
238+
"""
239+
levelmap= {'silent':logging.WARNING,'helpful':logging.INFO,
240+
'debug':logging.DEBUG,'debug-annoying':logging.DEBUG,
241+
'info':logging.INFO,'warning':logging.WARNING}
242+
# Check that current state of logger isn't already more verbose
243+
# than the requested level. If its more verbose, then leave more
244+
# verbose.
245+
iflevel_strinlevelmap:
246+
newlev=levelmap[level_str]
247+
oldlev=_log.getEffectiveLevel()
248+
ifnewlev<oldlev:
249+
_log.setLevel(newlev)
250+
std= {
251+
'sys.stdout':sys.stdout,
252+
'sys.stderr':sys.stderr,
253+
}
254+
iffile_strinstd:
255+
fileo=std[file_str]
256+
else:
257+
fileo=sys.stdout
258+
try:
259+
fileo=open(file_str,'w')
260+
# if this fails, we will just write to stdout
261+
exceptIOError:
262+
warnings.warn('could not open log file "{0}"'
263+
'for writing. Check your '
264+
'matplotlibrc'.format(file_str))
265+
console=logging.StreamHandler(fileo)
266+
console.setLevel(newlev)
267+
_log.addHandler(console)
268+
269+
270+
def_parse_commandline():
271+
"""
272+
Check for --verbose-LEVEL type command line arguments and
273+
set logging level appropriately.
274+
"""
275+
276+
levels= ('silent','helpful','debug','debug-annoying',
277+
'info','warning')
278+
279+
forarginsys.argv[1:]:
280+
# cast to str because we are using unicode_literals,
281+
# and argv is always str
282+
283+
ifarg.startswith(str('--verbose-')):
284+
level_str=arg[10:]
285+
# If it doesn't match one of ours, then don't even
286+
# bother noting it, we are just a 3rd-party library
287+
# to somebody else's script.
288+
iflevel_strinlevels:
289+
_set_logger_verbose_level(level_str)
290+
291+
_parse_commandline()
292+
293+
294+
@deprecated("2.2",message=_verbose_msg)
219295
classVerbose(object):
220296
"""
221297
A class to handle reporting. Set the fileo attribute to any file
@@ -311,7 +387,31 @@ def ge(self, level):
311387
returnself.vald[self.level]>=self.vald[level]
312388

313389

314-
verbose=Verbose()
390+
def_wrap(fmt,func,level='INFO',always=True):
391+
"""
392+
return a callable function that wraps func and reports it
393+
output through logger
394+
395+
if always is True, the report will occur on every function
396+
call; otherwise only on the first time the function is called
397+
"""
398+
assertcallable(func)
399+
400+
defwrapper(*args,**kwargs):
401+
ret=func(*args,**kwargs)
402+
403+
if (alwaysornotwrapper._spoke):
404+
lvl=logging.getLevelName(level.upper())
405+
_log.log(lvl,fmt%ret)
406+
spoke=True
407+
ifnotwrapper._spoke:
408+
wrapper._spoke=spoke
409+
returnret
410+
wrapper._spoke=False
411+
wrapper.__doc__=func.__doc__
412+
returnwrapper
413+
414+
# verbose = Verbose()
315415

316416

317417
defcheckdep_dvipng():
@@ -512,7 +612,7 @@ def _create_tmp_config_dir():
512612
returnconfigdir
513613

514614

515-
get_home=verbose.wrap('$HOME=%s',_get_home,always=False)
615+
get_home=_wrap('$HOME=%s',_get_home,always=False)
516616

517617

518618
def_get_xdg_config_dir():
@@ -601,7 +701,7 @@ def _get_configdir():
601701
"""
602702
return_get_config_or_cache_dir(_get_xdg_config_dir())
603703

604-
get_configdir=verbose.wrap('CONFIGDIR=%s',_get_configdir,always=False)
704+
get_configdir=_wrap('CONFIGDIR=%s',_get_configdir,always=False)
605705

606706

607707
def_get_cachedir():
@@ -613,7 +713,7 @@ def _get_cachedir():
613713
"""
614714
return_get_config_or_cache_dir(_get_xdg_cache_dir())
615715

616-
get_cachedir=verbose.wrap('CACHEDIR=%s',_get_cachedir,always=False)
716+
get_cachedir=_wrap('CACHEDIR=%s',_get_cachedir,always=False)
617717

618718

619719
def_decode_filesystem_path(path):
@@ -671,7 +771,7 @@ def _get_data_path_cached():
671771
defaultParams['datapath'][0]=_get_data_path()
672772
returndefaultParams['datapath'][0]
673773

674-
get_data_path=verbose.wrap('matplotlib data path %s',_get_data_path_cached,
774+
get_data_path=_wrap('matplotlib data path %s',_get_data_path_cached,
675775
always=False)
676776

677777

@@ -1035,22 +1135,18 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
10351135
ifkeynotin_all_deprecated])
10361136
config.update(config_from_file)
10371137

1038-
verbose.set_level(config['verbose.level'])
1039-
verbose.set_fileo(config['verbose.fileo'])
1040-
10411138
ifconfig['datapath']isNone:
10421139
config['datapath']=get_data_path()
10431140

10441141
if"".join(config['text.latex.preamble']):
1045-
verbose.report("""
1142+
_log.info("""
10461143
*****************************************************************
10471144
You have the following UNSUPPORTED LaTeX preamble customizations:
10481145
%s
10491146
Please do not ask for support with these customizations active.
10501147
*****************************************************************
1051-
"""%'\n'.join(config['text.latex.preamble']),'helpful')
1052-
1053-
verbose.report('loaded rc file %s'%fname)
1148+
""",'\n'.join(config['text.latex.preamble']))
1149+
_log.info('loaded rc file %s',fname)
10541150

10551151
returnconfig
10561152

@@ -1736,9 +1832,8 @@ def inner(ax, *args, **kwargs):
17361832
returninner
17371833
returnparam
17381834

1739-
1740-
verbose.report('matplotlib version %s'%__version__)
1741-
verbose.report('verbose.level %s'%verbose.level)
1742-
verbose.report('interactive is %s'%is_interactive())
1743-
verbose.report('platform is %s'%sys.platform)
1744-
verbose.report('loaded modules: %s'%list(sys.modules),'debug')
1835+
_log.info('matplotlib version %s',__version__)
1836+
#_log.info('verbose.level %s' % verbose.level)
1837+
_log.info('interactive is %s',is_interactive())
1838+
_log.info('platform is %s',sys.platform)
1839+
_log.debug('loaded modules: %s',list(sys.modules))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp