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