Skip to content

Commit dfcc0d8

Browse files
authored
Match StdlibFormatter signature with logger.Formatter (#54)
* Match StdlibFormatter signature with that of logger.Formatter * Add a test Also fix mypy settings for tests and noxfile * Fix mypy for validate arg (only py3.8+) * Add mypy ignore * Fix logging.Formatter for py2.7
1 parent 450a106 commit dfcc0d8

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ repos:
1515
rev: v0.910
1616
hooks:
1717
- id: mypy
18-
args: [--strict]
18+
args: [--strict, --show-error-codes, --no-warn-unused-ignores]
1919
- repo: https://github.com/ambv/black
2020
rev: 21.6b0
2121
hooks:

ecs_logging/_stdlib.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,16 @@ class StdlibFormatter(logging.Formatter):
7171
} | _LOGRECORD_DIR
7272
converter = time.gmtime
7373

74-
def __init__(self, stack_trace_limit=None, exclude_fields=()):
75-
# type: (Any, Optional[int], Sequence[str]) -> None
74+
def __init__(
75+
self,
76+
fmt=None,
77+
datefmt=None,
78+
style="%",
79+
validate=None,
80+
stack_trace_limit=None,
81+
exclude_fields=(),
82+
):
83+
# type: (Any, Optional[str], Optional[str], str, Optional[bool], Optional[int], Sequence[str]) -> None
7684
"""Initialize the ECS formatter.
7785
7886
:param int stack_trace_limit:
@@ -91,7 +99,18 @@ def __init__(self, stack_trace_limit=None, exclude_fields=()):
9199
92100
exclude_keys=["error"]
93101
"""
94-
super(StdlibFormatter, self).__init__()
102+
_kwargs = {}
103+
if validate is not None:
104+
# validate was introduced in py3.8 so we need to only provide it if the user provided it
105+
_kwargs["validate"] = validate
106+
if sys.version_info < (3, 0): # Different args in py2.7
107+
super(StdlibFormatter, self).__init__( # type: ignore[call-arg]
108+
fmt=fmt, datefmt=datefmt
109+
)
110+
else:
111+
super(StdlibFormatter, self).__init__( # type: ignore[call-arg]
112+
fmt=fmt, datefmt=datefmt, style=style, **_kwargs
113+
)
95114

96115
if stack_trace_limit is not None:
97116
if not isinstance(stack_trace_limit, int):

mypy.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[mypy]
2+
exclude = "/tests/"
3+
4+
[mypy-tests.*]
5+
ignore_errors = true
6+
7+
[mypy-noxfile]
8+
ignore_errors = true

noxfile.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,10 @@ def lint(session):
5050
session.install("flake8", "black", "mypy")
5151
session.run("black", "--check", "--target-version=py27", *SOURCE_FILES)
5252
session.run("flake8", "--ignore=E501,W503", *SOURCE_FILES)
53-
session.run("mypy", "--strict", "ecs_logging/")
53+
session.run(
54+
"mypy",
55+
"--strict",
56+
"--show-error-codes",
57+
"--no-warn-unused-ignores",
58+
"ecs_logging/",
59+
)

tests/test_stdlib_formatter.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# under the License.
1717

1818
import logging
19+
import logging.config
1920
import mock
2021
import pytest
2122
import json
@@ -327,3 +328,12 @@ def test_stack_info_excluded(logger, exclude_fields):
327328

328329
ecs = json.loads(stream.getvalue().rstrip())
329330
assert "error" not in ecs
331+
332+
333+
def test_stdlibformatter_signature():
334+
logging.config.dictConfig(
335+
{
336+
"version": 1,
337+
"formatters": {"my_formatter": {"class": "ecs_logging.StdlibFormatter"}},
338+
}
339+
)

0 commit comments

Comments
 (0)