Public » MiniRedis Read More
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

allow logging to file

Changeset c8799f11def4

Parent 059b497723c8

by Profile picture of Benjamin PollackBenjamin Pollack

Changes to one file · Browse files at c8799f11def4 Showing diff from parent 059b497723c8 Diff from another changeset...

Change 1 of 3 Show Entire File miniredis.py Stacked
 
45
46
47
48
 
 
 
 
 
 
49
50
51
 
86
87
88
89
90
91
92
93
94
95
 
 
 
 
 
 
96
97
98
 
275
276
277
278
 
279
280
281
282
283
284
285
 
286
287
288
 
45
46
47
 
48
49
50
51
52
53
54
55
56
 
91
92
93
 
 
 
 
 
 
 
94
95
96
97
98
99
100
101
102
 
279
280
281
 
282
283
284
285
286
287
288
 
289
290
291
292
@@ -45,7 +45,12 @@
  super(MiniRedis, self).__init__()   self.host = host   self.port = port - self.log_file = log_file + if log_file: + self.log_name = log_file + self.log_file = open(self.log_name, 'w') + else: + self.log_name = None + self.log_file = sys.stdout   self.halt = True     self.clients = {} @@ -86,13 +91,12 @@
  self.log(None, 'loaded database from file "%s"' % self.db_file)     def log(self, client, s): - if self.log_file: - try: - who = '%s:%s' % client.socket.getpeername() if client else 'SERVER' - except: - who = '<CLOSED>' - self.log_file.write('%s - %s: %s\n' % (datetime.datetime.now(), who, s)) - self.log_file.flush() + try: + who = '%s:%s' % client.socket.getpeername() if client else 'SERVER' + except: + who = '<CLOSED>' + self.log_file.write('%s - %s: %s\n' % (datetime.datetime.now(), who, s)) + self.log_file.flush()     def handle(self, client):   line = client.rfile.readline() @@ -275,14 +279,14 @@
   def main(args):   host, port, log_file, db_file = '127.0.0.1', 6379, None, None - opts, args = getopt.getopt(args, 'h:p:d:l') + opts, args = getopt.getopt(args, 'h:p:d:l:')   for o, a in opts:   if o == '-h':   host = a   elif o == '-p':   port = int(a)   elif o == '-l': - log_file = sys.stdout + log_file = os.path.abspath(a)   elif o == '-d':   db_file = os.path.abspath(a)   m = MiniRedis(host=host, port=port, log_file=log_file, db_file=db_file)