
Security News
Vite Releases Technical Preview of Rolldown-Vite, a Rust-Based Bundler
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
Log handler to forward logs to Redis.
Installation | Usage | Handler classes | Documentation |
---|
Installation with pip
:
pip install redis-logs
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
.
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)
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)
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)
LogRecord
as pickle formatLogs 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 as_pkl set to True
handler = RedisStreamLogHandler(as_pkl=True)
# add the handler to the logger
logger.addHandler(handler)
LogRecord
as pickle formatLogs can also be saved in DB as their JSON representation:
from rlh import RedisStreamLogHandler
# define your logger
logger = logging.getLogger('my_app')
# define the Redis log handler with ad_json set to True
handler = RedisStreamLogHandler(as_json=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.
Currently rlh
implements two classes of handlers:
RedisStreamLogHandler
Handler used to forward logs to a Redis stream.
RedisPubSubLogHandler
Handler used to publish logs to a Redis pub/sub channel.
:warning: Before using
RedisPubSubLogHandler
, make sure to define at least one listener to the channel, otherwise the logs emitted will be lost
FAQs
Python log handler to forward logs to Redis database
We found that redis-logs demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
Research
Security News
A malicious npm typosquat uses remote commands to silently delete entire project directories after a single mistyped install.
Research
Security News
Malicious PyPI package semantic-types steals Solana private keys via transitive dependency installs using monkey patching and blockchain exfiltration.