Skip to content

Commit

Permalink
LogstashFormatter: Move top_level_field_set creation to __init__
Browse files Browse the repository at this point in the history
This is a follow-up to eea419c
to ease setting FORMATTER_LOGSTASH_MESSAGE_FIELD_LIST after
importing the formatter class.
  • Loading branch information
eht16 committed Apr 13, 2024
1 parent be6e30e commit 25254d4
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions logstash_async/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

0 comments on commit 25254d4

Please sign in to comment.