2
2
import redis
3
3
import simplejson as json
4
4
5
+
5
6
class RedisFormatter (logging .Formatter ):
6
7
def format (self , record ):
7
8
"""
@@ -17,20 +18,21 @@ def format(self, record):
17
18
# stringify exception data
18
19
if data .get ('traceback' ):
19
20
data ['traceback' ] = self .formatException (data ['traceback' ])
20
-
21
+
21
22
return json .dumps (data )
22
23
24
+
23
25
class RedisHandler (logging .Handler ):
24
26
"""
25
27
Publish messages to redis channel.
26
28
27
- As a convenience, the classmethod to() can be used as a
29
+ As a convenience, the classmethod to() can be used as a
28
30
constructor, just as in Andrei Savu's mongodb-log handler.
29
31
"""
30
32
31
33
@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 )
34
36
35
37
def __init__ (self , channel , redis_client , level = logging .NOTSET ):
36
38
"""
@@ -45,11 +47,12 @@ def emit(self, record):
45
47
"""
46
48
Publish record to redis logging channel
47
49
"""
48
- try :
50
+ try :
49
51
self .redis_client .publish (self .channel , self .format (record ))
50
52
except redis .RedisError :
51
53
pass
52
-
54
+
55
+
53
56
class RedisListHandler (logging .Handler ):
54
57
"""
55
58
Publish messages to redis a redis list.
@@ -78,13 +81,13 @@ def emit(self, record):
78
81
"""
79
82
Publish record to redis logging list
80
83
"""
81
- try :
84
+ try :
82
85
if self .max_messages :
83
86
p = self .redis_client .pipeline ()
84
87
p .rpush (self .key , self .format (record ))
85
88
p .ltrim (self .key , - self .max_messages , - 1 )
86
89
p .execute ()
87
90
else :
88
- self .redis_client .rpush (self .key ,self .format (record ))
91
+ self .redis_client .rpush (self .key , self .format (record ))
89
92
except redis .RedisError :
90
93
pass
0 commit comments