Skip to content

Commit

Permalink
docs(readme): enhance README.md formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Toilal committed Jan 6, 2021
1 parent 39d7e78 commit 3507ba3
Showing 1 changed file with 47 additions and 48 deletions.
95 changes: 47 additions & 48 deletions README.md
Expand Up @@ -10,9 +10,6 @@ Click-logging
[![Code coverage](https://img.shields.io/coveralls/github/Toilal/click-logging)](https://coveralls.io/github/Toilal/click-logging)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/relekang/python-semantic-release)


Integrates logging with click.

Project sources and documentation are available on [Github](https://github.com/Toilal/click-logging)

Documentation
Expand All @@ -23,43 +20,54 @@ Getting started

Assuming you have this Click application:

@click.command()
def cli():
click.echo("Dividing by zero.")
```python
import click

try:
1 / 0
except:
click.echo("ERROR: Failed to divide by zero.")
@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:

@click.command()
@click.option('--quiet', default=False, is_flag=True)
def cli(quiet):
if not quiet:
click.echo("Dividing by zero.")
```python
import click

try:
1 / 0
except:
click.echo("ERROR: Failed to divide by zero.")
@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:

import logging
logger = logging.getLogger(__name__)
# More setup for logging handlers here
```python
import logging
import click

@click.command()
@click.option('--quiet', default=False, is_flag=True)
def cli(quiet):
if quiet:
logger.setLevel(logging.ERROR)
else:
logger.setLevel(logging.INFO)
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.

Expand All @@ -72,29 +80,32 @@ 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:
except ZeroDivisionError:
logger.error("Failed to divide by zero.")
```

The output will look like this:

Dividing by zero.
error: Failed to divide by zero.
```
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.
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.
> 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
---
Expand All @@ -115,7 +126,7 @@ style_kwargs = {
click_logging.basic_config(logger, style_kwargs=style_kwargs)
```

You can customize [click echo](https://click.palletsprojects.com/en/7.x/api/#utilities) to stderr using `echo_kwargs` keyword argument of `basic_config` function.
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
Expand All @@ -130,18 +141,6 @@ echo_kwargs = {
click_logging.basic_config(logger, echo_kwargs=True)
```

API
---

### Classes

Indices and tables
------------------

- genindex
- modindex
- search

Fork
====

Expand Down

0 comments on commit 3507ba3

Please sign in to comment.