Skip to main content

`nnlogging2` is a non-invasive extension of the standard Python logging library. `nnlogging2` is designed to track numerical metrics by time and execution context.

Project description

nnlogging2

status-badge PyPI - Version PyPI - Downloads PyPI - Python Version

nnlogging2 is a non-invasive extension of the standard Python logging library. nnlogging2 is designed to track numerical metrics by time and execution context.

  • Metric Tracking: Fast automatic metric statistics tracking.
  • Extensibility: Fork metric, logger, handler or formatter at any time.
  • Compatibility: Compatible with standard python logging library and third-party modules.

Preview

assets/preview.png


Installation

pip install nnlogging2

Preview Script

from nnlogging2 import (
    get_metric_logger,
    ContextedMetricFormatter,
    AttyStreamHandler,
    level_to_int,
    modify_logger_fallback_attrs_recorded,
)

# 1. Initialize the promoted logger

modify_logger_fallback_attrs_recorded(("lastval", "mean", "median"))
logger = get_metric_logger("train")
logger.setLevel(level_to_int("INFO"))

# 2. Setup formatting for context and metrics
# Inject "%(context_ext)s" and "%(metrics_ext)s" to formatter
# (8 showing characters, 9 ANSI escaped codes)
formatter = ContextedMetricFormatter(
    fmt="%(levelname)-17s %(context_ext)s %(message)s%(metrics_ext)s"
)
handler = AttyStreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)

# 3. Attach context to logger (optional)
ctx_logger = logger.contexted({"epoch": "E1/1"})

# 4. Standard logging
ctx_logger.debug("debug ...")
ctx_logger.info("info ...")
ctx_logger.warning("warning ...")
ctx_logger.error("error ...")
ctx_logger.critical("critical ...")

# 5. Update & Logging
for i in range(3):
    # Update numerical values (automatically tracked in a moving window)
    ctx_logger.update(loss=0.5 / (i + 1), accuracy=0.8 + (i * 0.05))
    ctx_logger.log_metric(
        level_to_int("INFO"),
        metrics=[
            ("loss", "[%(metricname)s: %(lastval).2f (%(mean).2f)]"),
            ("accuracy", "[%(metricname)s: %(median).2f]"),
        ],
    )

License

This project is licensed under the MIT License LICENSE.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

nnlogging2-0.1.1-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file nnlogging2-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: nnlogging2-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for nnlogging2-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 703730daf2b79478c33ad72123f999a2319c045cc45e87da8fd9294921101c9a
MD5 b82befc0b0cb93282b1d8816fa64a088
BLAKE2b-256 2d56f44da3b327343d2685a5a6b5dfea8295a00e9a836b36772ad7a87f4b1044

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