From 4131a3c38d23f81e163d23ba5e31f92cb69fa4bd Mon Sep 17 00:00:00 2001 From: Andrii Lahuta <13280256+andriilahuta@users.noreply.github.com> Date: Mon, 22 Jan 2024 23:40:51 +0200 Subject: [PATCH] Lint --- logstash_async/constants.py | 3 ++- logstash_async/formatter.py | 14 ++++++++++---- logstash_async/utils.py | 1 + tests/formatter_test.py | 27 +++++++++++++++++++-------- tests/utils_test.py | 3 ++- 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/logstash_async/constants.py b/logstash_async/constants.py index b12edfd..0ae01ef 100644 --- a/logstash_async/constants.py +++ b/logstash_async/constants.py @@ -44,7 +44,8 @@ class Constants: FORMATTER_LOGSTASH_MESSAGE_FIELD_LIST = [ '@timestamp', '@version', 'host', 'level', 'logsource', 'message', 'pid', 'program', 'type', 'tags', '@metadata'] - FORMATTER_LOGSTASH_ECS_MESSAGE_FIELD_LIST = ['@timestamp', '@version', '@metadata', 'message', 'labels', 'tags'] + FORMATTER_LOGSTASH_ECS_MESSAGE_FIELD_LIST = [ + '@timestamp', '@version', '@metadata', 'message', 'labels', 'tags'] # convert dotted ECS fields into nested objects FORMATTER_LOGSTASH_ECS_NORMALIZE_MESSAGE = True # enable rate limiting for error messages (e.g. network errors) emitted by the logger diff --git a/logstash_async/formatter.py b/logstash_async/formatter.py index 15ac7c0..edf15aa 100644 --- a/logstash_async/formatter.py +++ b/logstash_async/formatter.py @@ -15,6 +15,7 @@ from logstash_async.utils import normalize_ecs_dict import logstash_async + try: import json except ImportError: @@ -265,7 +266,8 @@ class LogstashEcsFormatter(LogstashFormatter): } normalize_ecs_message = constants.FORMATTER_LOGSTASH_ECS_NORMALIZE_MESSAGE - top_level_field_set = {*constants.FORMATTER_LOGSTASH_ECS_MESSAGE_FIELD_LIST, *__schema_dict.values()} + top_level_field_set = {*constants.FORMATTER_LOGSTASH_ECS_MESSAGE_FIELD_LIST, + *__schema_dict.values()} MessageSchema = type('MessageSchema', (LogstashFormatter.MessageSchema,), __schema_dict) def _get_primary_fields(self, record): @@ -277,6 +279,7 @@ def _get_primary_fields(self, record): def _format_to_dict(self, record): message = super()._format_to_dict(record) if self.normalize_ecs_message: + # pylint: disable-next=redefined-variable-type message = normalize_ecs_dict(message) return message @@ -435,19 +438,22 @@ def __init__(self, *args, **kwargs): # ---------------------------------------------------------------------- def _fetch_flask_version(self): - from flask import __version__ # pylint: disable=import-error,import-outside-toplevel + # pylint: disable-next=import-error,import-outside-toplevel,no-name-in-module + from flask import __version__ self._flask_version = __version__ # ---------------------------------------------------------------------- def _get_extra_fields(self, record): - from flask import request # pylint: disable=import-error,import-outside-toplevel + # pylint: disable-next=import-error,import-outside-toplevel + from flask import request extra_fields = super()._get_extra_fields(record) Schema = self.MessageSchema extra_fields[Schema.FLASK_VERSION] = self._flask_version if request: # request might be unbound in other threads - extra_fields[Schema.REQ_USER_AGENT] = str(request.user_agent) if request.user_agent else '' + extra_fields[Schema.REQ_USER_AGENT] = (str(request.user_agent) + if request.user_agent else '') extra_fields[Schema.REQ_REMOTE_ADDRESS] = request.remote_addr extra_fields[Schema.REQ_HOST] = request.host.split(':', 1)[0] extra_fields[Schema.REQ_URI] = request.url diff --git a/logstash_async/utils.py b/logstash_async/utils.py index 49a2c5b..9274538 100644 --- a/logstash_async/utils.py +++ b/logstash_async/utils.py @@ -64,6 +64,7 @@ def import_string(dotted_path): # ---------------------------------------------------------------------- +# pylint: disable-next=invalid-name class normalize_ecs_dict: """ Convert dotted ecs fields into nested objects. diff --git a/tests/formatter_test.py b/tests/formatter_test.py index c7c7398..0838427 100644 --- a/tests/formatter_test.py +++ b/tests/formatter_test.py @@ -3,18 +3,25 @@ # This software may be modified and distributed under the terms # of the MIT license. See the LICENSE file for details. -import socket from contextlib import suppress from logging import FileHandler, makeLogRecord +from types import SimpleNamespace +from unittest.mock import patch import os +import socket import sys import unittest -from types import SimpleNamespace -from unittest.mock import patch +from logstash_async.formatter import ( + DjangoLogstashEcsFormatter, + DjangoLogstashFormatter, + FlaskLogstashEcsFormatter, + FlaskLogstashFormatter, + LogstashEcsFormatter, + LogstashFormatter, +) import logstash_async -from logstash_async.formatter import LogstashFormatter, DjangoLogstashFormatter, FlaskLogstashFormatter, \ - LogstashEcsFormatter, DjangoLogstashEcsFormatter, FlaskLogstashEcsFormatter + # pylint: disable=protected-access @@ -190,12 +197,15 @@ class _LogstashEcsFormatter(LogstashEcsFormatter): class DjangoTestMixin: @classmethod - def setUpClass(cls): + def setUpClass(cls): # pylint: disable=invalid-name super().setUpClass() - import django + # pylint: disable=import-outside-toplevel from django.conf import settings from django.http import HttpRequest + import django + + # pylint: enable=import-outside-toplevel with suppress(RuntimeError): settings.configure() @@ -342,9 +352,10 @@ def test_default_schema(self): class FlaskTestMixin: @classmethod - def setUpClass(cls): + def setUpClass(cls): # pylint: disable=invalid-name super().setUpClass() + # pylint: disable-next=import-outside-toplevel,no-name-in-module from flask import __version__ cls.flask_version = __version__ diff --git a/tests/utils_test.py b/tests/utils_test.py index 11d24d4..1b1c749 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -1,5 +1,5 @@ -import unittest from copy import deepcopy +import unittest from logstash_async.utils import normalize_ecs_dict @@ -45,5 +45,6 @@ def test_normalization(self): with self.subTest('source dict not mutated'): self.assertDictEqual(d, d_copy) + # pylint: disable-next=unsubscriptable-object result['c']['d'].append(22) self.assertDictEqual(d, d_copy)