# redis-log-handler
Log handler to forward logs to Redis.
| [Installation](#installation) | [Usage](#usage) | [Handler classes](#handlers-classes) | [Documentation](https://redis-log-handler.readthedocs.io/en/stable/) |
| :---------------------------: | :-------------: | :----------------------------------: | :------------------------------------------------------------------: |
## 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.
## Handlers classes
Currently `rlh` implements two classes of handlers:
- [`RedisStreamLogHandler`](#redisstreamloghandler)
- [`RedisPubSubLogHandler`](#redispubsubloghandler)
### `RedisStreamLogHandler`
Handler used to forward logs to a [Redis stream](https://redis.io/docs/data-types/streams/).
### `RedisPubSubLogHandler`
Handler used to publish logs to a [Redis pub/sub](https://redis.io/docs/manual/pubsub/) channel.
> :warning: Before using `RedisPubSubLogHandler`, make sure to define at least one listener to the channel, otherwise the logs emitted will be lost
Raw data
{
"_id": null,
"home_page": "https://github.com/Iglesys347/redis-log-handler",
"name": "redis-logs",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": "",
"keywords": "Redis,logging",
"author": "Iglesys347",
"author_email": "g.imbert34@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/9e/c5/3a19ab6a162057897cfc28a8b73efc1c2f467a292cd29be22e772c9612cf/redis-logs-1.1.0.tar.gz",
"platform": null,
"description": "# redis-log-handler\n\nLog handler to forward logs to Redis.\n\n| [Installation](#installation) | [Usage](#usage) | [Handler classes](#handlers-classes) | [Documentation](https://redis-log-handler.readthedocs.io/en/stable/) |\n| :---------------------------: | :-------------: | :----------------------------------: | :------------------------------------------------------------------: |\n\n## Installation\n\nInstallation with `pip`:\n\n```bash\npip install redis-logs\n```\n\n## Usage\n\n### Basic example\n\nSetup log forwarding to a redis stream:\n\n```python\nfrom rlh import RedisStreamLogHandler\n\n# define your logger\nlogger = logging.getLogger('my_app')\n\n# define the Redis log handler\nhandler = RedisStreamLogHandler()\n# add the handler to the logger\nlogger.addHandler(handler)\n```\n\nAfter 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`.\n\n### Use a different stream name\n\n```python\nfrom rlh import RedisStreamLogHandler\n\n# define your logger\nlogger = logging.getLogger('my_app')\n\n# define the Redis log handler\nhandler = RedisStreamLogHandler(stream_name=\"custom_stream_name\")\n# add the handler to the logger\nlogger.addHandler(handler)\n```\n\n### Specify a custom Redis client\n\nTo use a custom Redis client, you can either define your own client with `redis.Redis` and then pass it to the handler:\n\n```python\nfrom redis import Redis\nfrom rlh import RedisStreamLogHandler\n\n# define a custom Redis client\nclient = Redis(host=\"redis\", port=6380, db=1)\n\n# define your logger\nlogger = logging.getLogger('my_app')\n\n# define the Redis log handler with custom Redis client\nhandler = RedisStreamLogHandler(redis_client=client)\n# add the handler to the logger\nlogger.addHandler(handler)\n```\n\nOr dirrectly call the handler constructor with your custom Redis settings:\n\n```python\nfrom rlh import RedisStreamLogHandler\n\n# define your logger\nlogger = logging.getLogger('my_app')\n\n# define the Redis log handler with custom Redis client\nhandler = RedisStreamLogHandler(host=\"redis\", port=6380, db=1)\n# add the handler to the logger\nlogger.addHandler(handler)\n```\n\n### Specify custom log fields to save\n\nBy 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)):\n\n```python\nfrom rlh import RedisStreamLogHandler\n\n# define your logger\nlogger = logging.getLogger('my_app')\n\n# define the Redis log handler with custom fields\nhandler = RedisStreamLogHandler(fields=[\"msg\", \"name\", \"module\", \"levelno\"])\n# add the handler to the logger\nlogger.addHandler(handler)\n```\n\n### Save `LogRecord` as pickle format\n\nLogs can also be saved in DB as [pickle format](https://docs.python.org/3/library/pickle.html):\n\n```python\nfrom rlh import RedisStreamLogHandler\n\n# define your logger\nlogger = logging.getLogger('my_app')\n\n# define the Redis log handler with custom fields\nhandler = RedisStreamLogHandler(as_pkl=True)\n# add the handler to the logger\nlogger.addHandler(handler)\n```\n\nThis can be useful if you need to re-use the logs with another python program.\n\n## Handlers classes\n\nCurrently `rlh` implements two classes of handlers:\n\n- [`RedisStreamLogHandler`](#redisstreamloghandler)\n- [`RedisPubSubLogHandler`](#redispubsubloghandler)\n\n### `RedisStreamLogHandler`\n\nHandler used to forward logs to a [Redis stream](https://redis.io/docs/data-types/streams/).\n\n### `RedisPubSubLogHandler`\n\nHandler used to publish logs to a [Redis pub/sub](https://redis.io/docs/manual/pubsub/) channel.\n\n> :warning: Before using `RedisPubSubLogHandler`, make sure to define at least one listener to the channel, otherwise the logs emitted will be lost\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Python log handler to forward logs to Redis database",
"version": "1.1.0",
"split_keywords": [
"redis",
"logging"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ed4850087f5bbbdf910780a5e02d5f0043b721219940aee1a9318afca960db35",
"md5": "a0e35f5da777c88e9db343e3238e71de",
"sha256": "c6855d56c9e2b9b7fb9df2718c43bbb6d29cffe9d0424c35e72488176d399e39"
},
"downloads": -1,
"filename": "redis_logs-1.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "a0e35f5da777c88e9db343e3238e71de",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 5760,
"upload_time": "2023-02-07T21:13:48",
"upload_time_iso_8601": "2023-02-07T21:13:48.580515Z",
"url": "https://files.pythonhosted.org/packages/ed/48/50087f5bbbdf910780a5e02d5f0043b721219940aee1a9318afca960db35/redis_logs-1.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9ec53a19ab6a162057897cfc28a8b73efc1c2f467a292cd29be22e772c9612cf",
"md5": "0332ed1cfcc00b98e0a48116d35735b9",
"sha256": "e370f642041e442eab4c94e5586ff9aecd5a0a14f2ebe562bcf042154c3dde1d"
},
"downloads": -1,
"filename": "redis-logs-1.1.0.tar.gz",
"has_sig": false,
"md5_digest": "0332ed1cfcc00b98e0a48116d35735b9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 5565,
"upload_time": "2023-02-07T21:13:50",
"upload_time_iso_8601": "2023-02-07T21:13:50.372136Z",
"url": "https://files.pythonhosted.org/packages/9e/c5/3a19ab6a162057897cfc28a8b73efc1c2f467a292cd29be22e772c9612cf/redis-logs-1.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-02-07 21:13:50",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "Iglesys347",
"github_project": "redis-log-handler",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "async-timeout",
"specs": [
[
"==",
"4.0.2"
]
]
},
{
"name": "redis",
"specs": [
[
"==",
"4.4.0"
]
]
}
],
"lcname": "redis-logs"
}