|
1 | 1 |
|
2 | 2 | # CMRESHandler.py
|
| 3 | +This library provides an Elasticsearch logging appender compatible with the |
| 4 | +python standard `logging <https://docs.python.org/2/library/logging.html>`_ library. |
| 5 | + |
| 6 | +The code source is in github at [https://github.com/SHolzhauer/python-elasticsearch-logger](https://github.com/SHolzhauer/python-elasticsearch-logger) |
| 7 | + |
3 | 8 | This is a fork of the [original work](https://github.com/cmanaha/python-elasticsearch-logger) by cmanaha.
|
4 | 9 |
|
5 | 10 | **Tested against**
|
6 |
| -* Elasticsearch 7.3.1 |
| 11 | +* `Elasticsearch 7.3.1` with `Python3.6` |
| 12 | + |
| 13 | + |
| 14 | +## Installation |
| 15 | +WIP |
| 16 | + |
| 17 | +## Requirements |
| 18 | +This library requires the following dependencies |
| 19 | +#### Python 2 |
| 20 | + - elasticsearch |
| 21 | + - requests |
| 22 | + - enum |
| 23 | +#### Python 3 |
| 24 | +- elasticsearch |
| 25 | +- requests |
| 26 | +- packaging |
| 27 | + |
| 28 | +## Using the handler |
| 29 | +To initialise and create the handler, just add the handler to your logger as follow |
| 30 | +```python |
| 31 | +from cmreslogging.handlers import CMRESHandler |
| 32 | + handler = CMRESHandler(hosts=[{'host': 'localhost', 'port': 9200}], |
| 33 | + auth_type=CMRESHandler.AuthType.NO_AUTH, |
| 34 | + es_index_name="my_python_index") |
| 35 | + log = logging.getLogger("PythonTest") |
| 36 | + log.setLevel(logging.INFO) |
| 37 | + log.addHandler(handler) |
| 38 | +``` |
| 39 | + |
| 40 | +You can add fields upon initialisation, providing more data of the execution context |
| 41 | +```python |
| 42 | +from cmreslogging.handlers import CMRESHandler |
| 43 | + handler = CMRESHandler(hosts=[{'host': 'localhost', 'port': 9200}], |
| 44 | + auth_type=CMRESHandler.AuthType.NO_AUTH, |
| 45 | + es_index_name="my_python_index", |
| 46 | + es_additional_fields={'App': 'MyAppName', 'Environment': 'Dev'}) |
| 47 | + log = logging.getLogger("PythonTest") |
| 48 | + log.setLevel(logging.INFO) |
| 49 | + log.addHandler(handler) |
| 50 | +``` |
| 51 | + |
| 52 | +This additional fields will be applied to all logging fields and recorded in elasticsearch |
| 53 | + |
| 54 | +To log, use the regular commands from the logging library |
| 55 | + |
| 56 | +```python |
| 57 | +log.info("This is an info statement that will be logged into elasticsearch") |
| 58 | +``` |
| 59 | + |
| 60 | +Your code can also dump additional extra fields on a per log basis that can be used to instrument |
| 61 | +operations. For example, when reading information from a database you could do something like |
| 62 | +```python |
| 63 | +start_time = time.time() |
| 64 | + database_operation() |
| 65 | + db_delta = time.time() - start_time |
| 66 | + log.debug("DB operation took %.3f seconds" % db_delta, extra={'db_execution_time': db_delta}) |
| 67 | +``` |
| 68 | +The code above executes the DB operation, measures the time it took and logs an entry that contains |
| 69 | +in the message the time the operation took as string and for convenience, it creates another field |
| 70 | +called db_execution_time with a float that can be used to plot the time this operations are taking using |
| 71 | +Kibana on top of elasticsearch |
| 72 | + |
| 73 | +## Initialisation parameters |
| 74 | +The constructors takes the following parameters: |
| 75 | + - hosts: The list of hosts that elasticsearch clients will connect, multiple hosts are allowed, for example |
| 76 | +```python |
| 77 | + [{'host':'host1','port':9200}, {'host':'host2','port':9200}] |
| 78 | +``` |
| 79 | + - auth_type: The authentication currently support CMRESHandler.AuthType = NO_AUTH, BASIC_AUTH, KERBEROS_AUTH |
| 80 | + - auth_details: When CMRESHandler.AuthType.BASIC_AUTH is used this argument must contain a tuple of string with the user and password that will be used to authenticate against the Elasticsearch servers, for example ('User','Password') |
| 81 | + - aws_access_key: When ``CMRESHandler.AuthType.AWS_SIGNED_AUTH`` is used this argument must contain the AWS key id of the the AWS IAM user |
| 82 | + - aws_secret_key: When ``CMRESHandler.AuthType.AWS_SIGNED_AUTH`` is used this argument must contain the AWS secret key of the the AWS IAM user |
| 83 | + - aws_region: When ``CMRESHandler.AuthType.AWS_SIGNED_AUTH`` is used this argument must contain the AWS region of the the AWS Elasticsearch servers, for example ``'us-east'`` |
| 84 | + - use_ssl: A boolean that defines if the communications should use SSL encrypted communication |
| 85 | + - verify_ssl: A boolean that defines if the SSL certificates are validated or not |
| 86 | + - buffer_size: An int, Once this size is reached on the internal buffer results are flushed into ES |
| 87 | + - flush_frequency_in_sec: A float representing how often and when the buffer will be flushed |
| 88 | + - es_index_name: A string with the prefix of the elasticsearch index that will be created. Note a date with |
| 89 | + YYYY.MM.dd, ``python_logger`` used by default |
| 90 | + - index_name_frequency: The frequency to use as part of the index naming. Currently supports |
| 91 | + `CMRESHandler.IndexNameFrequency.DAILY`, `CMRESHandler.IndexNameFrequency.WEEKLY`, |
| 92 | + `CMRESHandler.IndexNameFrequency.MONTHLY`, `CMRESHandler.IndexNameFrequency.YEARLY`, `CMRESHandler.IndexNameFrequency.NONE` by default the daily rotation |
| 93 | + is used |
| 94 | + - es_doc_type: A string with the name of the document type that will be used ``python_log`` used by default |
| 95 | + - es_additional_fields: A dictionary with all the additional fields that you would like to add to the logs |
0 commit comments