Skip to content

Commit a3e71e0

Browse files
authored
Merge pull request #7 from Colin-b/develop
Release 0.2.0
2 parents 4ceb548 + 07fcff2 commit a3e71e0

10 files changed

Lines changed: 113 additions & 40 deletions

File tree

.github/workflows/release.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
build:
10+
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
python-version: ['3.6', '3.7', '3.8', '3.9', '3.10']
15+
16+
steps:
17+
- uses: actions/checkout@v2
18+
- name: Set up Python ${{ matrix.python-version }}
19+
uses: actions/setup-python@v2
20+
with:
21+
python-version: ${{ matrix.python-version }}
22+
- name: Install dependencies
23+
run: |
24+
python -m pip install --upgrade pip
25+
python -m pip install .[testing]
26+
- name: Test with pytest
27+
run: |
28+
pytest --cov=logging_json --cov-fail-under=100 --cov-report=term-missing
29+
- name: Create packages
30+
run: |
31+
python -m pip install wheel
32+
python setup.py sdist bdist_wheel
33+
- name: Publish packages
34+
run: |
35+
python -m pip install twine
36+
python -m twine upload dist/* --skip-existing --username __token__ --password ${{ secrets.pypi_password }}

.github/workflows/test.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Test
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
8+
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
python-version: ['3.6', '3.7', '3.8', '3.9', '3.10']
12+
13+
steps:
14+
- uses: actions/checkout@v2
15+
- name: Set up Python ${{ matrix.python-version }}
16+
uses: actions/setup-python@v2
17+
with:
18+
python-version: ${{ matrix.python-version }}
19+
- name: Install dependencies
20+
run: |
21+
python -m pip install --upgrade pip
22+
python -m pip install .[testing]
23+
- name: Test with pytest
24+
run: |
25+
pytest --cov=logging_json --cov-fail-under=100 --cov-report=term-missing

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
repos:
22
- repo: https://github.com/psf/black
3-
rev: 21.9b0
3+
rev: 21.11b1
44
hooks:
55
- id: black

.travis.yml

Lines changed: 0 additions & 17 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [0.2.0] - 2021-11-24
10+
### Added
11+
- Added `message_field_name` parameter.
12+
913
## [0.1.0] - 2021-10-04
1014
### Fixed
1115
- Handle `extra` logging parameter.
@@ -14,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1418
### Added
1519
- Public release.
1620

17-
[Unreleased]: https://github.com/Colin-b/healthpy/compare/v0.1.0...HEAD
21+
[Unreleased]: https://github.com/Colin-b/healthpy/compare/v0.2.0...HEAD
22+
[0.2.0]: https://github.com/Colin-b/healthpy/compare/v0.1.0...v0.2.0
1823
[0.1.0]: https://github.com/Colin-b/healthpy/compare/v0.0.1...v0.1.0
1924
[0.0.1]: https://github.com/Colin-b/healthpy/releases/tag/v0.0.1

README.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
<p align="center">
44
<a href="https://pypi.org/project/logging_json/"><img alt="pypi version" src="https://img.shields.io/pypi/v/logging_json"></a>
5-
<a href="https://travis-ci.com/Colin-b/logging_json"><img alt="Build status" src="https://api.travis-ci.com/Colin-b/logging_json.svg?branch=master"></a>
6-
<a href="https://travis-ci.com/Colin-b/logging_json"><img alt="Coverage" src="https://img.shields.io/badge/coverage-100%25-brightgreen"></a>
5+
<a href="https://github.com/Colin-b/logging_json/actions"><img alt="Build status" src="https://github.com/Colin-b/logging_json/workflows/Release/badge.svg"></a>
6+
<a href="https://github.com/Colin-b/logging_json/actions"><img alt="Coverage" src="https://img.shields.io/badge/coverage-100%25-brightgreen"></a>
77
<a href="https://github.com/psf/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
8-
<a href="https://travis-ci.com/Colin-b/logging_json"><img alt="Number of tests" src="https://img.shields.io/badge/tests-15 passed-blue"></a>
8+
<a href="https://github.com/Colin-b/logging_json/actions"><img alt="Number of tests" src="https://img.shields.io/badge/tests-17 passed-blue"></a>
99
<a href="https://pypi.org/project/logging_json/"><img alt="Number of downloads" src="https://img.shields.io/pypi/dm/logging_json"></a>
1010
</p>
1111

@@ -33,7 +33,7 @@ It must be a dictionary where keys are the keys to be appended to the resulting
3333

3434
#### Logging exceptions, a specific case
3535

36-
If an exception is loggued, the `exception` key will be appended to the resulting JSON dictionary.
36+
If an exception is logged, the `exception` key will be appended to the resulting JSON dictionary.
3737

3838
This dictionary will contains 3 keys:
3939
* `type`: The name of the exception class (useful when the message is blank).
@@ -56,6 +56,7 @@ The resulting JSON dictionary will be the one you provided (with the [additional
5656

5757
Anything not logged using a dictionary will be handled by the standard formatter and it can result in one of the 2 output:
5858
* A JSON dictionary, if [additional fields](#adding-additional-fields-and-values) are set or if `extra` parameter is used while logging, with the message available in the `msg` key of the resulting JSON dictionary.
59+
Default `msg` key name can be changed by `message_field_name` parameter of the `logging_json.JSONFormatter` instance.
5960
* The formatted record, if no [additional fields](#adding-additional-fields-and-values) are set.
6061

6162
This handles the usual string logging as in the following:
@@ -88,24 +89,25 @@ You can configure your logging as advertise by python, by using the `logging.con
8889

8990
```python
9091
import logging.config
91-
import logging_json
92-
import sys
93-
94-
formatter = logging_json.JSONFormatter(fields={
95-
"level_name": "levelname",
96-
"thread_name": "threadName",
97-
"process_name": "processName"
98-
})
99-
handler = logging.StreamHandler(stream=sys.stdout)
100-
handler.setFormatter(formatter)
10192

10293
logging.config.dictConfig({
10394
"version": 1,
10495
"formatters": {
105-
"json": formatter
96+
"json": {
97+
'()': 'logging_json.JSONFormatter',
98+
'fields':{
99+
"level_name": "levelname",
100+
"thread_name": "threadName",
101+
"process_name": "processName"
102+
}
103+
}
106104
},
107105
"handlers": {
108-
"standard_output": handler,
106+
"standard_output": {
107+
'class': 'logging.StreamHandler',
108+
'formatter': 'json',
109+
'stream': 'ext://sys.stdout'
110+
},
109111
},
110112
"loggers": {
111113
"my_app": {"level": "DEBUG"}

logging_json/_formatter.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,18 @@ def _value(record: logging.LogRecord, field_name_or_value: Any) -> Any:
4949

5050

5151
class JSONFormatter(logging.Formatter):
52-
def __init__(self, *args, fields: Dict[str, Any] = None, **kwargs):
52+
def __init__(
53+
self,
54+
*args,
55+
fields: Dict[str, Any] = None,
56+
message_field_name: str = "msg",
57+
**kwargs,
58+
):
5359
# Allow to provide any formatter setting (useful to provide a custom date format)
5460
super().__init__(*args, **kwargs)
5561
self.fields = fields or {}
5662
self.usesTime = lambda: "asctime" in self.fields.values()
63+
self.message_field_name = message_field_name
5764

5865
def format(self, record: logging.LogRecord):
5966
# Let python set every additional record field
@@ -66,7 +73,7 @@ def format(self, record: logging.LogRecord):
6673
if isinstance(record.msg, collections.abc.Mapping):
6774
message.update(record.msg)
6875
else:
69-
message["msg"] = super().formatMessage(record)
76+
message[self.message_field_name] = super().formatMessage(record)
7077

7178
message.update(_extra_attributes(record))
7279

@@ -79,7 +86,7 @@ def format(self, record: logging.LogRecord):
7986

8087
return (
8188
super().formatMessage(record)
82-
if (len(message) == 1 and "msg" in message)
89+
if (len(message) == 1 and self.message_field_name in message)
8390
else json.dumps(message)
8491
)
8592

logging_json/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
# Major should be incremented in case there is a breaking change. (eg: 2.5.8 -> 3.0.0)
44
# Minor should be incremented in case there is an enhancement. (eg: 2.5.8 -> 2.6.0)
55
# Patch should be incremented in case there is a bug fix. (eg: 2.5.8 -> 2.5.9)
6-
__version__ = "0.1.0"
6+
__version__ = "0.2.0"

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"Programming Language :: Python :: 3.7",
3030
"Programming Language :: Python :: 3.8",
3131
"Programming Language :: Python :: 3.9",
32+
"Programming Language :: Python :: 3.10",
3233
"Topic :: Software Development :: Build Tools",
3334
],
3435
keywords=["logging", "json"],
@@ -37,7 +38,7 @@
3738
extras_require={
3839
"testing": [
3940
# Used to check coverage
40-
"pytest-cov==2.*",
41+
"pytest-cov==3.*",
4142
]
4243
},
4344
python_requires=">=3.6",

tests/test_formatter.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,26 @@ def test_str_with_args_message(caplog):
3333
assert fmt(caplog) == "message 1"
3434

3535

36+
def test_str_with_message_field_name(caplog):
37+
caplog.set_level("INFO")
38+
logging.info("message 1")
39+
assert fmt(caplog, message_field_name="message") == "message 1"
40+
41+
3642
def test_str_with_extra_message(caplog):
3743
caplog.set_level("INFO")
3844
logging.info("message 1", extra={"key1": "value 1"})
3945
assert dict_fmt(caplog) == {"msg": "message 1", "key1": "value 1"}
4046

4147

48+
def test_str_with_message_field_name_and_fields(caplog):
49+
caplog.set_level("INFO")
50+
logging.info("message 1")
51+
assert dict_fmt(
52+
caplog, message_field_name="message", fields={"level": "levelname"}
53+
) == {"message": "message 1", "level": "INFO"}
54+
55+
4256
def test_str_with_args_and_extra_message(caplog):
4357
caplog.set_level("INFO")
4458
logging.info("message %s", "1", extra={"key1": "value 1"})

0 commit comments

Comments
 (0)