Skip to content

Commit

Permalink
Add API to silence logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Mar 4, 2018
1 parent d10bb3b commit 6a4e457
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Unix: [![Unix Build Status](https://img.shields.io/travis/jacebrowning/minilog/d

# Overview

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

```python
import logging
Expand Down
2 changes: 1 addition & 1 deletion log/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .logger import * # pylint: disable=wildcard-import
from .helpers import init
from .helpers import init, silence

d = debug
i = info
Expand Down
14 changes: 14 additions & 0 deletions log/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,17 @@ def init(**kwargs):

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)
File renamed without changes.
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)
27 changes: 0 additions & 27 deletions tests/test_log.py

This file was deleted.

44 changes: 44 additions & 0 deletions tests/test_records.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# pylint: disable=redefined-outer-name,unused-variable,expression-not-assigned,singleton-comparison

import pytest

import log

from . import demo, other


def describe_text():

def it_includes_the_caller_location(expect, caplog):
demo.greet("caller")
expect(caplog.text) == \
"demo.py 5 ERROR Hello, caller!\n"

@pytest.mark.last
def it_can_be_formatted_with_init(expect, caplog):
log.init(format=log.helpers.DEFAULT_FORMAT)
demo.greet("format")
expect(caplog.text) == "ERROR: tests.demo: Hello, format!\n"


def describe_silence():

def when_off(expect, caplog):
log.silence('3rd-party')
other.do_3rd_party_thing()
expect(caplog.records) == []

def with_errors(expect, caplog):
log.silence('3rd-party', allow_error=True)
other.do_3rd_party_thing()
expect(len(caplog.records)) == 1

def with_warnings(expect, caplog):
log.silence('3rd-party', allow_warning=True)
other.do_3rd_party_thing()
expect(len(caplog.records)) == 2

def with_infos(expect, caplog):
log.silence('3rd-party', allow_info=True)
other.do_3rd_party_thing()
expect(len(caplog.records)) == 3

0 comments on commit 6a4e457

Please sign in to comment.