Skip to content
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

Improve logging #80

Merged
merged 26 commits into from
Oct 1, 2021
Merged

Improve logging #80

merged 26 commits into from
Oct 1, 2021

Conversation

jayqi
Copy link
Member

@jayqi jayqi commented Sep 19, 2021

Improves logging for nbautoexport to make things easier to troubleshoot.

There are three scenarios of use, each of which will have logging that works differently, in order of likely relevance to users:

  1. Running with Jupyter app. The logger is set to the Jupyter server logger, which we grab via a traitlets convenience method. This will log things with consistent formatting as the rest of Jupyter's logs with pretty colors.
  2. Using nbautoexport CLI. The logger named "nbautoexport" is used, and configured with a stream handler that will print to console. The --verbose/-v flag is now a counter instead of a boolean flag.
    • Default will print typer.echo statements and sets log level to WARNING.
    • -v is INFO log level.
    • -vv is DEBUG log level.
  3. Importing as library. (This isn't something users will likely do, but it is possible so this is implemented for completeness.) The logger named "nbautoexport" is used, and set with a null handler. This is best practice so that the nbautoexport logger will inherit the users' desired configuration from the root logger.

One change looking for feedback:

  • Before: Runtime error in post-save hook (e.g., if .nbautoexport config is invalid) would fail loudly (popup in Jupyter UI, see example)
  • After: Will not do anything in Jupyter UI but will still log error to the server logs

Examples of logs from Jupyter Lab

Successful startup

Jupyter Lab

startup_success

Jupyter Notebook

notebook_startup_success

Failed startup

startup_error

Successful export

Jupyter Lab

export_success

Jupyter Notebook

notebook_export_success

.nbautoexport validation error

export_error

Export with debug mode

export_debug

Example of CLI verbosity levels

cli_verbosity

Example of importing as library

demo_logging.py

import logging
from pathlib import Path

from nbautoexport.export import export_notebook
from nbautoexport.sentinel import NbAutoexportConfig

print("---DEFAULT---")

logger = logging.getLogger()
logging.basicConfig()
export_notebook(Path("notebooks/Untitled.ipynb"), NbAutoexportConfig())

print("---INFO---")

logger.setLevel(logging.INFO)
export_notebook(Path("notebooks/Untitled.ipynb"), NbAutoexportConfig())

print("---DEBUG---")

logger.setLevel(logging.DEBUG)
export_notebook(Path("notebooks/Untitled.ipynb"), NbAutoexportConfig())

Output:

---DEFAULT---
---INFO---
INFO:nbautoexport:nbautoexport | Exporting notebooks/Untitled.ipynb ...
---DEBUG---
INFO:nbautoexport:nbautoexport | Exporting notebooks/Untitled.ipynb ...
DEBUG:nbautoexport:nbautoexport | Using export configuration:
{
  "export_formats": [
    "script"
  ],
  "organize_by": "extension",
  "clean": {
    "exclude": []
  }
}
DEBUG:traitlets:Loading script exporter: python
DEBUG:traitlets:Applying preprocessor: TagRemovePreprocessor
DEBUG:traitlets:Applying preprocessor: RegexRemovePreprocessor

@jayqi jayqi linked an issue Sep 19, 2021 that may be closed by this pull request
@codecov
Copy link

codecov bot commented Sep 19, 2021

Codecov Report

Merging #80 (1d9c13b) into master (f539250) will increase coverage by 0.4%.
The diff coverage is 98.5%.

@@           Coverage Diff            @@
##           master     #80     +/-   ##
========================================
+ Coverage    98.1%   98.5%   +0.4%     
========================================
  Files           8       8             
  Lines         382     425     +43     
========================================
+ Hits          375     419     +44     
+ Misses          7       6      -1     
Impacted Files Coverage Δ
nbautoexport/jupyter_config.py 97.1% <95.4%> (+0.6%) ⬆️
nbautoexport/__init__.py 100.0% <100.0%> (ø)
nbautoexport/export.py 95.8% <100.0%> (+0.9%) ⬆️
nbautoexport/nbautoexport.py 99.2% <100.0%> (+0.8%) ⬆️
nbautoexport/sentinel.py 100.0% <100.0%> (ø)
nbautoexport/utils.py 100.0% <100.0%> (ø)

@github-actions
Copy link
Contributor

github-actions bot commented Sep 19, 2021

@jayqi jayqi marked this pull request as ready for review September 20, 2021 12:29
@jayqi jayqi requested a review from r-b-g-b September 20, 2021 12:31
@klwetstone
Copy link

@jayqi Looks very helpful! I installed it from the new branch and had no issues. A couple questions for my own understanding:

  • Is the idea that we will still set up nbautoexport the same, but when there is an error we'll (1) get more information in the logs, and (2) can manually save out the notebook in debug mode in either python or the nbautoexport cli to get more info (per the exampling of importing as library and CLI verbosity levels sections)?
  • How do we set the notebook to export with debug mode when running with the jupyter app - adding -vv to the nbautoexport configure command?

@jayqi
Copy link
Member Author

jayqi commented Sep 20, 2021

@klwetstone

Is the idea that we will still set up nbautoexport the same, but when there is an error we'll (1) get more information in the logs

Yes.

can manually save out the notebook in debug mode in either python or the nbautoexport cli to get more info (per the exampling of importing as library and CLI verbosity levels sections)

No, this is mainly just for completeness. You could always invoke nbautoexport as a library or (more commonly) with the CLI command. This just makes sure the logs behave in ways that Python devs expect if you were to do those things.

How do we set the notebook to export with debug mode when running with the jupyter app

You can run jupyter lab --debug or jupyter notebook --debug to get more logs than you ever wanted.

Copy link
Collaborator

@r-b-g-b r-b-g-b left a comment

Choose a reason for hiding this comment

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

This is excellent! One tiny tiny suggestion related to what is logged at specific levels during installation. Here's the outputs for different possible states at different log levels:

Not already installed:

(nbautoexport-dev) ➜  nbautoexport git:(74-improve-logging) nbautoexport install
nbautoexport post-save hook successfully installed with Jupyter.
If a Jupyter server is already running, you will need to restart it for nbautoexport to work.

(nbautoexport-dev) ➜  nbautoexport git:(74-improve-logging) nbautoexport install -v
2021-10-01 09:50:44,793 - nbautoexport - INFO - Installing post-save hook.
2021-10-01 09:50:44,793 - nbautoexport - INFO - nbautoexport post-save hook installed.
nbautoexport post-save hook successfully installed with Jupyter.
If a Jupyter server is already running, you will need to restart it for nbautoexport to work.

(nbautoexport-dev) ➜  nbautoexport git:(74-improve-logging) nbautoexport install -vv
2021-10-01 09:51:06,471 - nbautoexport - DEBUG - Detected existing Jupyter configuration at /home/robert/.jupyter/jupyter_notebook_config.py
2021-10-01 09:51:06,471 - nbautoexport - INFO - Installing post-save hook.
2021-10-01 09:51:06,471 - nbautoexport - INFO - nbautoexport post-save hook installed.
nbautoexport post-save hook successfully installed with Jupyter.
If a Jupyter server is already running, you will need to restart it for nbautoexport to work.

Already installed:

(nbautoexport-dev) ➜  nbautoexport git:(74-improve-logging) nbautoexport install
nbautoexport post-save hook successfully installed with Jupyter.
If a Jupyter server is already running, you will need to restart it for nbautoexport to work.

(nbautoexport-dev) ➜  nbautoexport git:(74-improve-logging) nbautoexport install -v
nbautoexport post-save hook successfully installed with Jupyter.
If a Jupyter server is already running, you will need to restart it for nbautoexport to work.

(nbautoexport-dev) ➜  nbautoexport git:(74-improve-logging) nbautoexport install -vv
2021-10-01 09:49:21,364 - nbautoexport - DEBUG - Detected existing Jupyter configuration at /home/robert/.jupyter/jupyter_notebook_config.py
2021-10-01 09:49:21,365 - nbautoexport - DEBUG - Detected existing nbautoexport post-save hook.
2021-10-01 09:49:21,366 - nbautoexport - DEBUG - Existing post-save hook is version 0.3.1+32.g5d71683
2021-10-01 09:49:21,366 - nbautoexport - DEBUG - No changes made.
nbautoexport post-save hook successfully installed with Jupyter.
If a Jupyter server is already running, you will need to restart it for nbautoexport to work.

Already installed different version:

(nbautoexport-dev) ➜  nbautoexport git:(74-improve-logging) nbautoexport install
nbautoexport post-save hook successfully installed with Jupyter.
If a Jupyter server is already running, you will need to restart it for nbautoexport to work.

(nbautoexport-dev) ➜  nbautoexport git:(74-improve-logging) ✗ nbautoexport install -v
2021-10-01 09:56:38,983 - nbautoexport - INFO - Updating nbautoexport post-save hook with version 0.3.1+32.g5d71683...
2021-10-01 09:56:38,984 - nbautoexport - INFO - nbautoexport post-save hook installed.
nbautoexport post-save hook successfully installed with Jupyter.
If a Jupyter server is already running, you will need to restart it for nbautoexport to work.

(nbautoexport-dev) ➜  nbautoexport git:(74-improve-logging) ✗ nbautoexport install -vv
2021-10-01 09:56:51,727 - nbautoexport - DEBUG - Detected existing Jupyter configuration at /home/robert/.jupyter/jupyter_notebook_config.py
2021-10-01 09:56:51,727 - nbautoexport - DEBUG - Detected existing nbautoexport post-save hook.
2021-10-01 09:56:51,729 - nbautoexport - DEBUG - Existing post-save hook is version 0.2.1+32.g5d71683
2021-10-01 09:56:51,729 - nbautoexport - INFO - Updating nbautoexport post-save hook with version 0.3.1+32.g5d71683...
2021-10-01 09:56:51,730 - nbautoexport - INFO - nbautoexport post-save hook installed.
nbautoexport post-save hook successfully installed with Jupyter.
If a Jupyter server is already running, you will need to restart it for nbautoexport to work.

For the "Already installed" state, the output for no verbosity flag and -v are the same. Besides that, as a user, I might expect that nbautoexport install -v would tell me whether it's installing or whether it detects an existing installation. My suggestion would just be to promote:

logger.debug("Detected existing nbautoexport post-save hook.")

logger.debug("No changes made.")

from debug to info. Then the output for nbautoexport install -v would be:

(nbautoexport-dev) ➜  nbautoexport git:(74-improve-logging) nbautoexport install -v
2021-10-01 09:49:21,364 - nbautoexport - INFO - Detected existing Jupyter configuration at /home/robert/.jupyter/jupyter_notebook_config.py
2021-10-01 09:49:21,366 - nbautoexport - INFO - No changes made.
nbautoexport post-save hook successfully installed with Jupyter.
If a Jupyter server is already running, you will need to restart it for nbautoexport to work.

nbautoexport/__init__.py Show resolved Hide resolved
nbautoexport/__init__.py Show resolved Hide resolved
@jayqi
Copy link
Member Author

jayqi commented Oct 1, 2021

Thanks for the review @r-b-g-b, I incorporated your suggestions.

@jayqi jayqi requested a review from r-b-g-b October 1, 2021 19:00
Copy link
Collaborator

@r-b-g-b r-b-g-b left a comment

Choose a reason for hiding this comment

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

Awesome!

@jayqi jayqi merged commit dd57020 into master Oct 1, 2021
@jayqi jayqi deleted the 74-improve-logging branch October 1, 2021 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve startup/runtime logging
3 participants