Skip to content

Match StdlibFormatter signature with logger.Formatter #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
25 changes: 22 additions & 3 deletions ecs_logging/_stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[mypy]
exclude = "/tests/"

[mypy-tests.*]
ignore_errors = true

[mypy-noxfile]
ignore_errors = true
8 changes: 7 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
)
10 changes: 10 additions & 0 deletions tests/test_stdlib_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# under the License.

import logging
import logging.config
import mock
import pytest
import json
Expand Down Expand Up @@ -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"}},
}
)