Quick and easy logging parameters for click commands.
Project description
Quick and easy logging parameters for click commands.
1 Installation
The click_logging_config package is available from PyPI. Installing into a virtual environment is recommended.
python3 -m venv .venv; .venv/bin/pip install click_logging_config
2 Getting Started
Using click_logging_config is intended to be very simple. A single decorator applied to your click command or group adds some click options specifically for managing logging context.
import click
import logging
from click_logging import logging_parameters
log = logging.getLogger(__name__)
def do_something()
pass
@click.command()
@click.option("--my-option", type=str)
# NOTE: Empty braces are required for hard-coded click-logging-config defaults.
@logging_parameters()
def my_command(my_option: str) -> None:
log.info("doing something")
try:
do_something(my_option)
except Exception as e:
log.critical(f"something bad happened, {str(e)}")
raise
Application of the @logging_parameters decorator must be applied immediately above your click command function and below any other click decorators such as arguments and options.
Having applied the decorator your command now has the following options available to it.
--log-console-enable / --log-console-disable
Enable or disable console logging.
[default: log-console-disable]
--log-console-json-enable / --log-console-json-disable
Enable or disable console JSON logging.
[default: log-console-json-disable]
--log-file-enable / --log-file-disable
Enable or disable file logging.
[default: log-file-enable]
--log-file-json-enable / --log-file-json-disable
Enable or disable file JSON logging.
[default: log-file-json-enable]
--log-file FILE The log file to write to. [default: this.log]
--log-level [critical|error|warning|info|debug|notset]
Select logging level to apply to all enabled
log sinks. [default: warning]
If you don’t like the click-logging-config internal defaults for the options you can define your own. The LoggingConfiguration class is derived from pydantic.BaseModel, so one easy way to define your defaults is using a dictionary. You only need to define values to want to change - any other value will continue using the internal defaults.
import pathlib
import click
import logging
from click_logging import logging_parameters, LoggingConfiguration
log = logging.getLogger(__name__)
MY_LOGGING_DEFAULTS = LoggingConfiguration.parse_obj(
{
"file_logging": {
# NOTE: file path must be specified using pathlib.Path
"log_file_path": pathlib.Path("some_other.log"),
},
"log_level": "info",
}
)
def do_something()
pass
@click.command()
@click.option("--my-option", type=str)
@logging_parameters(MY_LOGGING_DEFAULTS)
def my_command(my_option: str) -> None:
log.info("doing something")
try:
do_something(my_option)
except Exception as e:
log.critical(f"something bad happened, {str(e)}")
raise
Review the LoggingConfiguration class definition to understand all the default configuration parameters available.
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
Hashes for click_logging_config-0.2.1.post1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12e76849259b5a0f2f7e03c408f7d63b08a35b280aa848980192b12dc37978d4 |
|
MD5 | e0957433bc9a49b4206c42b4227b5452 |
|
BLAKE2b-256 | 0196f78d11abae2dd55477fd5cae3135ecaff4abd595b9d859a1328f4555d0fe |
Hashes for click_logging_config-0.2.1.post1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3553df7d1811bd2178fc73846c5cf38e24a49c3d1de974c83750501b188194e2 |
|
MD5 | dbbd5563868ae1973c89184a0a4a9aa3 |
|
BLAKE2b-256 | bce89555d0ac9b0406beab0a1daf71f34ec639ce63b08f4262990ac3d96acf82 |