Logging integration for Click
Project description
Click-logging
Simple and beautiful logging for click applications
Integrates logging with click.
Project sources and documentation are available on Github
Documentation
Getting started
Assuming you have this Click application:
@click.command()
def cli():
click.echo("Dividing by zero.")
try:
1 / 0
except:
click.echo("ERROR: Failed to divide by zero.")
Ignore the application's core functionality for a moment. The much more pressing question here is: How do we add an option to not print anything on success? We could try this:
@click.command()
@click.option('--quiet', default=False, is_flag=True)
def cli(quiet):
if not quiet:
click.echo("Dividing by zero.")
try:
1 / 0
except:
click.echo("ERROR: Failed to divide by zero.")
Wrapping if-statements around each echo
-call is cumbersome though. And with that, we discover logging:
import logging
logger = logging.getLogger(__name__)
# More setup for logging handlers here
@click.command()
@click.option('--quiet', default=False, is_flag=True)
def cli(quiet):
if quiet:
logger.setLevel(logging.ERROR)
else:
logger.setLevel(logging.INFO)
...
Logging is a better solution, but partly because Python's logging module aims to be so generic, it doesn't come with sensible defaults for CLI applications. At some point you might also want to expose more logging levels through more options, at which point the boilerplate code grows even more.
This is where click-logging comes in:
import logging
import click
import click_logging
logger = logging.getLogger(__name__)
click_logging.basic_config(logger)
@click.command()
@click_logging.simple_verbosity_option(logger)
def cli():
logger.info("Dividing by zero.")
try:
1 / 0
except:
logger.error("Failed to divide by zero.")
The output will look like this:
Dividing by zero.
error: Failed to divide by zero.
The error:
-prefix will be red, unless the output is piped to another command.
The :pysimple_verbosity_option decorator adds a --verbosity
option that takes a (case-insensitive) value of DEBUG
, INFO
, WARNING
, ERROR
, or CRITICAL
, and calls setLevel
on the given logger accordingly.
note
Make sure to define the simple_verbosity_option as early as possible. Otherwise logging setup will not be early enough for some of your other eager options.
Customize output
You can customize click styles for each log level with
style_kwargs
keyword argument of basic_config
function.
import logging
import click_logging
logger = logging.getLogger(__name__)
style_kwargs = {
'error': dict(fg='red', blink=True),
'exception': dict(fg='red', blink=True),
'critical': dict(fg='red', blink=True)
}
click_logging.basic_config(logger, style_kwargs=style_kwargs)
You can customize click echo to stderr using echo_kwargs
keyword argument of basic_config
function.
import logging
import click_logging
logger = logging.getLogger(__name__)
echo_kwargs = {
'error': dict(err=True),
'exception': dict(err=True),
'critical': dict(err=True),
}
click_logging.basic_config(logger, echo_kwargs=True)
API
Classes
Indices and tables
- genindex
- modindex
- search
Fork
This is a fork of click-contrib/click-log
Motivations of the fork:
- semantic-release and github actions to merge and release often.
- more configuration options.
License
Licensed under the MIT, see LICENSE
.
Changelog
v1.0.0 (2021-01-06)
Feature
- customize: Add more customization capabilities in basic_config (
621c0a2
)
Breaking
- click_log has been renamed to click_logging (
f4a8ddf
)
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-1.0.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae2fa94e7bb7a2774a8692776f4d83d4dbd549cc595d591643701faf628a1aa9 |
|
MD5 | 0dd2d20ca9c40dcd1c5f26862a8712b8 |
|
BLAKE2b-256 | 2c5c355fac9a04182d6e41a874e13e1c92dc3d7e4ae03523df6864c0fff6e6b9 |