Skip to main content

A custom asynchronous logging library with Elasticsearch integration.

Project description

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:

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.

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.

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:

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 file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

async_elastic_logger-0.4.5.1.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

async_elastic_logger-0.4.5.1.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file async_elastic_logger-0.4.5.1.1.tar.gz.

File metadata

File hashes

Hashes for async_elastic_logger-0.4.5.1.1.tar.gz
Algorithm Hash digest
SHA256 a5fc4aa09b2f5837c0c7b2cc8ae04726521372eb7a7093044511435f283dbb60
MD5 89e513764b1a12d12437e630ef1d5b3f
BLAKE2b-256 1e02726e745a977fe0053422ec3adb7cea034e497e0ffdb8c4067f6548a9a07a

See more details on using hashes here.

File details

Details for the file async_elastic_logger-0.4.5.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for async_elastic_logger-0.4.5.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9db9e8b2d046a6a3af1f7d666fc5c891c184d7bb35e1847c3475c152e2f75fa2
MD5 609ec30e8b312407ec96ccd8e025454c
BLAKE2b-256 ca421c930fd669827ef3d0da9e54f636a814955b29d90b1888e4fb811264d2fa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page