diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 19d0369..a922d09 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,7 +15,7 @@ repos: rev: v0.910 hooks: - id: mypy - args: [--strict] + args: [--strict, --show-error-codes, --no-warn-unused-ignores] - repo: https://github.com/ambv/black rev: 21.6b0 hooks: diff --git a/ecs_logging/_stdlib.py b/ecs_logging/_stdlib.py index a89abee..9620ea6 100644 --- a/ecs_logging/_stdlib.py +++ b/ecs_logging/_stdlib.py @@ -71,8 +71,16 @@ class StdlibFormatter(logging.Formatter): } | _LOGRECORD_DIR converter = time.gmtime - def __init__(self, stack_trace_limit=None, exclude_fields=()): - # type: (Any, Optional[int], Sequence[str]) -> None + def __init__( + self, + fmt=None, + datefmt=None, + style="%", + validate=None, + stack_trace_limit=None, + exclude_fields=(), + ): + # type: (Any, Optional[str], Optional[str], str, Optional[bool], Optional[int], Sequence[str]) -> None """Initialize the ECS formatter. :param int stack_trace_limit: @@ -91,7 +99,18 @@ def __init__(self, stack_trace_limit=None, exclude_fields=()): exclude_keys=["error"] """ - super(StdlibFormatter, self).__init__() + _kwargs = {} + if validate is not None: + # validate was introduced in py3.8 so we need to only provide it if the user provided it + _kwargs["validate"] = validate + if sys.version_info < (3, 0): # Different args in py2.7 + super(StdlibFormatter, self).__init__( # type: ignore[call-arg] + fmt=fmt, datefmt=datefmt + ) + else: + super(StdlibFormatter, self).__init__( # type: ignore[call-arg] + fmt=fmt, datefmt=datefmt, style=style, **_kwargs + ) if stack_trace_limit is not None: if not isinstance(stack_trace_limit, int): diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 0000000..5066fd3 --- /dev/null +++ b/mypy.ini @@ -0,0 +1,8 @@ +[mypy] +exclude = "/tests/" + +[mypy-tests.*] +ignore_errors = true + +[mypy-noxfile] +ignore_errors = true \ No newline at end of file diff --git a/noxfile.py b/noxfile.py index 3e19c42..a57e026 100644 --- a/noxfile.py +++ b/noxfile.py @@ -50,4 +50,10 @@ def lint(session): session.install("flake8", "black", "mypy") session.run("black", "--check", "--target-version=py27", *SOURCE_FILES) session.run("flake8", "--ignore=E501,W503", *SOURCE_FILES) - session.run("mypy", "--strict", "ecs_logging/") + session.run( + "mypy", + "--strict", + "--show-error-codes", + "--no-warn-unused-ignores", + "ecs_logging/", + ) diff --git a/tests/test_stdlib_formatter.py b/tests/test_stdlib_formatter.py index f0a3d32..af320ea 100644 --- a/tests/test_stdlib_formatter.py +++ b/tests/test_stdlib_formatter.py @@ -16,6 +16,7 @@ # under the License. import logging +import logging.config import mock import pytest import json @@ -327,3 +328,12 @@ def test_stack_info_excluded(logger, exclude_fields): ecs = json.loads(stream.getvalue().rstrip()) assert "error" not in ecs + + +def test_stdlibformatter_signature(): + logging.config.dictConfig( + { + "version": 1, + "formatters": {"my_formatter": {"class": "ecs_logging.StdlibFormatter"}}, + } + )