Skip to content

Logging instead of print#860

Open
astoeckel wants to merge 8 commits into
masterfrom
logging-instead-of-print
Open

Logging instead of print#860
astoeckel wants to merge 8 commits into
masterfrom
logging-instead-of-print

Conversation

@astoeckel

@astoeckel astoeckel commented Feb 7, 2017

Copy link
Copy Markdown
Contributor

This pull request fixes #290. All modules fetch a module-specific logger object which is then used to log messages previously printed with print, or logging. without a logger object.

This patchset should not significantly change the output of nengo_gui visible to the user. A future improvement would be a nice formater, which may print the log severity and other information. Note that all messages are now written to stderr instead of stdout.

Note that this patch set touches various places all over the code, it should thus be reviewed thoroughly.

Things which need to be checked before merging this pull request:

  • Were the log severities chosen correctly?
  • Does lowering the default log level have any unwanted side-effects?
  • Can the calls to the warnings-module in ipython.py be safely replaced by calls to logging? My guess was no, so I didn't change anything there.

Reason for the change of the format string is not to change the
output on the command line. A nice formater may be desirable in
the long run.
These log messages are not that important to the average user. Since
the default log level has been set to "info", the log level of these
messages has to be adapted in order to hide them from the user.

@jgosmann jgosmann left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread nengo_gui/gui.py
print("Resuming...")
logger.info("Resuming...")
else:
print("No confirmation received. Resuming...")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These last two print statements should either stay prints or should be converted to sys.stdout.write because this output is part of direct user interaction and should never be redirected to a log file for example.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I might argue that the other prints in this function should stay prints too. Though it is not as clear cut for those.

Comment thread nengo_gui/gui.py Outdated
for thread, _ in self.server.requests)
if n_zombie > 0:
print("%d zombie threads will close abruptly" % n_zombie)
logger.warning("%d zombie threads will close abruptly",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably make this a debug message. It isn't really relevant to the user.

Comment thread nengo_gui/main.py

def old_main():
print("'nengo_gui' has been renamed to 'nengo'.")
print("Please run 'nengo' in the future to avoid this message!\n")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should always be visible to the user and not end up in some log file. Thus, it should stay a print or even better go to sys.stderr.

Comment thread nengo_gui/password.py
p1 = getpass("Enter password: ")
if p0 == p1:
return p0
print("Passwords do not match. Please try again.")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also code with direct user interaction via stdin and stdout, thus it should stay a print.

Comment thread nengo_gui/guibackend.py

def log_message(self, format, *args):
logger.info(format, *args)
logger.debug(format, *args)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are “these” log messages? It seems that this function isn't called in normal operation. Maybe in some specific error cases? But if it is error cases a higher log level might be better?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is called from the web server base class and is called once for each incoming request.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, for some reason those messages did not appear on my Macbook (potentially I was running the wrong version of Nengo GUI). I agree that the debug log level makes more sense.

@jgosmann jgosmann self-assigned this Feb 8, 2017
@jgosmann jgosmann removed their assignment Feb 8, 2017
@jgosmann

jgosmann commented Feb 8, 2017

Copy link
Copy Markdown
Collaborator

Added a commit to change things back to prints where appropriate. LGTM now. 🍰

It seems safe to me to replace this `warns` call with a logging call
and it makes things consistent with the other logging call for error
output. It seems to also give a nicer output in the notebook.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

Use logging instead of print statements

2 participants