Skip to main content

best practice low-config python logging

Project description

vcdilog: logging as easy as print statements

You almost certainly should be using log statement instead of messy print statements. vcdilog makes this easy.

Install

You can install vcdilog using pip:

$ pip install vcdilog

If using poetry, simply add this line to your pyproject.toml file.

vcdilog = "*"

Philosophy

The built in python logging library is highly customizable, but hard to visualize and understand. vcdilog doesn't try to replace what is in the standard library, but streamlines it with sensible defaults.

Basic, instead-of-print-statements usage

All you need is two lines of setup: One import, and one line to add a handler for stdout/stderr output.

from vcdilog import log
log.add_std_handler()

log.info('hello world')  # .info, .warning, .debug, etc

More flexible usage

You can create a log object:

from vcdilog import Logger

log = Logger()

Crucially, you don’t need to give the logger a name, it will figure out the current module name automatically and use that. This same method is used by the print-statements example above - they'll be logged to an appropriately-named logger automatically, based on the module they're in.

You can set a log level with:

log.set_level(level="DEBUG")

DEBUG means all logged messages will actually get logged. When you add a handler with e.g. add_std_handler, the handler will be assigned the DEBUG level by default, and so will the associated logger.

For production usage, a lower level should ordinarily be set to avoid overly verbose logging.

Other handlers

So far three handlers are supported.

x.add_std_handler()
x.add_null_handler()
x.add_json_handler()

As per the above, the standard output handler logs to the console. It uses some customization log errors to stderr and normal messages to stdout, just like print() does, which means you can use logging calls instead of print statements.

The null handler should be used in libraries - it’s best practice when publishing a library to create a logger but add only a null handler and no others to that users of the library don’t get unwanted log messages (if they want to see them, they’ll add their own handlers).

Then there’s the json handler. That logs json-formatted log items to a file.

For instance, the following:

log.info("classic message", extra={"special": "value", "run": 12})

Will result in:

{"message": "classic message", "special": "value", "run": 12}

In the log file (log.json by default)

Basic prefect support

Getting a prefect logger is as easy as:

prefect_logger = vcdilog.get_prefect_logger()

and you can do...

vcdilog.set_prefect_extra_loggers(['boto3', 'whatever'])

to have those loggers inherit the prefect logging config.

More prefect support is on the way!

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

vcdilog-0.2.5.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

vcdilog-0.2.5-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file vcdilog-0.2.5.tar.gz.

File metadata

  • Download URL: vcdilog-0.2.5.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.8.10 Linux/5.4.0-1049-azure

File hashes

Hashes for vcdilog-0.2.5.tar.gz
Algorithm Hash digest
SHA256 402941fb6a6e0d6b86c5a059154af8c48070b3ee71b95f6a870b7586b728c97e
MD5 9e0eba0a1d4e0c9478a45d5bf21d8e73
BLAKE2b-256 f66f6bc8c10d5c54e0e82b5417b2070ce7490c2ea1eafec5add1837880295043

See more details on using hashes here.

File details

Details for the file vcdilog-0.2.5-py3-none-any.whl.

File metadata

  • Download URL: vcdilog-0.2.5-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.8.10 Linux/5.4.0-1049-azure

File hashes

Hashes for vcdilog-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 3e6c5396fcd1e2f77805f0df390fb772c2b568687f699f44432f6f572974d72f
MD5 777fa4588c7123cf149a7f0ae240105e
BLAKE2b-256 cc390fb1b3535b8c59cb87764762db7bbeb280706ed3d6dda0395cb14bc55bf9

See more details on using hashes here.

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