-
Notifications
You must be signed in to change notification settings - Fork 830
Refactoring the logging module #5367
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
base: develop
Are you sure you want to change the base?
Changes from all commits
33a8005
1bf0ee1
24a6c31
7a5c0b3
f1be0df
dbcfb1c
93966fb
c5f6133
e30d0ab
5a5906d
f679251
2ba1c2e
cca9258
94f056f
7f5812f
ba9315e
0e36231
e63f5ef
a81e4ad
f25aec7
bdaf241
ab00172
d0b732f
ec0ed25
6d1a675
74bcc03
0fdd02e
d952902
c34eca5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,22 +86,41 @@ | |
| """ | ||
| import sys | ||
| import logging | ||
| import re | ||
| import os | ||
|
|
||
| from tqdm.auto import tqdm | ||
|
|
||
| # from MDAnalysis.lib.util import deprecate | ||
|
|
||
| from .. import version | ||
|
|
||
| # Things to deprecated: | ||
| # logfile -> stream (bc it could be any stream like object) | ||
| # | ||
| # version? Why would any user want to modify version in the first place? | ||
| # Just have the message logged it directly | ||
|
|
||
|
|
||
| def start_logging(logfile="MDAnalysis.log", version=version.__version__): | ||
| def start_logging(stream="MDAnalysis.log", version=version.__version__): | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment above in line 101. Why would a user want to manually change the |
||
| """Start logging of messages to file and console. | ||
|
|
||
| The default logfile is named `MDAnalysis.log` and messages are | ||
| logged with the tag *MDAnalysis*. | ||
| """ | ||
| create("MDAnalysis", logfile=logfile) | ||
| logging.getLogger("MDAnalysis").info( | ||
|
jauy123 marked this conversation as resolved.
|
||
| "MDAnalysis %s STARTED logging to %r", version, logfile | ||
| create( | ||
| "MDAnalysis", | ||
| stream=stream, | ||
| level="DEBUG", | ||
| fmt="%(asctime)s %(name)-12s %(levelname)-8s %(message)s", | ||
| ) | ||
| create( | ||
| "MDAnalysis", | ||
| stream=sys.stdout, | ||
| level="INFO", | ||
| fmt="%(name)-12s: %(levelname)-8s %(message)s", | ||
| ) | ||
|
Comment on lines
+110
to
+121
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All of this is intend to replicate old behavior from the original. |
||
| logging.getLogger(__name__).info( | ||
| f"MDAnalysis {version} STARTED logging to {stream!r}" | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -112,7 +131,30 @@ def stop_logging(): | |
| clear_handlers(logger) # this _should_ do the job... | ||
|
|
||
|
|
||
| def create(logger_name="MDAnalysis", logfile="MDAnalysis.log"): | ||
| # Things to deprecated overall: | ||
| # log.NullHandler -> replace with logging.NullHandler() warning | ||
| # create() -> add_handler() (potentially confusing bc standard library | ||
| # has the same method but in camelcase weirdly?) | ||
| # or create_handler()? | ||
| # | ||
| # For create(): | ||
| # logfile -> stream (bc it could be any stream like object, not just logfiles) | ||
| # logger_name should be deprecated. | ||
| # | ||
| # Standard library recommends constructing through | ||
| # logging.getLogger(__name__) because all loggers share the same namespace | ||
| # and this is a systemmatic way of defining loggers | ||
|
Comment on lines
+144
to
+146
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that changing from |
||
| # | ||
| # __name__ is MDAnalysis | ||
| # See 2nd paragraph: https://docs.python.org/3/library/logging.html#logger-objects | ||
| # | ||
| def create( | ||
| logger_name="MDAnalysis", | ||
| stream="MDAnalysis.log", | ||
| level="DEBUG", | ||
| fmt=None, | ||
| mode="a", | ||
| ): | ||
| """Create a top level logger. | ||
|
|
||
| - The file logger logs everything (including DEBUG). | ||
|
|
@@ -130,25 +172,23 @@ def create(logger_name="MDAnalysis", logfile="MDAnalysis.log"): | |
| http://docs.python.org/library/logging.html?#logging-to-multiple-destinations | ||
| """ | ||
|
|
||
| # replaced with logging.getLogger(__name__) | ||
| logger = logging.getLogger(logger_name) | ||
|
|
||
| logger.setLevel(logging.DEBUG) | ||
|
|
||
| # handler that writes to logfile | ||
| logfile_handler = logging.FileHandler(logfile) | ||
| logfile_formatter = logging.Formatter( | ||
| "%(asctime)s %(name)-12s %(levelname)-8s %(message)s" | ||
| ) | ||
| logfile_handler.setFormatter(logfile_formatter) | ||
| logger.addHandler(logfile_handler) | ||
|
|
||
| # define a Handler which writes INFO messages or higher to the sys.stderr | ||
| console_handler = logging.StreamHandler() | ||
| console_handler.setLevel(logging.INFO) | ||
| # set a format which is simpler for console use | ||
| formatter = logging.Formatter("%(name)-12s: %(levelname)-8s %(message)s") | ||
| console_handler.setFormatter(formatter) | ||
| logger.addHandler(console_handler) | ||
| # This checks for file-like object per duck typing | ||
| # https://docs.python.org/3/library/logging.handlers.html#streamhandler | ||
| if hasattr(stream, "write") and hasattr(stream, "flush"): | ||
| handler = logging.StreamHandler(stream) | ||
| elif isinstance(stream, (str, os.PathLike)): | ||
| handler = logging.FileHandler(stream, mode=mode) | ||
| else: | ||
| raise TypeError( | ||
| "Input Stream is neither a string, PathLike object or a stream" | ||
| ) | ||
|
|
||
| handler.setLevel(level.upper()) | ||
| handler.setFormatter(logging.Formatter(fmt)) | ||
| logger.addHandler(handler) | ||
|
|
||
| return logger | ||
|
|
||
|
|
@@ -159,27 +199,10 @@ def clear_handlers(logger): | |
| (only important for reload/debug cycles...) | ||
|
|
||
| """ | ||
| for h in logger.handlers: | ||
| for h in list(logger.handlers): | ||
| logger.removeHandler(h) | ||
|
Comment on lines
+202
to
203
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a really subtle bug with the original version. See |
||
|
|
||
|
|
||
| class NullHandler(logging.Handler): | ||
| """Silent Handler. | ||
|
|
||
| Useful as a default:: | ||
|
|
||
| h = NullHandler() | ||
| logging.getLogger("MDAnalysis").addHandler(h) | ||
| del h | ||
|
|
||
| see the advice on logging and libraries in | ||
| http://docs.python.org/library/logging.html?#configuring-logging-for-a-library | ||
| """ | ||
|
|
||
| def emit(self, record): | ||
| pass | ||
|
|
||
|
|
||
|
Comment on lines
-166
to
-182
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #5368
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should deprecated instead of remove huh |
||
| class ProgressBar(tqdm): | ||
| r"""Display a visual progress bar and time estimate. | ||
|
|
||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Under semantic versioning, you need to maintain backwards compatibility. So something like