Skip to main content

Enable glog-style vmodule for python's logging

Project description

vmodule

This is a tiny project with no deps to add extra levels to stdlib logging, and also something you can run from your main that processes logger-specific verbosity.

This is inspired by glog, which considers VLOG(1) to be a more restrictive version of LOG(INFO). For simplicity we just map that onto numeric severities.

Here's a combined demo of how you might use this library -- we don't depend on click but you certainly can:

from logging import getLogger
from typing import Optional

import click

from vmodule import VLOG_1, VLOG_2, vmodule_init

LOG = getLogger(__name__)


@click.command()
@click.option("-v", type=int)
@click.option("--vmodule")
def main(v: Optional[int], vmodule: Optional[str]) -> None:
    vmodule_init(v, vmodule)
    LOG.warning("Warn")
    LOG.info("Starting up")
    LOG.log(VLOG_1, "Verbose %d", 1)
    LOG.log(VLOG_2, "Verbose %d", 2)
    LOG.debug("foo")


if __name__ == "__main__":
    main()

If you're writing a library, it's even easier:

from logging import getLogger
from vmodule import VLOG_1

LOG = getLogger(__name__)

def foo(...):
    LOG.info("Starting up")
    LOG.log(VLOG_1, "Verbose %d", 1)

Command line parsing

If you use the example above,

(unset)  -> WARNING
-v 0     -> INFO
-v 1     -> VLOG_1
-v 2     -> VLOG_2
-v 3     -> VLOG_3
-v 10    -> DEBUG (by accident)

You can also specify those same numbers with --vmodule although there are two gotchas:

  1. You CLI's logger is quite possibly called "__main__".
  2. This doesn't take wildcards or use hierarchy. You need to specify actual logger names.
--vmodule filelock=10,concurrent.futures=0

sets filelock to DEBUG
sets concurrent.futures to INFO

Version Compat

Usage of this library should work back to 3.7, but development (and mypy compatibility) only on 3.10-3.12. Linting requires 3.12 for full fidelity.

License

vmodule is copyright Tim Hatch, and licensed under the MIT license. I am providing code in this repository to you under an open source license. This is my personal repository; the license you receive to my code is from me and not from my employer. See the LICENSE file for details.

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

vmodule-0.5.0.tar.gz (7.0 kB view hashes)

Uploaded Source

Built Distribution

vmodule-0.5.0-py3-none-any.whl (5.7 kB view hashes)

Uploaded Python 3

Supported by

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