Skip to content

Commit

Permalink
Merge pull request #9 from jacebrowning/release/v0.2
Browse files Browse the repository at this point in the history
Release v0.2
  • Loading branch information
jacebrowning authored Mar 4, 2018
2 parents c543d3e + fdafe7c commit 2c168a8
Show file tree
Hide file tree
Showing 17 changed files with 237 additions and 131 deletions.
3 changes: 2 additions & 1 deletion .pylint.ini
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ disable=print-statement,
too-few-public-methods,
fixme,
too-many-arguments,
too-many-branches
too-many-branches,
global-statement,

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Revision History

## 0.2 (2018/03/03)

- Added method to force logging format: `log.init(format="...")`
- Added method to silenced named loggers: `log.silence('requests', allow_error=True)`
- Added convenience aliases: `log.d`, `log.i`, `log.w`, `log.e`

## 0.1 (2018/03/03)

- Initial release.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ test: test-all ## Run unit and integration tests

.PHONY: test-unit
test-unit: install
@- mv $(FAILURES) $(FAILURES).bak
@ ( mv $(FAILURES) $(FAILURES).bak || true ) > /dev/null 2>&1
$(PYTEST) $(PYTEST_OPTIONS) $(PACKAGE) --junitxml=$(REPORTS)/unit.xml
@- mv $(FAILURES).bak $(FAILURES)
@ ( mv $(FAILURES).bak $(FAILURES) || true ) > /dev/null 2>&1
$(COVERAGE_SPACE) $(REPOSITORY) unit

