# Async Logger
Async Logger is a custom Python logging library designed to log messages asynchronously to both the console and Elasticsearch.
It is built on top of Python's `logging` module and integrates with Elasticsearch using the `AsyncElasticsearch` client.
This allows for non-blocking, asynchronous logging in distributed environments.
## Features
- Asynchronous logging to Elasticsearch.
- Buffered log messages with periodic flushing to avoid performance hits.
- Customizable logging levels (DEBUG, INFO, WARNING, ERROR, CRITICAL).
- Integrated console logging.
## Installation
Install the package using pip:
```bash
pip install async_elastic_logger
```
## Usage
Here’s an example of how to use `AsyncLogger` in your Python application.
### 1. Define your configuration class using `pydantic`
You'll need to use `pydantic` to define a settings class that contains your Elasticsearch configurations.
```python
from pydantic import BaseSettings
class ElasticLoggerConfig(BaseSettings):
elastic_url: str
elastic_username: str
elastic_password: str
elastic_log_level: str = "WARNING"
elastic_log_index_name: str = "logs"
```
### 2. Use the `get_logger` function to get the singleton logger
Once you have the configuration, you can get the logger instance using the `get_logger` function. The logger is created as a singleton, so the same instance will be returned every time you call `get_logger`.
```python
from async_logger.logger import get_logger, ElasticLoggerConfig
# Define your Elasticsearch logger configurations
config = ElasticLoggerConfig(
elastic_url="https://your-elasticsearch-url",
elastic_username="your-username",
elastic_password="your-password",
elastic_log_level="INFO", # or DEBUG, ERROR, etc.
elastic_log_index_name="your-log-index"
)
# Get the singleton logger instance
logger = get_logger(config)
# Log messages
await logger.info("This is an info message")
await logger.error("This is an error message")
await logger.debug("This is a debug message")
```
### 3. Handling log levels
You can specify the logging level for Elasticsearch logs using the `elastic_log_level` field in your configuration. It accepts values like `DEBUG`, `INFO`, `WARNING`, `ERROR`, or `CRITICAL`.
For example:
```python
config = ElasticLoggerConfig(
elastic_url="https://your-elasticsearch-url",
elastic_username="your-username",
elastic_password="your-password",
elastic_log_level="DEBUG", # Logs at DEBUG level
elastic_log_index_name="your-log-index"
)
```
### 4. Configuration flexibility
You can easily switch the log level or Elasticsearch index by updating the configuration parameters. This allows you to fine-tune logging based on the environment or the level of detail you want in your logs.
### 5. Singleton behavior
Once the logger is instantiated with the configuration, subsequent calls to `get_logger` will return the same instance, ensuring that logging across your application is handled consistently.
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
Raw data
{
"_id": null,
"home_page": "https://github.com/nimamiri9248/async_elastic_logger",
"name": "async-elastic-logger",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Nima Miri",
"author_email": "nimamiri9248@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/11/7b/fa2238ac8afad53e06015623550cf070b5b80e8bbe976b05aac24f558ca0/async_elastic_logger-0.4.5.0.tar.gz",
"platform": null,
"description": "\n# Async Logger\n\nAsync Logger is a custom Python logging library designed to log messages asynchronously to both the console and Elasticsearch. \nIt is built on top of Python's `logging` module and integrates with Elasticsearch using the `AsyncElasticsearch` client. \nThis allows for non-blocking, asynchronous logging in distributed environments.\n\n## Features\n\n- Asynchronous logging to Elasticsearch.\n- Buffered log messages with periodic flushing to avoid performance hits.\n- Customizable logging levels (DEBUG, INFO, WARNING, ERROR, CRITICAL).\n- Integrated console logging.\n\n## Installation\n\nInstall the package using pip:\n\n```bash\npip install async_elastic_logger\n```\n\n## Usage\n\nHere\u2019s an example of how to use `AsyncLogger` in your Python application.\n\n### 1. Define your configuration class using `pydantic`\n\nYou'll need to use `pydantic` to define a settings class that contains your Elasticsearch configurations.\n\n```python\nfrom pydantic import BaseSettings\n\nclass ElasticLoggerConfig(BaseSettings):\n elastic_url: str\n elastic_username: str\n elastic_password: str\n elastic_log_level: str = \"WARNING\"\n elastic_log_index_name: str = \"logs\"\n```\n\n### 2. Use the `get_logger` function to get the singleton logger\n\nOnce you have the configuration, you can get the logger instance using the `get_logger` function. The logger is created as a singleton, so the same instance will be returned every time you call `get_logger`.\n\n```python\nfrom async_logger.logger import get_logger, ElasticLoggerConfig\n\n# Define your Elasticsearch logger configurations\nconfig = ElasticLoggerConfig(\n elastic_url=\"https://your-elasticsearch-url\",\n elastic_username=\"your-username\",\n elastic_password=\"your-password\",\n elastic_log_level=\"INFO\", # or DEBUG, ERROR, etc.\n elastic_log_index_name=\"your-log-index\"\n)\n\n# Get the singleton logger instance\nlogger = get_logger(config)\n\n# Log messages\nawait logger.info(\"This is an info message\")\nawait logger.error(\"This is an error message\")\nawait logger.debug(\"This is a debug message\")\n```\n\n### 3. Handling log levels\n\nYou can specify the logging level for Elasticsearch logs using the `elastic_log_level` field in your configuration. It accepts values like `DEBUG`, `INFO`, `WARNING`, `ERROR`, or `CRITICAL`.\n\nFor example:\n\n```python\nconfig = ElasticLoggerConfig(\n elastic_url=\"https://your-elasticsearch-url\",\n elastic_username=\"your-username\",\n elastic_password=\"your-password\",\n elastic_log_level=\"DEBUG\", # Logs at DEBUG level\n elastic_log_index_name=\"your-log-index\"\n)\n```\n\n### 4. Configuration flexibility\n\nYou can easily switch the log level or Elasticsearch index by updating the configuration parameters. This allows you to fine-tune logging based on the environment or the level of detail you want in your logs.\n\n### 5. Singleton behavior\n\nOnce the logger is instantiated with the configuration, subsequent calls to `get_logger` will return the same instance, ensuring that logging across your application is handled consistently.\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A custom asynchronous logging library with Elasticsearch integration.",
"version": "0.4.5.0",
"project_urls": {
"Homepage": "https://github.com/nimamiri9248/async_elastic_logger"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c24ad11b215e6e02257a68cf9692feaf368ea0dcb6aca7f38a5aee890a064eaa",
"md5": "8e4b55aaa699ad8f5a7de851f870c213",
"sha256": "242780a18479b76addaa8e877a2f4ee8844fcee16b2ec44e67665c7ca7407c0a"
},
"downloads": -1,
"filename": "async_elastic_logger-0.4.5.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8e4b55aaa699ad8f5a7de851f870c213",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 5859,
"upload_time": "2024-12-26T09:39:36",
"upload_time_iso_8601": "2024-12-26T09:39:36.499756Z",
"url": "https://files.pythonhosted.org/packages/c2/4a/d11b215e6e02257a68cf9692feaf368ea0dcb6aca7f38a5aee890a064eaa/async_elastic_logger-0.4.5.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "117bfa2238ac8afad53e06015623550cf070b5b80e8bbe976b05aac24f558ca0",
"md5": "c8641ebc0ef4bbcb3bc32f2d7be2ecd5",
"sha256": "49137b50ff24b99af0f70fd06b4d916c74247d3c6303c6f4715dd2ebe9147aee"
},
"downloads": -1,
"filename": "async_elastic_logger-0.4.5.0.tar.gz",
"has_sig": false,
"md5_digest": "c8641ebc0ef4bbcb3bc32f2d7be2ecd5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 5339,
"upload_time": "2024-12-26T09:39:39",
"upload_time_iso_8601": "2024-12-26T09:39:39.054991Z",
"url": "https://files.pythonhosted.org/packages/11/7b/fa2238ac8afad53e06015623550cf070b5b80e8bbe976b05aac24f558ca0/async_elastic_logger-0.4.5.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-26 09:39:39",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "nimamiri9248",
"github_project": "async_elastic_logger",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "async-elastic-logger"
}