No project description provided
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file logargparser-0.1.1.tar.gz
.
File metadata
- Download URL: logargparser-0.1.1.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.4 Darwin/21.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6626fc2e72b116dcd83beb790d9ce0af138b9fdab64e28e25a19accdf65b26d |
|
MD5 | 53e47ea895f4cec45b30fe2fdf5aa98f |
|
BLAKE2b-256 | 04073ab8f90a07f3a808cedea84e79293ab268ad0f19e15e2ff695983e8ee01e |
File details
Details for the file logargparser-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: logargparser-0.1.1-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.4 Darwin/21.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 182eefc844ba41c534a30df4114fcc04dcc60c5d20e19eb915a831a30d386c72 |
|
MD5 | 0dff57be9a449206dd687fa64deafc3c |
|
BLAKE2b-256 | ce802e808b4afcde758d8010ef471a8c593115ba1c586ba29e1d8f4a413ee0a9 |