Skip to main content

logargparser

Utility for the common case of managing logs from the command line in simple utilities.

Sample usage

In a python script called sample.py:

import logging
from logargparser import LoggingArgumentParser
        
logger = logging.getLogger(__name__)
        
def main():
    # Instead of the normal `parser = argparse.ArgumentParser.
    parser = LoggingArgumentParser(logger)
        
    # Add as many other arguments as you want.
    parser.add_argument('-o', "--output")
        
    args = parser.parse_args()
        
    logger.info("The logger was set up for us based on --log and/or --logfile command line args.")
        
    if args.output is not None:
        # Do something....
        pass
        
        
if __name__ == "__main__":
    main()

We can then invoke the script from the command line. First, to get help on all args:

python -m sample -h

The output will be:

usage: sample.py [-h] [--log {DEBUG,INFO,WARNING,ERROR,CRITICAL}]
                 [--logfile LOGFILE] [-o OUTPUT]

options:
  -h, --help            show this help message and exit
  --log {DEBUG,INFO,WARNING,ERROR,CRITICAL}
                        Logging level.
  --logfile LOGFILE     Optional file path that logs should be appended to.
                        The file will be created if it does not exist.
  -o OUTPUT, --output OUTPUT

Notice that everything worked like a normal script using argparse.ArgParser, but in addition to the -o/--output argument we created ourselves, there are two other arguments --log and --logfile available.

We can use --log as follows:

python -m sample --log INFO

This will produce the output

INFO:__main__:The logger was set up for us based on --log and/or --logfile command line args.

Notice that this output appears on the standard error output. It is generated by the logger.info statement in the python code above.

If we want this output to be appended to a log file instead of being written to standard error, we can run

python -m sample --log INFO --logfile mylog.log

Now when we run it the output will go to the file we specifcied, just as the help said it would.

Metadata

Release files for logargparser 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for logargparser 0.1.1
File Size Uploaded
logargparser-0.1.1.tar.gz 9.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for logargparser 0.1.1
File Interpreter ABI Platform
logargparser-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.2 kB

Release files / logargparser-0.1.1.tar.gz

Download URL logargparser-0.1.1.tar.gz
Size 9.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c6626fc2e72b116dcd83beb790d9ce0af138b9fdab64e28e25a19accdf65b26d
BLAKE2b-256 checksum
How to use checksums
04073ab8f90a07f3a808cedea84e79293ab268ad0f19e15e2ff695983e8ee01e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.4 Darwin/21.6.0

Release files / logargparser-0.1.1-py3-none-any.whl

Download URL logargparser-0.1.1-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
182eefc844ba41c534a30df4114fcc04dcc60c5d20e19eb915a831a30d386c72
BLAKE2b-256 checksum
How to use checksums
ce802e808b4afcde758d8010ef471a8c593115ba1c586ba29e1d8f4a413ee0a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.4 Darwin/21.6.0

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page