Skip to content

Commit

Permalink
Merge pull request #2 from Iglesys347/1-prepare-for-pypi-publish
Browse files Browse the repository at this point in the history
1 prepare for pypi publish
  • Loading branch information
Iglesys347 authored Dec 15, 2022
2 parents 2cfad3e + 30074b4 commit 0d5f42a
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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')
36 changes: 36 additions & 0 deletions .github/workflows/pypi-publish.yml
Original file line number Diff line number Diff line change
@@ -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 }}
117 changes: 117 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,120 @@
# redis-log-handler

Log handler to forward logs to Redis

<center>

| [Installation](#installation) | [Usage](#usage) |
| :---------------------------: | :-------------: |

</center>

## 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.
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
async-timeout==4.0.2
redis==4.4.0
9 changes: 9 additions & 0 deletions rlh/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rlh.handlers import (
RedisLogHandler,
RedisStreamLogHandler,
)

__all__ = [
"RedisLogHandler",
"RedisStreamLogHandler",
]
File renamed without changes.
45 changes: 45 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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={},
)

0 comments on commit 0d5f42a

Please sign in to comment.