Skip to content

Commit 3fc81a4

Browse files
committed
Merge pull request jedp#4 from profitsee/master
Support for Redis passwords,
2 parents 80dbc82 + 7550cdd commit 3fc81a4

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Usage
4444

4545
>>> from redislog import handlers, logger
4646
>>> l = logger.RedisLogger('my.logger')
47-
>>> l.addHandler(handlers.RedisHandler.to("my:channel"))
47+
>>> l.addHandler(handlers.RedisHandler.to("my:channel", host='localhost', port=6379, password='foobie'))
4848
>>> l.info("I like pie")
4949
>>> l.error("Trousers!", exc_info=True)
5050

redislog/handlers.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import redis
33
import simplejson as json
44

5+
56
class RedisFormatter(logging.Formatter):
67
def format(self, record):
78
"""
@@ -17,20 +18,21 @@ def format(self, record):
1718
# stringify exception data
1819
if data.get('traceback'):
1920
data['traceback'] = self.formatException(data['traceback'])
20-
21+
2122
return json.dumps(data)
2223

24+
2325
class RedisHandler(logging.Handler):
2426
"""
2527
Publish messages to redis channel.
2628
27-
As a convenience, the classmethod to() can be used as a
29+
As a convenience, the classmethod to() can be used as a
2830
constructor, just as in Andrei Savu's mongodb-log handler.
2931
"""
3032

3133
@classmethod
32-
def to(cklass, channel, host='localhost', port=6379, level=logging.NOTSET):
33-
return cklass(channel, redis.Redis(host=host, port=port), level=level)
34+
def to(cklass, channel, host='localhost', port=6379, password=None, level=logging.NOTSET):
35+
return cklass(channel, redis.Redis(host=host, port=port, password=password), level=level)
3436

3537
def __init__(self, channel, redis_client, level=logging.NOTSET):
3638
"""
@@ -45,11 +47,12 @@ def emit(self, record):
4547
"""
4648
Publish record to redis logging channel
4749
"""
48-
try :
50+
try:
4951
self.redis_client.publish(self.channel, self.format(record))
5052
except redis.RedisError:
5153
pass
52-
54+
55+
5356
class RedisListHandler(logging.Handler):
5457
"""
5558
Publish messages to redis a redis list.
@@ -78,13 +81,13 @@ def emit(self, record):
7881
"""
7982
Publish record to redis logging list
8083
"""
81-
try :
84+
try:
8285
if self.max_messages:
8386
p = self.redis_client.pipeline()
8487
p.rpush(self.key, self.format(record))
8588
p.ltrim(self.key, -self.max_messages, -1)
8689
p.execute()
8790
else:
88-
self.redis_client.rpush(self.key,self.format(record))
91+
self.redis_client.rpush(self.key, self.format(record))
8992
except redis.RedisError:
9093
pass

0 commit comments

Comments
 (0)