python-elasticsearch-logger icon indicating copy to clipboard operation
python-elasticsearch-logger copied to clipboard

No support for format

Open saadqc opened this issue 7 years ago • 6 comments

I have a custom method to return logstash format. Although initializing it doesn't seem to work.

handler = CMRESHandler(hosts=[{'host': 'localhost', 'port': 9200}],
                               auth_type=CMRESHandler.AuthType.NO_AUTH,
                               es_index_name="my_logs")
handler.setFormatter(LogstashFormatterVersion1())

I noticed in emit method in handlers.py self.format(record) is being called but return dict is not stored anywhere.

saadqc avatar Aug 20 '18 16:08 saadqc

Thanks a lot for the feedback.

I'm not entirely sure why do you need a formatter for this handler in particular? Could you give more context of what are you trying to achieve so we get a better understanding ?

This handler is intended to pick up the full record (including the internal dict), serialise into a json doc and sent/store into elastic in bulk format.

Formatters (please read https://docs.python.org/3.5/library/logging.html#formatter-objects) for more reference are responsible for converting a LogRecord to a string (usually) which can be interpreted by either a human or an external system.

For the CMRESHandler, you do not need a formatter, the library will pick up the internal logrecord and inject directly into elastic.

cmanaha avatar Sep 24 '18 21:09 cmanaha

@saadqc I'm not sure what your use case is but you may be able to use filters to achieve what you're trying to do while using this log handler.

https://docs.python.org/2/howto/logging-cookbook.html#filters-contextual

stephenpaulger avatar Dec 12 '18 15:12 stephenpaulger

@stephenpaulger @cmanaha : I switched to a different package after struggling with this.

I will reproduce this issue and share my findings here. Otherwise close this PR.

Thanks

saadqc avatar Dec 12 '18 15:12 saadqc

@saadqc Interested to hear what package you've switched to. I've had a different issue with this one when using ElasticSearch 6.5.

stephenpaulger avatar Dec 12 '18 15:12 stephenpaulger

@stephenpaulger : it's python-logstash with python's default logging.

        fh = FileHandler(log_file_path)
        fh.setLevel(logging.INFO)
        fh.setFormatter(LogstashFormatterVersion1())

saadqc avatar Dec 12 '18 16:12 saadqc

@cmanaha One use case for using a formatter is Elastic Common Schema. The official Python support for ECS in elastic/ecs-logging-python is implemented as a formatter.

michielvoo avatar Oct 19 '20 14:10 michielvoo