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)