Skip to main content

Quick and easy logging parameters for click commands.

Project description

Quick and easy CLI logging options for click commands using a Python decorator.

I found myself implementing logging preferences repeatedly for utilities. Logging configuration is pretty simple, but for each new implementation I would find myself spending time researching the same options to refresh my memory and then implementing something slightly different than the last time. 🙄

click-logging-config is my attempt to stop the circle of re-implementation with settings that are useful enough, with configurability to change it if you don’t like the out-of-box behaviour. It’s proving to be pretty useful and I’m already using it across several of my other projects. 😄

It is released under the MIT license so you are free to use it in lots of different ways. As simple as it looks, a tool like this still represents research time and implementation effort, so please use the link below to help support development.

Support click-logging-config

Byting Chipmunk 🐿

Take a byte off.

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]

Note that the single log level configuration parameter applies to both console and file logging.

The internal defaults are configured for an interactive utility (run by a human in a terminal rather than via automation, or in a container). In summary,

  • disabled console logging (allows your application to use console output, if needed)

  • enabled file logging (1MB rotation size, with 10 rotation backups)

  • “warning” log level

3 Custom defaults

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 you 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

The table below summarizes the available settings for defaults. Otherwise review the LoggingConfiguration class definition .

Available top-level settings for logging defaults.

Setting

Type

Hard default

Description

log_level

str

warning

Define log level

enable_console_logging

boolean

False

Enable console logging

console_logging

dict

Console logging specific settings. See table below.

enable_file_logging

bool

True

Enable file logging

file_logging

dict

File logging specific settings. See table below.

Available console logging defaults.

Setting

Type

Hard default

Description

json_enabled

bool

False

Output JSON logs using json_log_formatter

Available file logging defaults.

Setting

Type

Hard default

Description

json_enabled

bool

True

Output JSON logs using json_log_formatter

log_file_path

pathlib.Path

./this.log

Path and name of log file.

file_rotation_size_megabytes

int

1

Maximum size of

max_rotation_backup_files

int

10

Maximum number of rotation backup files

4 Console logging

Console logging can be enabled or disabled, and there is an additional option to output line-by-line text based timestamped log entries, or JSON logging via the json_log_formatter framework. The format of text based log entries cannot be configured at this time and console logging is always emitted to stderr at this time.

5 File logging

File rotation on the file log is implemented as a “sensible default” - it cannot be disabled at this time, although you might be able to specify a maximum rotation of 1 to achieve the same end (not tested). The maximum rotation size can be specified as a configuration default. File logging itself can be enabled or disabled via defaults or the CLI options described above.

Similar to console logging the format can be as either text-based or JSON logging.

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

click_logging_config-1.1.0.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

click_logging_config-1.1.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file click_logging_config-1.1.0.tar.gz.

File metadata

  • Download URL: click_logging_config-1.1.0.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for click_logging_config-1.1.0.tar.gz
Algorithm Hash digest
SHA256 5ac21844812fcdbba428db63ddd5d2cb9403c29793d2d7b394a15f011f21ab7a
MD5 0e765c0a1261180de88a80faf05bff09
BLAKE2b-256 91f1bddaac0bc20808241fd62f6b97e9e7a4b70156c95b6a42849d2bbf812d6e

See more details on using hashes here.

File details

Details for the file click_logging_config-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for click_logging_config-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 205719132d7c0779f46c191fef29ecba5ff8f224760890410eebed2600ca9dba
MD5 3870e65ed8af71d479bb2da2c0487be6
BLAKE2b-256 9d7de6817cd3745bbfeeeb2f0c4af4e10d028ceeda11ff03b6de30af258fbc37

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