diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index 28b5ce1..71dfcd0 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -21,7 +21,8 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip + pip install -r requirements.txt pip install pylint - name: Analysing the code with pylint run: | - pylint $(git ls-files '*.py') + pylint --fail-under=8 $(git ls-files 'rlh/*.py') diff --git a/.github/workflows/pypi-publish.yml b/.github/workflows/pypi-publish.yml new file mode 100644 index 0000000..26d0817 --- /dev/null +++ b/.github/workflows/pypi-publish.yml @@ -0,0 +1,36 @@ +name: Publish package to Pypi + +on: + release: + types: [published] + +permissions: + contents: read # to fetch code (actions/checkout) + +jobs: + build_and_package: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: install python + uses: actions/setup-python@v4 + with: + python-version: 3.9 + - name: Install dev tools + run: | + pip install -r dev_requirements.txt + pip install twine wheel + - name: Build package + run: | + python setup.py build + python setup.py sdist bdist_wheel + - name: Publish to Test Pypi + uses: pypa/gh-action-pypi-publish@release/v1 + with: + password: ${{ secrets.TEST_PYPI_API_TOKEN }} + repository_url: https://test.pypi.org/legacy/ + # NOT PUBLISHING TO THE REAL Pypi FOR THE MOMENT + # - name: Publish to Pypi + # uses: pypa/gh-action-pypi-publish@release/v1 + # with: + # password: ${{ secrets.PYPI_API_TOKEN }} diff --git a/README.md b/README.md index 43ddeed..5c94e6c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,120 @@ # redis-log-handler Log handler to forward logs to Redis + +
+ +| [Installation](#installation) | [Usage](#usage) | +| :---------------------------: | :-------------: | + +
+ +## Installation + +Installation with `pip`: + +```bash +pip install redis-logs +``` + +## Usage + +### Basic example + +Setup log forwarding to a redis stream: + +```python +from rlh import RedisStreamLogHandler + +# define your logger +logger = logging.getLogger('my_app') + +# define the Redis log handler +handler = RedisStreamLogHandler() +# add the handler to the logger +logger.addHandler(handler) +``` + +After that, all the logs emitted with the logger will be forwarded to a [Redis Stream](https://redis.io/docs/data-types/streams/); by default the logs are forwarded to a Redis instance running at `localhost:6379` in a stream named `logs`. + +### Use a different stream name + +```python +from rlh import RedisStreamLogHandler + +# define your logger +logger = logging.getLogger('my_app') + +# define the Redis log handler +handler = RedisStreamLogHandler(stream_name="custom_stream_name") +# add the handler to the logger +logger.addHandler(handler) +``` + +### Specify a custom Redis client + +To use a custom Redis client, you can either define your own client with `redis.Redis` and then pass it to the handler: + +```python +from redis import Redis +from rlh import RedisStreamLogHandler + +# define a custom Redis client +client = Redis(host="redis", port=6380, db=1) + +# define your logger +logger = logging.getLogger('my_app') + +# define the Redis log handler with custom Redis client +handler = RedisStreamLogHandler(redis_client=client) +# add the handler to the logger +logger.addHandler(handler) +``` + +Or dirrectly call the handler constructor with your custom Redis settings: + +```python +from rlh import RedisStreamLogHandler + +# define your logger +logger = logging.getLogger('my_app') + +# define the Redis log handler with custom Redis client +handler = RedisStreamLogHandler(host="redis", port=6380, db=1) +# add the handler to the logger +logger.addHandler(handler) +``` + +### Specify custom log fields to save + +By default the handler only saves the logs fieds `msg`, `levelname` and `created`. You can however change this default behaviour by setting your own desired fields (see the full list of fields in [logging documentation](https://docs.python.org/3/library/logging.html#logrecord-attributes)): + +```python +from rlh import RedisStreamLogHandler + +# define your logger +logger = logging.getLogger('my_app') + +# define the Redis log handler with custom fields +handler = RedisStreamLogHandler(fields=["msg", "name", "module", "levelno"]) +# add the handler to the logger +logger.addHandler(handler) +``` + +### Save `LogRecord` as pickle format + +Logs can also be saved in DB as [pickle format](https://docs.python.org/3/library/pickle.html): + +```python +from rlh import RedisStreamLogHandler + +# define your logger +logger = logging.getLogger('my_app') + +# define the Redis log handler with custom fields +handler = RedisStreamLogHandler(as_pkl=True) +# add the handler to the logger +logger.addHandler(handler) +``` + +This can be useful if you need to re-use the logs with another python program. diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2809513 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +async-timeout==4.0.2 +redis==4.4.0 diff --git a/rlh/__init__.py b/rlh/__init__.py new file mode 100644 index 0000000..fb29139 --- /dev/null +++ b/rlh/__init__.py @@ -0,0 +1,9 @@ +from rlh.handlers import ( + RedisLogHandler, + RedisStreamLogHandler, +) + +__all__ = [ + "RedisLogHandler", + "RedisStreamLogHandler", +] diff --git a/redis-log-handler.py b/rlh/handlers.py similarity index 100% rename from redis-log-handler.py rename to rlh/handlers.py diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..203a2d2 --- /dev/null +++ b/setup.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +from setuptools import find_packages, setup + +setup( + name="redis-logs", + description="Python log handler to forward logs to Redis database", + long_description=open("README.md").read().strip(), + long_description_content_type="text/markdown", + keywords=["Redis", "logging"], + license="MIT", + version="0.0.1", + packages=find_packages( + include=[ + "rlh", + "rlh.handlers", + ] + ), + url="https://github.com/Iglesys347/redis-log-handler", + project_urls={ + # "Documentation": "TODO:add link to readthedoc", + "Changes": "https://github.com/Iglesys347/redis-log-handler/releases", + "Code": "https://github.com/Iglesys347/redis-log-handler", + "Issue tracker": "https://github.com/Iglesys347/redis-log-handler/issues", + }, + author="Iglesys347", + author_email="g.imbert34@gmail.com", + python_requires=">=3.9", + install_requires=[ + "redis", + ], + classifiers=[ + "Development Status :: 2 - Pre-Alpha", + "Environment :: Console", + "Intended Audience :: Developers", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + ], + extras_require={}, +)