Click-logging
=============
**Simple and beautiful logging for click applications**
[](https://pypi.org/project/click-logging/)
[](https://pypi.org/project/click-logging/)
[](https://github.com/Toilal/click-logging/blob/develop/LICENSE)
[](https://github.com/Toilal/click-logging/actions?query=workflow%3Abuild)
[](https://coveralls.io/github/Toilal/click-logging)
[](https://github.com/relekang/python-semantic-release)
Project sources and documentation are available on [Github](https://github.com/Toilal/click-logging)
Documentation
=============
Getting started
---------------
Assuming you have this Click application:
```python
import click
@click.command()
def cli():
click.echo("Dividing by zero.")
try:
1 / 0
except ZeroDivisionError:
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:
```python
import click
@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 ZeroDivisionError:
click.echo("ERROR: Failed to divide by zero.")
```
Wrapping if-statements around each `echo`-call is cumbersome though. And with that, we discover logging:
```python
import logging
import click
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:
```python
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 ZeroDivisionError:
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 `simple_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](https://click.palletsprojects.com/en/7.x/api/#utilities) for each log level with
`style_kwargs` keyword argument of `basic_config` function.
```python
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](https://click.palletsprojects.com/en/7.x/api/#utilities) for each log level using `echo_kwargs` keyword argument of `basic_config` function.
```python
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)
```
Fork
====
This is a fork of [click-contrib/click-log](https://github.com/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
=========
<!--next-version-placeholder-->
## v1.0.1 (2021-01-06)
### Fix
* Add missing string interpolation in error message ([`3de6f06`](https://github.com/Toilal/click-logging/commit/3de6f064a8b4b3e11b7b4df9f47c2e5fce33abe1))
* **help:** Add trailing dot to help text ([`1ed426f`](https://github.com/Toilal/click-logging/commit/1ed426f2b128b0d934a59fe6c28e19af6aa0cb46))
### Documentation
* **readme:** Enhance README.md formatting ([`3507ba3`](https://github.com/Toilal/click-logging/commit/3507ba3d39e3318baf9cc9f4fbc65e7a39f8787a))
## v1.0.0 (2021-01-06)
### Feature
* **customize:** Add more customization capabilities in basic_config ([`621c0a2`](https://github.com/Toilal/click-logging/commit/621c0a2b4532f0dacade1e031f0ed5c2174269ae))
### Breaking
* click_log has been renamed to click_logging ([`f4a8ddf`](https://github.com/Toilal/click-logging/commit/f4a8ddf83fa6e1f7197e458662f89c303f5dc606))