Python log handler to forward logs to Redis database
Project description
redis-log-handler
Log handler to forward logs to Redis
Installation | Usage |
---|
Installation
Installation with pip
:
pip install redis-logs
Usage
Basic example
Setup log forwarding to a redis stream:
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; by default the logs are forwarded to a Redis instance running at localhost:6379
in a stream named logs
.
Use a different stream name
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:
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:
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):
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:
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.
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
Built Distribution
Hashes for redis_logs-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8325b6385501b12d5202f38f40645b34279b7eb243d0e94a4036b6a09007cd5 |
|
MD5 | f017808ed82f5d855ea99f2dd6314364 |
|
BLAKE2b-256 | e56ea90b7834c7593ee79c2fa1d0bf5e0ff5ba2a820510b244522d21bd359c70 |