-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from jacebrowning/release/v0.2
Release v0.2
- Loading branch information
Showing
17 changed files
with
237 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.