Title: Use of logging in matplotlib · Issue #13264 · matplotlib/matplotlib · GitHub
Open Graph Title: Use of logging in matplotlib · Issue #13264 · matplotlib/matplotlib
X Title: Use of logging in matplotlib · Issue #13264 · matplotlib/matplotlib
Description: I think the use of logging in matplotlib is not right. Unfortunately the official logging documentation is not quite comprehensive. I'll try to explain in the following how I understand that logging should be used. TLDR: I propose to jus...
Open Graph Description: I think the use of logging in matplotlib is not right. Unfortunately the official logging documentation is not quite comprehensive. I'll try to explain in the following how I understand that loggin...
X Description: I think the use of logging in matplotlib is not right. Unfortunately the official logging documentation is not quite comprehensive. I'll try to explain in the following how I understand that lo...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/13264
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Use of logging in matplotlib","articleBody":"I think the use of logging in matplotlib is not right.\r\n\r\nUnfortunately the official logging documentation is not quite comprehensive. I'll try to explain in the following how I understand that logging should be used.\r\n\r\n**TLDR: I propose to just remove `matplotlib._set_logger_verbose_level()` and all logging issues are gone. - Maybe add a bit of documentation how to use logging, but that's really not matplotlib specific.**\r\n\r\n## How logging should be used in simple cases\r\nIn the minimal case, libraries do only define a logger and call the respective `info()`, `error()` etc. functions on that logger.\r\n\r\n~~~\r\n_log = logging.getLogger(__name__)\r\n~~~\r\n\r\nIn this case, the library does not care about handling at all. IMO this is generally good practice as the library will issue messages, but it will not care which will be delivered or where they will go to (stdout, file, ...). That's the task of the code using the library:\r\n\r\n- if the programmer does not care about logging at all, [logging.lastResort](https://docs.python.org/3/library/logging.html#logging.lastResort) is used, which is a StreamHandler to stderr. This makes sure the messages go somewhere even without configuration (note: this was introduced in 3.2).\r\n\r\n- if the programmer wants to control the logging, the simple way is [logging.basicConfig()](https://docs.python.org/3/library/logging.html#logging.basicConfig). This is sufficent to redirect output to files, set the global log level etc.\r\n\r\n## How matplotlib uses logging\r\n\r\n### Former times\r\n\r\nThere has been some argument parsing magic, so that passing `--verbose` to a script using matplotlib could be used to control matplotlib's logging. This has been removed because it's too magical and can badly interfere with the argument handling of a script.\r\n\r\n### Now\r\n\r\nCurrently, there is only `matplotlib._set_logger_verbose_level()`.\r\n\r\nIf unused (default) the processes work as described above for loggging in simple cases.\r\n\r\nThe function binds a new StreamHandler (named *console* in the code) to `_log` and controls the level of the handler. This works ok for the case when the matplotlib-using programmer does not configure any logging himself: The messages are sent to *console*. `logging.lastResort` is not used. However, if logging `basicConfig()` is used, we have two handlers: *console* plus the one from the config, as can be seen when running\r\n\r\n~~~\r\nimport matplotlib\r\nimport logging\r\nlogging.basicConfig()\r\nmatplotlib._set_logger_verbose_level('helpful')\r\nmatplotlib._log.info('test')\r\n~~~\r\n\r\nThis outputs the message twice\r\n~~~\r\ntest\r\nINFO:matplotlib:test\r\n~~~\r\n\r\nluckily `_set_logger_verbose_level()` is private and should not be used currently.\r\n\r\n### Future\r\n\r\nThe proposal in #13129 is making `_set_logger_verbose_level()` indirectly public through some convenience functions. *IMHO this is the wrong way to go as described above*.\r\n\r\nWhat we should do is just remove `_set_logger_verbose_level()`. It's standard use casescan be handled using `logging.basicConfig()`. e.g.\r\n\r\n- redirecting to file: `logging.basicConfig(filename='errors.log')\r\n- setting the level: `logging.basicConfig(level='debug')\r\n\r\n`logging.basicConfig()` is actually more general, e.g.\r\n- specifying the output format\r\n- choosing between append and overwrite for a file.\r\n- using another handler\r\n- etc.\r\n\r\nThere's one special case that we do not cover: Logging matplotlibs output somewhere else that other logging output. That was possible / the case for `_set_logger_verbose_level()`. Not sure if it was by design or just accidentially. If a user really needs that, he has to attach a handler to the matplotlib logger (but also maybe set `_log.propagate = False`. We could add a convenience function for this, but IMHO this is rather an advance usage and it should be sufficient to make the matplotlib logger accessible.","author":{"url":"https://github.com/timhoffm","@type":"Person","name":"timhoffm"},"datePublished":"2019-01-22T21:08:26.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":17},"url":"https://github.com/13264/matplotlib/issues/13264"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:205f33af-cea3-e815-54b2-619e1ac527ab |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B338:1826AA:6E2A0D:92C93D:6A518EA6 |
| html-safe-nonce | 4d0e89ac42594aacb4e5c0ba3aaecbd07360431ce2a33673c749f09aaca96ac7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMzM4OjE4MjZBQTo2RTJBMEQ6OTJDOTNEOjZBNTE4RUE2IiwidmlzaXRvcl9pZCI6IjkxMjI1MTQ2ODMxMzExNzA0NzAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 22223d7cd3914c952bdfe7135deff2a08d6718579a764ac62cc09e8d1171d489 |
| hovercard-subject-tag | issue:401954424 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/13264/issue_layout |
| twitter:image | https://opengraph.githubassets.com/49196d179e58717081241fbd8cf7dad1db5cacdaba339c01995746d3f528bba9/matplotlib/matplotlib/issues/13264 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/49196d179e58717081241fbd8cf7dad1db5cacdaba339c01995746d3f528bba9/matplotlib/matplotlib/issues/13264 |
| og:image:alt | I think the use of logging in matplotlib is not right. Unfortunately the official logging documentation is not quite comprehensive. I'll try to explain in the following how I understand that loggin... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | timhoffm |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7aed05249554b889eb33d002851a973eebcc7e91 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width