diff --git a/README.md b/README.md index f8ce099..6b74c8a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/docs/api.md b/docs/api.md index 34bd4cb..aadb92b 100644 --- a/docs/api.md +++ b/docs/api.md @@ -31,8 +31,15 @@ log.log(level, message, *args) # Configuration -Optionally, set the format for all logging handlers: +Optionally 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) +``` diff --git a/log/__init__.py b/log/__init__.py index e0d4e9c..a102f9b 100644 --- a/log/__init__.py +++ b/log/__init__.py @@ -1,5 +1,5 @@ from .logger import * # pylint: disable=wildcard-import -from .helpers import init +from .helpers import init, silence d = debug i = info diff --git a/log/helpers.py b/log/helpers.py index d5873b1..d9a33e3 100644 --- a/log/helpers.py +++ b/log/helpers.py @@ -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) diff --git a/log/tests/test_package.py b/log/tests/test_api.py similarity index 100% rename from log/tests/test_package.py rename to log/tests/test_api.py diff --git a/tests/other.py b/tests/other.py new file mode 100644 index 0000000..22911e3 --- /dev/null +++ b/tests/other.py @@ -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) diff --git a/tests/test_log.py b/tests/test_log.py deleted file mode 100644 index 0e24cd0..0000000 --- a/tests/test_log.py +++ /dev/null @@ -1,27 +0,0 @@ -# pylint: disable=redefined-outer-name,unused-variable,expression-not-assigned,singleton-comparison - -import pytest - -import log - -from . import demo - - -def describe_api(): - - def it_has_custom_warn_function(expect): - expect(log.warn) == log.warning - - -def describe_output(): - - 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" diff --git a/tests/test_records.py b/tests/test_records.py new file mode 100644 index 0000000..12e6b9f --- /dev/null +++ b/tests/test_records.py @@ -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