Package flumotion :: Package component :: Package plugs :: Module loggers
[hide private]

Source Code for Module flumotion.component.plugs.loggers

 1  # -*- Mode: Python -*- 
 2  # vi:si:et:sw=4:sts=4:ts=4 
 3  # 
 4  # Flumotion - a streaming media server 
 5  # Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com). 
 6  # All rights reserved. 
 7   
 8  # This file may be distributed and/or modified under the terms of 
 9  # the GNU General Public License version 2 as published by 
10  # the Free Software Foundation. 
11  # This file is distributed without any warranty; without even the implied 
12  # warranty of merchantability or fitness for a particular purpose. 
13  # See "LICENSE.GPL" in the source distribution for more information. 
14   
15  # Licensees having purchased or holding a valid Flumotion Advanced 
16  # Streaming Server license may use this file in accordance with the 
17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
18  # See "LICENSE.Flumotion" in the source distribution for more information. 
19   
20  # Headers in this file shall remain intact. 
21   
22  import time 
23   
24  from flumotion.common import errors 
25  from flumotion.component.plugs import base 
26   
27   
28 -class Logger(base.ComponentPlug):
29 """ 30 Base class for logger implementations. Should be renamed to 31 StreamLogger later... 32 """
33 - def event(self, type, args):
34 handler = getattr(self, 'event_' + type, None) 35 if handler: 36 handler(args)
37
38 - def rotate(self):
39 # do nothing by default 40 pass
41
42 -def _http_session_completed_to_apache_log(args):
43 # ident is something that should in theory come from identd but in 44 # practice is never there 45 ident = '-' 46 date = time.strftime('%d/%b/%Y:%H:%M:%S +0000', args['time']) 47 48 return ("%s %s %s [%s] \"%s %s %s\" %d %d %s \"%s\" %d\n" 49 % (args['ip'], ident, args['username'], date, 50 args['method'], args['uri'], args['clientproto'], 51 args['response'], args['bytes-sent'], args['referer'], 52 args['user-agent'], args['time-connected']))
53
54 -class ApacheLogger(Logger):
55 filename = None 56 file = None 57
58 - def start(self, component=None):
59 self.filename = self.args['properties']['logfile'] 60 try: 61 self.file = open(self.filename, 'a') 62 except IOError, data: 63 raise errors.PropertiesError('could not open log file %s ' 64 'for writing (%s)' 65 % (self.filename, data[1]))
66
67 - def stop(self, component=None):
68 if self.file: 69 self.file.close() 70 self.file = None
71
72 - def event_http_session_completed(self, args):
73 self.file.write(_http_session_completed_to_apache_log(args)) 74 self.file.flush()
75
76 - def rotate(self):
77 self.stop() 78 self.start()
79