# -*- coding: utf-8 -*-
from setuptools import setup

packages = \
['opensearch_logger']

package_data = \
{'': ['*']}

install_requires = \
['opensearch-py>=1.0.0,<2.0.0']

setup_kwargs = {
    'name': 'opensearch-logger',
    'version': '0.1.0',
    'description': 'OpenSearch logging handler',
    'long_description': '# Opensearch Logger for Python\n\n<p>\n    <a href="https://pypi.org/pypi/opensearch-logger"><img alt="Supported python versions" src="https://img.shields.io/pypi/pyversions/opensearch-logger?logo=python&logoColor=white"></a>\n    <a href="https://pypi.org/pypi/opensearch-logger"><img alt="Package stability" src="https://img.shields.io/pypi/status/opensearch-logger?logo=python&logoColor=white"></a>\n    <a href="https://pypi.org/pypi/opensearch-logger"><img alt="License" src="https://img.shields.io/pypi/l/opensearch-logger"></a>\n    <a href="https://github.com/vduseev/opensearch-logger/actions/workflows/test.yml"><img alt="Tests (main branch)" src="https://img.shields.io/github/workflow/status/vduseev/opensearch-logger/Test/main?logo=github"></a>\n</p>\n\nThis library provides a standard Python logging handler compatible with [Opensearch][opensearch] suite.\n\nThe **goals** of this project are\n\n* to provide a **simple** and direct logging from Python to Opensearch without *fluentd*, *logstash* or other middleware;\n* keep it up to date with the growing difference between Opensearch and Elasticsearch projects;\n* keep the library easy to use, robust, and simple (entire source code is currently `~300` lines).\n\nThe library has been open-sourced from an internal project where it has been successfully used in production\nsince the release of Opensearch 1.0.\n\nGenerated log records follow the [Elastic Common Schema (ECS)][ecs] field naming convention.\nFor better performance it is recommended to set up a proper mapping for you logging indices but everything will\nwork even without it. You can find a ready to use [compatible JSON mapping][ecs-mapping] in the repository.\n\n## Installation\n\n```shell\npip install opensearch-logger\n```\n\n## Usage\n\nJust add the handler to your logger as follows\n\n```python\nimport logging\nfrom opensearch_logger import OpensearchHandler\n\nhandler = OpensearchHandler(\n    hosts=["https://localhost:9200"],\n    http_auth=("admin", "admin"),\n    use_ssl=True,\n    verify_certs=False,\n    index_prefix="my-logs",\n)\n\nlogger = logging.getLogger(__name__)\nlogger.setLevel(logging.INFO)\nlogger.addHandler(handler)\n```\n\nTo log into Opensearch, simply use the regular logging commands:\n\n```python\nlogger.info("This message will be indexed in Opensearch")\n\n# Report extra fields\nstart_time = time.perf_counter()\nheavy_database_operation()\nelapsed_time = time.perf_counter() - start_time\n\nlogger.info(f"Database operation took {elapsed_time:.3f} seconds", extra={"elapsed_time": elapsed_time})\n```\n\n## Configuration\n\nThe constructor takes the following parameters\n\n| Argument | Default | Description |\n| - | - | - |\n| `index_prefix` | `python-logs` | Prefix of the Opensearch index name that will be created. |\n| `index_rotate` | `OpensearchHandler.DAILY` | The period which is used to generate actual index names and rotate them. |\n| `buffer_size` | 1000 | Number of log records which when reached on the internal buffer results in a flush to Opensearch. |\n| `flush_frequency` | 1 | Float representing how often the buffer will be flushed (in seconds). |\n| `extra_fields` | `{}` | Nested dictionary with all the additional fields that you would like to add to all logs. |\n\nAll other keyword arguments are passed directly to the underlying Opensearch python client.\nFull list of connection parameters can be found in [`opensearch-py`][opensearch-py] docs. Here are few examples:\n\n* `hosts`:  The list of hosts that Opensearch client will connect, multiple hosts are allowed.\n\n  ```python\n  [{"host": "localhost", "port": 9200}, "https://admin:admin@opensearch:9200"]\n  ```\n\n* `http_auth`: Tuple with user name and password that will be used to authenticate against the Opensearch servers.\n\n  ```python\n  ("admin","admin")\n  ```\n\n* `use_ssl`: A boolean that defines if the communications should be SSL encrypted.\n* `verify_certs`: A boolean that defines whether the SSL certificates are validated or not.\n\n## Configuring using dictConfig or in Django\n\nA complete configuration of logging is also supported.\nJust specify the `opensearch_logger.OpensearchHandler` as one of the handlers.\n\nFull guide on tweaking `logging.config` can be found in the official python documentation for [`dictConfig`][dictConfig].\n\n```python\nimport logging.config\n\nLOGGING = {\n    "version": 1,\n    "disable_existing_loggers": True,\n    "formatters": {\n        "standard": {\n            "format": "%(asctime)-15s | %(process)d | %(levelname)s | %(name)s | %(message)s",\n            "datefmt": "%Y-%m-%d %H:%M:%S %z",\n        },\n    },\n    "handlers": {\n        "console": {\n            "level": "INFO",\n            "formatter": "standard",\n            "class": "logging.StreamHandler",\n            "stream": "ext://sys.stderr",\n        },\n        "file": {\n            "level": "DEBUG",\n            "class": "logging.handlers.RotatingFileHandler",\n            "filename": "./debug.log",\n            "maxBytes": 102400,\n            "backupCount": 5,\n        },\n        "opensearch": {\n            "level": "INFO",\n            "class": "opensearch_logger.OpensearchHandler",\n            "hosts": [{"host": "localhost", "port": 9200}],\n            "index_prefix": "my-logs",\n            "extra_fields": {"App": "test", "Environment": "dev"},\n            "use_ssl": True,\n            "verify_certs": False,\n        },\n    },\n    "loggers": {\n        "root": {\n            "handlers": ["console", "file", "opensearch"],\n            "level": "INFO",\n            "propogate": False,\n        },\n        "django": {\n            "handlers": ["file","opensearch"],\n            "level": "DEBUG",\n            "propagate": True,\n        },\n    },\n}\n\nlogging.config.dictConfig(LOGGING)\n```\n\nIn the example above following things are configured:\n\n* 1 formatter named "standard" that will be used to output to the terminal.\n* 3 different log handlers:\n  * One named `console` for logging to terminal, through the `stderr` stream\n  * One called `file` for logging into a rotated log file\n  * And finally one for Opensearch\n* 2 loggers are configured:\n  * `root`, from which all other loggers are derived, will log all messages with level INFO or higher using all three handlers.\n  * `django` handler log all messages with level DEBUG or higher using file and opensearch handlers.\n\n## Dependencies\n\nThis library uses the following packages\n\n* [`opensearch-py`][opensearch-py]\n\n## Building from source & Developing\n\nThis package uses [`pyenv`][pyenv] (optional) and [Poetry][poetry] for development purposes.\nIt also uses Docker to run Opensearch container for integration testing during development.\n\n1. Clone the repo\n1. Instruct poetry to use a proper Python version for virtual environment creation.\n\n   ```shell\n   poetry env use 3.8.12\n   ```\n\n1. Create virtual environment and install dependencies\n\n   ```shell\n   poetry install\n   ```\n\n1. Test that the library works\n\n   **WARNING**: You need Docker installed and available on the system to run the tests.\n   Part of the tests verifies that logs actually get into Opensearch that runs in a docker container.\n\n   ```shell\n   poetry run pytest\n   ```\n\n1. Build a package\n\n   ```shell\n   poetry build\n   ```\n\n## Contributions\n\nContributions are welcome! 👏  🎉\n\nPlease create a GitHub issue and a Pull Request that references that issue as well as your proposed changes.\nYour Pull Request will be automatically tested using GitHub actions.\n\nAfter your pull request will be accepted, it will be merged and the version of the library will be bumped\nand released to PyPI.\n\n[opensearch]: https://opensearch.org/\n[opensearch-py]: https://github.com/opensearch-project/opensearch-py\n[logging]: https://docs.python.org/3/library/logging.html\n[ecs]: https://www.elastic.co/guide/en/ecs/current/index.html\n[dictConfig]: https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig\n[pyenv]: https://github.com/pyenv/pyenv\n[poetry]: https://python-poetry.org/\n[ecs-mapping]: https://github.com/vduseev/opensearch-logger/blob/master/mappings/ecs1.4.0_compatible_minimal.json\n',
    'author': 'vduseev',
    'author_email': 'vagiz@duseev.com',
    'maintainer': 'vduseev',
    'maintainer_email': 'vagiz@duseev.com',
    'url': 'https://github.com/vduseev/python-opensearch-logger',
    'packages': packages,
    'package_data': package_data,
    'install_requires': install_requires,
    'python_requires': '>=3.8,<4.0',
}


setup(**setup_kwargs)
