Skip to content

Commit

Permalink
Set logging level with 'verbosity' option for Django
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Oct 26, 2018
1 parent 4d45f90 commit 106f039
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 69 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## 1.1 (unreleased)

- Added `%(relpath)s` logging format.
- Added `verbosity` as `init()` option to work with Django admin commands.

## 1.0 (2018-09-27)

Expand Down
65 changes: 0 additions & 65 deletions docs/api.md

This file was deleted.

55 changes: 55 additions & 0 deletions docs/extras.md
@@ -0,0 +1,55 @@
# Initialization

To customize logging formats and levels, `minilog` supports the same initialization arguments as [`logging.basicConfig`](https://docs.python.org/3/library/logging.html#logging.basicConfig). To set the format for all logging handlers:

```python
log.init(format="%(levelname)s: %(name)s: %(message)s")

```

To set the level for the root logging handler:

```python
log.init(format=<>, level=log.WARNING)
```

### Debug Option

To simply enable debug-level logging, a convenience option is provided:

```python
log.init(format=<>, debug=True)
```

### Verbosity Option

To work with frameworks that provide a `verbosity` level in their CLI frameworks (such as [Django](https://docs.djangoproject.com/en/2.1/ref/django-admin/#cmdoption-verbosity)), that can be used instead:

```python
log.init(format=<>, verbosity=verbosity)
```

### Silencing Loggers

Set the logging level for specific named loggers:

```python
log.silence('selenium')
log.silence('werkzeug', 'requests', allow_warning=True)
```

### Reset Loggers

Finally, if another package has already set the logging format or level, that can be reset so that `minilog` takes over:

```python
log.init(…, reset=True)
```

# Records

In addition to the standard [`LogRecord`](https://docs.python.org/3/library/logging.html#logrecord-attributes) attributes, the following additional patterns are available:

| Logging Format | Description
| --- | --- |
| `%(relpath)s` | Full pathname of the source file where the logging call was issued relative to the current working directory. |
28 changes: 28 additions & 0 deletions docs/logging.md
@@ -0,0 +1,28 @@
# API

This package intends to be a drop-in replacement for `logging.Logger` objects. It supports the standard logging API:

```python
log.debug(message, *args)
log.info(message, *args)
log.warning(message, *args)
log.error(message, *args)
log.critical(message, *args)

log.exception(message, *args)

log.log(level, message, *args)
```

As well as convenience methods:

```python
log.warn(message, *args) # warning

log.d(message, *args) # debug
log.i(message, *args) # info
log.w(message, *args) # warning
log.e(message, *args) # error

log.exc(message, *args) # exception
```
2 changes: 1 addition & 1 deletion log/__init__.py
Expand Up @@ -13,4 +13,4 @@
exc = exception

__project__ = 'minilog'
__version__ = '1.1a3'
__version__ = '1.1b1'
15 changes: 13 additions & 2 deletions log/helpers.py
Expand Up @@ -8,15 +8,26 @@

DEFAULT_LEVEL = logging.INFO
DEFAULT_FORMAT = "%(levelname)s: %(name)s: %(message)s"
VERBOSITY_TO_LEVEL = {
0: logging.ERROR,
1: logging.WARNING,
2: logging.INFO,
3: logging.DEBUG,
}


def init(*, reset=False, debug=False, **kwargs):
def init(*, reset=False, debug=False, verbosity=None, **kwargs):
if reset:
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)

custom_format = kwargs.get('format')
default_level = logging.DEBUG if debug else DEFAULT_LEVEL
if debug:
default_level = logging.DEBUG
elif verbosity is not None:
default_level = VERBOSITY_TO_LEVEL[verbosity]
else:
default_level = DEFAULT_LEVEL

kwargs['level'] = kwargs.get('level', default_level)
kwargs['format'] = kwargs.get('format', DEFAULT_FORMAT)
Expand Down
33 changes: 33 additions & 0 deletions log/tests/test_helpers.py
@@ -0,0 +1,33 @@
# pylint: disable=unused-variable,expression-not-assigned

from unittest.mock import patch, call

from log import helpers


def describe_init():

@patch('logging.basicConfig')
def with_verbosity_0(config, expect):
helpers.init(format='%(message)s', verbosity=0)
expect(config.mock_calls) == [call(format='%(message)s', level=40)]

@patch('logging.basicConfig')
def with_verbosity_1(config, expect):
helpers.init(format='%(message)s', verbosity=1)
expect(config.mock_calls) == [call(format='%(message)s', level=30)]

@patch('logging.basicConfig')
def with_verbosity_2(config, expect):
helpers.init(format='%(message)s', verbosity=2)
expect(config.mock_calls) == [call(format='%(message)s', level=20)]

@patch('logging.basicConfig')
def with_verbosity_3(config, expect):
helpers.init(format='%(message)s', verbosity=3)
expect(config.mock_calls) == [call(format='%(message)s', level=10)]

@patch('logging.basicConfig')
def with_verbosity_0_and_debug(config, expect):
helpers.init(format='%(message)s', verbosity=0, debug=True)
expect(config.mock_calls) == [call(format='%(message)s', level=10)]
3 changes: 2 additions & 1 deletion mkdocs.yml
Expand Up @@ -7,7 +7,8 @@ theme: readthedocs

pages:
- Home: index.md
- API: api.md
- Logging: logging.md
- Extras: extras.md
- About:
- Release Notes: about/changelog.md
- Contributing: about/contributing.md
Expand Down

0 comments on commit 106f039

Please sign in to comment.