Skip to content

Commit

Permalink
Use field sets instead of lists
Browse files Browse the repository at this point in the history
  • Loading branch information
andriilahuta committed Jan 4, 2024
1 parent 20535be commit 2433756
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions logstash_async/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class LogstashFormatter(logging.Formatter):

_basic_data_types = (type(None), bool, str, int, float)

formatter_record_field_skip_list = constants.FORMATTER_RECORD_FIELD_SKIP_LIST
formatter_logstash_message_field_list = constants.FORMATTER_LOGSTASH_MESSAGE_FIELD_LIST
formatter_record_field_skip_set = set(constants.FORMATTER_RECORD_FIELD_SKIP_LIST)
formatter_logstash_message_field_set = set(constants.FORMATTER_LOGSTASH_MESSAGE_FIELD_LIST)

class MessageSchema:
TIMESTAMP = '@timestamp'
Expand Down Expand Up @@ -217,7 +217,7 @@ def _format_exception(self, exc_info):
def _remove_excluded_fields(self, message, extra_fields):
for fields in (message, extra_fields):
for field_name in list(fields):
if field_name in self.formatter_record_field_skip_list:
if field_name in self.formatter_record_field_skip_set:
del fields[field_name]

# ----------------------------------------------------------------------
Expand All @@ -232,9 +232,9 @@ def _move_extra_record_fields_to_prefix(self, message):
return # early out if no prefix is configured

message.setdefault(self._extra_prefix, {})
field_skip_list = self.formatter_logstash_message_field_list + [self._extra_prefix]
field_skip_set = self.formatter_logstash_message_field_set | {self._extra_prefix}
for key in list(message):
if key not in field_skip_list:
if key not in field_skip_set:
message[self._extra_prefix][key] = message.pop(key)

# ----------------------------------------------------------------------
Expand Down Expand Up @@ -266,8 +266,8 @@ class LogstashEcsFormatter(LogstashFormatter):
'THREAD_NAME': 'process.thread.name',
}

formatter_logstash_message_field_list = (LogstashFormatter.formatter_logstash_message_field_list
+ list(__schema_dict.values()))
formatter_logstash_message_field_set = (LogstashFormatter.formatter_logstash_message_field_set
| set(__schema_dict.values()))
MessageSchema = type('MessageSchema', (LogstashFormatter.MessageSchema,), __schema_dict)

def _post_process_message(self, message):
Expand Down Expand Up @@ -395,8 +395,8 @@ class DjangoLogstashEcsFormatter(DjangoLogstashFormatter, LogstashEcsFormatter):
'REQ_REFERER': 'http.request.referrer',
}

formatter_logstash_message_field_list = (LogstashEcsFormatter.formatter_logstash_message_field_list
+ list(__schema_dict.values()))
formatter_logstash_message_field_set = (LogstashEcsFormatter.formatter_logstash_message_field_set
| set(__schema_dict.values()))
MessageSchema = type(
'MessageSchema',
(DjangoLogstashFormatter.MessageSchema, LogstashEcsFormatter.MessageSchema),
Expand Down Expand Up @@ -486,8 +486,8 @@ class FlaskLogstashEcsFormatter(FlaskLogstashFormatter, LogstashEcsFormatter):
'REQ_ID': 'http.request.id',
}

formatter_logstash_message_field_list = (LogstashEcsFormatter.formatter_logstash_message_field_list
+ list(__schema_dict.values()))
formatter_logstash_message_field_set = (LogstashEcsFormatter.formatter_logstash_message_field_set
| set(__schema_dict.values()))
MessageSchema = type(
'MessageSchema',
(FlaskLogstashFormatter.MessageSchema, LogstashEcsFormatter.MessageSchema),
Expand Down

0 comments on commit 2433756

Please sign in to comment.