cmanaha / python-elasticsearch-logger

Python Elasticsearch handler for the standard python logging framework
Other
232 stars 116 forks source link

[Bug] Log message formatted with `%r` raises `BulkIndexError` #62

Open popes01a opened 5 years ago

popes01a commented 5 years ago

I have recently switched to using CMRESHandler and I encoutered some issues when trying to use %r for string formatting.

The handler is set up as below:

handler = {
    'level': 'INFO',
    'class': 'cmreslogging.handlers.CMRESHandler',
    'hosts': [{'host': ELASTICSEARCH_HOST, 'port': ELASTICSEARCH_PORT}],
    'es_index_name': 'app-name',
    'raise_on_indexing_exceptions': True,
}

Assuming I have the following dictionary to log:

parameters = { 
    'month': datetime.datetime(2018, 12, 1, 0, 0, tzinfo=tzutc()), 
    'status': 'complete', 
}

If I do logger.info('Parameters: %r' % parameters) then nothing gets logged and I get the following error:

raise BulkIndexError('%i document(s) failed to index.' % len(errors), errors)

'error': {
    'type': 'mapper_parsing_exception',
    'reason': 'failed to parse field [args] of type [text]',
    'caused_by': {'type': 'illegal_state_exception', 'reason': "Can't get text on a START_OBJECT at 1:610"}
}

I switched to logger.info('Parameters: %s' % repr(parameters)) and that works fine, however it would be nice to be able to use %r and avoid wrapping parameters in repr().