Skip to content

Commit

Permalink
0.2.0 (#4)
Browse files Browse the repository at this point in the history
* readme stuff

* CI

* fix CI + 0.1.1

* remove comments

* added set_entries, badges - 0.2.0
  • Loading branch information
aviramha authored Aug 12, 2020
1 parent 8f05bf3 commit ed4c1fc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# contextfilter

![Version](https://img.shields.io/pypi/v/contextfilter)
![License](https://img.shields.io/pypi/l/contextfilter)
![Tests](https://github.com/aviramha/contextfilter/workflows/Test%20Contextfilter/badge.svg?branch=develop)
Small, helper library for logging contextual information in Python 3.7.

## Installation
Expand All @@ -11,11 +13,13 @@ $ pip install contextfilter
## Usage
```py
import logging
from contextfilter import ContextFilter, set_entry
from contextfilter import ContextFilter, set_entry, set_entries

logger = logging.getLogger("test")
logger.addFilter(ContextFilter())
set_entry("request_id", 3)
# or
set_entries(request_id=3)
logger.info("test")
# Log record will contain the attribute request_id with value 3
```
Expand Down
4 changes: 2 additions & 2 deletions contextfilter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .main import ContextFilter, reset, set_entry
from .main import ContextFilter, reset, set_entries, set_entry

__all__ = ("ContextFilter", "set_entry", "reset")
__all__ = ("ContextFilter", "set_entry", "reset", "set_entries")
12 changes: 12 additions & 0 deletions contextfilter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ def set_entry(name: Hashable, value: Any) -> None:
extra_dict[name] = value


def set_entries(**entries: Any) -> None:
"""
Sets multiple entries at once.
Args:
**entries - key, value
Raises:
ValueError - if any of the entries is in PROTECTED_KEYS.
"""
for key, value in entries.items():
set_entry(key, value)


def reset() -> Dict[Any, Any]:
"""
Resets the ContextFilter's context dictioniary.
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
[tool.poetry]
name = "contextfilter"
version = "0.1.1"
version = "0.2.0"
description = "ContextVars Filter for easily enriching log records"
authors = ["Aviram Hassan <[email protected]>"]
license = "MIT"
readme = "README.md"
repository = "https://github.com/aviramha/contextfilter"


[tool.poetry.dependencies]
python = "^3.7"
Expand Down
4 changes: 2 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import unittest

from contextfilter import ContextFilter, reset, set_entry
from contextfilter import ContextFilter, reset, set_entries, set_entry


class TestSanity(unittest.TestCase):
Expand All @@ -10,7 +10,7 @@ def test_sanity(self):
logger.addFilter(ContextFilter())
with self.assertLogs(logger, level="INFO") as output:
logger.info("test1")
set_entry("test_attr", "test_data")
set_entries(test_attr="test_data")
logger.info("test2")
reset()
logger.info("test3")
Expand Down

0 comments on commit ed4c1fc

Please sign in to comment.