.PHONY: test-int
Expand Down
23 changes: 9 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ Unix: [![Unix Build Status](https://img.shields.io/travis/jacebrowning/minilog/d

# Overview

Instead of including this boilerplate in every module:
Every project should utilize logging, but sometimes the required boilerplate is too much. Instead of including this:

```python
import logging
import logging

logging.basicConfig(
level=logging.INFO,
format="%(levelname)s: %(name)s: %(message)s",
)

log = logging.getLogger(__name__)

def foobar(name):
def greet(name):
log.info("Hello, %s!", name)
```

Expand All @@ -18,7 +23,7 @@ with this package you can simply:
```python
import log

def foobar(name):
def greet(name):
log.info("Hello, %s!", name)
```

Expand All @@ -29,13 +34,3 @@ It will produce the exact same standard library `logging` records behind the sce
```sh
$ pip install minilog
```

# Setup

Optionally, change for format for all logging handlers:

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


26 changes: 24 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
This package is a drop-in replacement for `logging.Logger` objects.
# Logging

The package intends to be a drop-in replacement for `logging.Logger` objects.

It supports standard the logging API:

Expand All @@ -13,11 +15,31 @@ log.critical(message, *args)
As well as convenience methods:

```python
log.warn(message, *args) # an alias for `warning(...)`
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
```

And programmatic logging:

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

# Configuration

Set the format for all logging handlers:

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

Set the logging level for specific named loggers:

```python
log.silence('selenium')
log.silence('werkzeug', 'requests', allow_warning=True)
```
10 changes: 8 additions & 2 deletions log/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
from .api import * # pylint: disable=wildcard-import
from .logger import * # pylint: disable=wildcard-import
from .helpers import init, silence

d = debug
i = info
w = warn = warning
e = error

__project__ = 'minilog'
__version__ = '0.1'
__version__ = '0.2'
73 changes: 0 additions & 73 deletions log/api.py

This file was deleted.

37 changes: 37 additions & 0 deletions log/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Wrappers to eliminate boilerplate `logging` activities."""

import logging


DEFAULT_LEVEL = logging.INFO
DEFAULT_FORMAT = "%(levelname)s: %(name)s: %(message)s"

initialized = False


def init(**kwargs):
custom_format = kwargs.get('format')
kwargs['level'] = kwargs.get('level', DEFAULT_LEVEL)
kwargs['format'] = kwargs.get('format', DEFAULT_FORMAT)
logging.basicConfig(**kwargs)
if custom_format:
formatter = logging.Formatter(custom_format)
for handler in logging.root.handlers:
handler.setFormatter(formatter)

global initialized
initialized = True


def silence(*names, allow_info=False, allow_warning=False, allow_error=False):
if allow_info:
level = logging.INFO
elif allow_warning:
level = logging.WARNING
elif allow_error:
level = logging.ERROR
else:
level = logging.CRITICAL

for name in names:
logging.getLogger(name).setLevel(level)
33 changes: 33 additions & 0 deletions log/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Replicates some of the `logging.Logger` API."""

import logging

from . import utils


def log(level, message, *args, **kwargs):
utils.create_logger_record(level, message, *args, **kwargs)


def debug(message, *args, **kwargs):
log(logging.DEBUG, message, *args, **kwargs)


def info(message, *args, **kwargs):
log(logging.INFO, message, *args, **kwargs)


def warning(message, *args, **kwargs):
log(logging.WARNING, message, *args, **kwargs)


def error(message, *args, **kwargs):
log(logging.ERROR, message, *args, **kwargs)


def critical(message, *args, **kwargs):
log(logging.CRITICAL, message, *args, **kwargs)


def exception(*args, **kwargs): # pylint: disable=unused-argument
raise NotImplementedError
26 changes: 18 additions & 8 deletions log/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
# pylint: disable=redefined-outer-name,unused-variable,expression-not-assigned,singleton-comparison

import logging
import pytest

from log import api
import log


def describe_log():

def it_sets_level_and_message(expect, caplog):
api.log(logging.DEBUG, "foobar")
expect(caplog.records[-1].levelname) == 'DEBUG'
expect(caplog.records[-1].message) == "foobar"
@pytest.mark.parametrize("name, levelname", [
('critical', 'CRITICAL'),
('d', 'DEBUG'),
('debug', 'DEBUG'),
('e', 'ERROR'),
('error', 'ERROR'),
# ('exception', 'ERROR'),
('i', 'INFO'),
('info', 'INFO'),
('w', 'WARNING'),
('warn', 'WARNING'),
('warning', 'WARNING'),
])
def test_level_mapping(expect, caplog, name, levelname):
getattr(log, name)("message")
expect(caplog.records[-1].levelname) == levelname
13 changes: 13 additions & 0 deletions log/tests/test_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# pylint: disable=redefined-outer-name,unused-variable,expression-not-assigned,singleton-comparison

import logging

from log import logger


def describe_log():

def it_sets_level_and_message(expect, caplog):
logger.log(logging.DEBUG, "foobar")
expect(caplog.records[-1].levelname) == 'DEBUG'
expect(caplog.records[-1].message) == "foobar"
28 changes: 28 additions & 0 deletions log/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Implements the "magic" to create `logging` records for the caller."""

import logging
import inspect

from . import helpers


def create_logger_record(level, message, *args, **kwargs):
if not helpers.initialized:
helpers.init()

frame, filename, lineno, *_ = inspect.stack()[3]
module = inspect.getmodule(frame)

logger = logging.getLogger()
record = logger.makeRecord(
module.__name__,
level,
fn=filename,
lno=lineno,
msg=message,
args=args,
exc_info=None,
extra=kwargs,
sinfo=None,
)
logger.handle(record)
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
site_name: log
site_name: minilog
site_description: Minimalistic wrapper for Python logging.
site_author: Jace Browning
repo_url: https://github.com/jacebrowning/minilog
Expand Down
2 changes: 1 addition & 1 deletion tests/demo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import log


def foobar(name="world"):
def greet(name):
log.error("Hello, %s!", name)
11 changes: 11 additions & 0 deletions tests/other.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import logging


log = logging.getLogger('3rd-party')


def do_3rd_party_thing():
log.debug(1)
log.info(2)
log.warning(3)
log.error(4)
Loading

0 comments on commit 2c168a8

Please sign in to comment.