Skip to main content

Helper library to configure logging for python

Project description

Logging for Data Pipelines

This library simplifies the logging setup process for new data pipelines, eliminating the need to repeatedly look up logger configurations.

Installation

Install the library via pip:

pip install log4data

Usage

To start using the library, simply import it in your script, and use the default configuration if you don't want to manually configure the logger.

import src.log4data.main as l4d
import logging as lg


if __name__ == "__main__":
    l4d.default_setup_logger()
    lg.info("Setup complete")

With just a few lines of code, you can set up effective logging. This will generate a log like this:

2024-07-03 00:00:00,000 - root - INFO - We are logging!

A more advanced use of the library is parametrize the file name or the logging level like this:

# main.py
import log4data as l4d
import logging as lg


if __name__ == "__main__":
    args = l4d.set_log_args(return_args=True)
    l4d.setup_logger_from_args(args)

    lg.info("Setup complete")

And then call python:

python main.py -lglv debug -lgfn etl.log

Finally, @inject_logger is a decorator to automatically add a logger to a function, named after that function. This is used like this:

# main.py
import argparse
import logging as lg
import log4data as l4d


@l4d.inject_logger
def my_data_processing_function(data, logger=None):
    logger.info(f"Processing data: {data}")
    print(data)
    return data


if __name__ == "__main__":
    # parse arguments
    parser = argparse.ArgumentParser()
    parser.add_argument("-d", "--data", type=str, default="sample data")
    l4d.set_log_args(parser)
    args = parser.parse_args()

    # Configure logging
    l4d.setup_logger_from_args(args)

    # Call the function without providing the logger manually
    my_data_processing_function(args.data)

Then, calling this:

python main.py -d "Hello log for data."

Results in this log:

2024-07-03 00:00:00,000 - my_data_processing_function - INFO - Processing data: Hello log for data.

Contributing

This is a small project developed for my team and me, and while major upgrades are not planned, we welcome pull requests. If you encounter bugs or wish to suggest new features, please initiate a discussion by opening an issue.

License

This repository is under the MIT 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 Distribution

log4data-0.1.2.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

log4data-0.1.2-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file log4data-0.1.2.tar.gz.

File metadata

  • Download URL: log4data-0.1.2.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for log4data-0.1.2.tar.gz
Algorithm Hash digest
SHA256 1de7a79fe14794286e01b8855e7badfb73cfa03ab11ad518f783420b89f72830
MD5 de25209d87a762446194c5b042a399ba
BLAKE2b-256 fb7150a5ebe9291d30c68d1edcc29131dc6747afe9ed1bfd1c26bc4e54e71cf5

See more details on using hashes here.

File details

Details for the file log4data-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: log4data-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for log4data-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f6c96ee29f17f7da0fd241b2b7582d5f24d53b2e441b19d6ce77f57a9b944f89
MD5 4d75f52395ee97c7773b0b831ddeeee9
BLAKE2b-256 536829b64d9aba0a80f565301f7685a295f46177c5347f6d180c54a49f6272ff

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