Skip to main content

notebooklog

Project description

notebooklog

CI

notebooklog is a small Python helper for capturing notebook or script output in a log file without hiding it from the user. It configures Python logging and also copies direct stdout/stderr writes, so messages from print, uncaught tracebacks, warnings, and regular loggers can land in the same timestamped file.

The package is intended for long-running notebooks and scripts where the visible session output is useful in the moment, but a durable record is needed afterward.

Installation

pip install notebooklog

notebooklog supports Python 3.8+ and depends on ipynbname and python-slugify.

Usage

import logging
from pathlib import Path

from notebooklog import setup_logger

logger, log_path = setup_logger(
    Path("logs"),
    name="analysis-run",
    log_level=logging.INFO,
)

logger.info("Starting analysis")
print("Printed output is copied to the same log file")

print(f"Writing log to {log_path}")

setup_logger() returns a named child logger plus the path to the log file it created. If name is omitted, the package tries to infer one from the current Jupyter notebook, the HEADLESS_NOTEBOOK_NAME environment variable, the running script filename, or a fallback name for interactive sessions.

How it works

Calling setup_logger(log_dir, name=None, log_level=logging.INFO):

  • creates log_dir if it does not already exist;
  • creates a UTC timestamped log file named like YYYYMMDD_HH-MM-SS.<slugified-name>.log;
  • configures the root logger with a formatted stderr handler at the requested log level;
  • enables logging.captureWarnings(True);
  • copies direct stdout and stderr writes into the log file.

In a normal Python process, sys.stdout and sys.stderr are wrapped with a pass-through stream that writes to both the original stream and the log file. In a Jupyter notebook, the package leaves the active notebook streams in place and attaches file writers through their echo hooks so later cell output is still shown in the correct cell.

Important behavior

setup_logger() changes process-wide logging and standard stream state. Call it once near the start of a notebook or script; repeated calls add more root logging handlers and can duplicate formatted log messages.

The log directory is created with Path.mkdir(exist_ok=True), so parent directories must already exist. The package exposes an importable API only; it does not define a command-line interface.

Development

pip install -r requirements_dev.txt
pip install -e .
make test
make lint
make docs

The repository also includes Sphinx documentation configuration, GitHub Actions CI, coverage settings, and packaging metadata for building and publishing the Python package.

Changelog

0.0.1

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

notebooklog-0.0.3.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

notebooklog-0.0.3-py2.py3-none-any.whl (7.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file notebooklog-0.0.3.tar.gz.

File metadata

  • Download URL: notebooklog-0.0.3.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for notebooklog-0.0.3.tar.gz
Algorithm Hash digest
SHA256 8f1de78d65639655324f77a3dac552fdc8941d4157806fc8285f1b3175896726
MD5 f3ffc65f636af537d65022d239c5e855
BLAKE2b-256 95689eb31afbc847a067485190ee6a0049d06f6e18e49227605b9bcf34f67d3e

See more details on using hashes here.

File details

Details for the file notebooklog-0.0.3-py2.py3-none-any.whl.

File metadata

  • Download URL: notebooklog-0.0.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for notebooklog-0.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d3699dc22c49fdb65c576b980f13085294a9d12663f56902c889db8ac36a0f7a
MD5 b39e9f78d23cde66575876deeeeb4c5e
BLAKE2b-256 9dcda2307698028be13b907c5f001259d1d13685e74b225af2da99ef8405aa8d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page