From 6936d95bbe2c57aa0a1de1fb41cf6d76a5c6e43e Mon Sep 17 00:00:00 2001 From: Colton Myers Date: Tue, 10 Aug 2021 10:45:34 -0600 Subject: [PATCH 1/5] Match StdlibFormatter signature with that of logger.Formatter --- ecs_logging/_stdlib.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/ecs_logging/_stdlib.py b/ecs_logging/_stdlib.py index a89abee..22141a3 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=True, + stack_trace_limit=None, + exclude_fields=(), + ): + # type: (Any, Optional[str], Optional[str], str, bool, Optional[int], Sequence[str]) -> None """Initialize the ECS formatter. :param int stack_trace_limit: @@ -91,7 +99,9 @@ def __init__(self, stack_trace_limit=None, exclude_fields=()): exclude_keys=["error"] """ - super(StdlibFormatter, self).__init__() + super(StdlibFormatter, self).__init__( + fmt=fmt, datefmt=datefmt, style=style, validate=validate + ) if stack_trace_limit is not None: if not isinstance(stack_trace_limit, int): From 0c543f11f5004d3d945f982701808ecf6fe7bd7d Mon Sep 17 00:00:00 2001 From: Colton Myers Date: Wed, 11 Aug 2021 11:14:08 -0600 Subject: [PATCH 2/5] Add a test Also fix mypy settings for tests and noxfile --- mypy.ini | 8 ++++++++ tests/test_stdlib_formatter.py | 10 ++++++++++ 2 files changed, 18 insertions(+) create mode 100644 mypy.ini 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/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"}}, + } + ) From 2ab9764167eca2a48ede9f82ec3f493060be2f25 Mon Sep 17 00:00:00 2001 From: Colton Myers Date: Thu, 12 Aug 2021 11:32:12 -0600 Subject: [PATCH 3/5] Fix mypy for validate arg (only py3.8+) --- .pre-commit-config.yaml | 2 +- ecs_logging/_stdlib.py | 10 +++++++--- noxfile.py | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 19d0369..6ac7765 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] - repo: https://github.com/ambv/black rev: 21.6b0 hooks: diff --git a/ecs_logging/_stdlib.py b/ecs_logging/_stdlib.py index 22141a3..ed230b5 100644 --- a/ecs_logging/_stdlib.py +++ b/ecs_logging/_stdlib.py @@ -76,11 +76,11 @@ def __init__( fmt=None, datefmt=None, style="%", - validate=True, + validate=None, stack_trace_limit=None, exclude_fields=(), ): - # type: (Any, Optional[str], Optional[str], str, bool, Optional[int], Sequence[str]) -> None + # type: (Any, Optional[str], Optional[str], str, Optional[bool], Optional[int], Sequence[str]) -> None """Initialize the ECS formatter. :param int stack_trace_limit: @@ -99,8 +99,12 @@ def __init__( exclude_keys=["error"] """ + _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 super(StdlibFormatter, self).__init__( - fmt=fmt, datefmt=datefmt, style=style, validate=validate + fmt=fmt, datefmt=datefmt, style=style, **_kwargs ) if stack_trace_limit is not None: diff --git a/noxfile.py b/noxfile.py index 3e19c42..0d060d6 100644 --- a/noxfile.py +++ b/noxfile.py @@ -50,4 +50,4 @@ 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", "ecs_logging/") From 14a1636fc6417adafc819495e4800fe5ae894f32 Mon Sep 17 00:00:00 2001 From: Colton Myers Date: Thu, 12 Aug 2021 12:02:40 -0600 Subject: [PATCH 4/5] Add mypy ignore --- .pre-commit-config.yaml | 2 +- ecs_logging/_stdlib.py | 2 +- noxfile.py | 8 +++++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6ac7765..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, --show-error-codes] + 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 ed230b5..81a0fd8 100644 --- a/ecs_logging/_stdlib.py +++ b/ecs_logging/_stdlib.py @@ -103,7 +103,7 @@ def __init__( 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 - super(StdlibFormatter, self).__init__( + super(StdlibFormatter, self).__init__( # type: ignore[call-arg] fmt=fmt, datefmt=datefmt, style=style, **_kwargs ) diff --git a/noxfile.py b/noxfile.py index 0d060d6..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", "--show-error-codes", "ecs_logging/") + session.run( + "mypy", + "--strict", + "--show-error-codes", + "--no-warn-unused-ignores", + "ecs_logging/", + ) From f23783a605dbef77ef999f874d9b5e9356cf66b0 Mon Sep 17 00:00:00 2001 From: Colton Myers Date: Fri, 13 Aug 2021 10:47:46 -0600 Subject: [PATCH 5/5] Fix logging.Formatter for py2.7 --- ecs_logging/_stdlib.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ecs_logging/_stdlib.py b/ecs_logging/_stdlib.py index 81a0fd8..9620ea6 100644 --- a/ecs_logging/_stdlib.py +++ b/ecs_logging/_stdlib.py @@ -103,9 +103,14 @@ def __init__( 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 - super(StdlibFormatter, self).__init__( # type: ignore[call-arg] - fmt=fmt, datefmt=datefmt, style=style, **_kwargs - ) + 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):