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 """ 35 Handle a log event. 36 37 This dispatches a particular event type such as "http_session_completed" 38 to a method "event_http_session_completed". 39 40 Returns a Deferred (which will fire once the event handling has been 41 completed), or None. 42 """ 43 handler = getattr(self, 'event_' + type, None) 44 if handler: 45 return handler(args)
46
47 - def rotate(self):
48 # do nothing by default 49 pass
50
51 -def _http_session_completed_to_apache_log(args):
52 # ident is something that should in theory come from identd but in 53 # practice is never there 54 ident = '-' 55 date = time.strftime('%d/%b/%Y:%H:%M:%S +0000', args['time']) 56 57 return ("%s %s %s [%s] \"%s %s %s\" %d %d %s \"%s\" %d\n" 58 % (args['ip'], ident, args['username'], date, 59 args['method'], args['uri'], args['clientproto'], 60 args['response'], args['bytes-sent'], args['referer'], 61 args['user-agent'], args['time-connected']))
62
63 -class ApacheLogger(Logger):
64 filename = None 65 file = None 66
67 - def start(self, component=None):
68 self.filename = self.args['properties']['logfile'] 69 try: 70 self.file = open(self.filename, 'a') 71 except IOError, data: 72 raise errors.PropertiesError('could not open log file %s ' 73 'for writing (%s)' 74 % (self.filename, data[1]))
75
76 - def stop(self, component=None):
77 if self.file: 78 self.file.close() 79 self.file = None
80
81 - def event_http_session_completed(self, args):
82 self.file.write(_http_session_completed_to_apache_log(args)) 83 self.file.flush()
84
85 - def rotate(self):
86 self.stop() 87 self.start()
88