From 25254d4d572beabb7ba7e7962a21bad74d827a67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Tr=C3=B6ger?= Date: Sat, 13 Apr 2024 19:32:30 +0200 Subject: [PATCH] LogstashFormatter: Move top_level_field_set creation to __init__ This is a follow-up to eea419c2f055f7c2cac0fe8297ca76c77af2e77a to ease setting FORMATTER_LOGSTASH_MESSAGE_FIELD_LIST after importing the formatter class. --- logstash_async/formatter.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/logstash_async/formatter.py b/logstash_async/formatter.py index 3f254b0..e57c84a 100644 --- a/logstash_async/formatter.py +++ b/logstash_async/formatter.py @@ -27,8 +27,6 @@ class LogstashFormatter(logging.Formatter): _basic_data_types = (type(None), bool, str, int, float) - top_level_field_set = set(constants.FORMATTER_LOGSTASH_MESSAGE_FIELD_LIST) - class MessageSchema: TIMESTAMP = '@timestamp' VERSION = '@version' @@ -89,6 +87,7 @@ def __init__( self._prefetch_program_name() self.field_skip_set = set(constants.FORMATTER_RECORD_FIELD_SKIP_LIST) + self.top_level_field_set = set(constants.FORMATTER_LOGSTASH_MESSAGE_FIELD_LIST) # ---------------------------------------------------------------------- def _prefetch_interpreter(self): @@ -271,10 +270,13 @@ 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()} MessageSchema = type('MessageSchema', (LogstashFormatter.MessageSchema,), __schema_dict) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.top_level_field_set = {*constants.FORMATTER_LOGSTASH_ECS_MESSAGE_FIELD_LIST, + *self.__schema_dict.values()} + def _get_primary_fields(self, record): message = super()._get_primary_fields(record) Schema = self.MessageSchema @@ -408,13 +410,16 @@ class DjangoLogstashEcsFormatter(DjangoLogstashFormatter, LogstashEcsFormatter): 'REQ_REFERER': 'http.request.referrer', } - top_level_field_set = LogstashEcsFormatter.top_level_field_set | set(__schema_dict.values()) MessageSchema = type( 'MessageSchema', (DjangoLogstashFormatter.MessageSchema, LogstashEcsFormatter.MessageSchema), __schema_dict, ) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.top_level_field_set = self.top_level_field_set | set(self.__schema_dict.values()) + def _remove_excluded_fields(self, message): message.pop('status_code', None) super()._remove_excluded_fields(message) @@ -499,13 +504,16 @@ class FlaskLogstashEcsFormatter(FlaskLogstashFormatter, LogstashEcsFormatter): 'REQ_ID': 'http.request.id', } - top_level_field_set = LogstashEcsFormatter.top_level_field_set | set(__schema_dict.values()) MessageSchema = type( 'MessageSchema', (FlaskLogstashFormatter.MessageSchema, LogstashEcsFormatter.MessageSchema), __schema_dict, ) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.top_level_field_set = self.top_level_field_set | set(self.__schema_dict.values()) + def _remove_excluded_fields(self, message): message.pop('status_code', None) super()._remove_excluded_fields(message)