-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
62 lines (49 loc) · 1.86 KB
/
logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import ConfigParser
import os, sys
import copy
from events import EventListener
class Logger(EventListener):
defaults = {
'write' : 'stdout',
'path' : '.',
}
def __init__(self, dispatcher = None):
print "logger"
self.events = {}
self.formats = {}
self.files = {
'stdout' : sys.stdout,
'stderr' : sys.stderr,
}
self.read_config()
if dispatcher: EventListener.__init__(self, dispatcher)
def register_mapping(self):
for event in self.events.keys():
self.dispatcher.listen(self, event)
print "Logger register", event
def __call__(self, event):
if event.command in self.events:
for file in self.events[ event.command ][ 'write' ]:
file = self.files[file]
print >>file, self.format(event)
def read_config(self):
p = ConfigParser.RawConfigParser( self.defaults )
p.read( 'log.conf' )
for section in p.sections():
if section[0]=='@':
if len(section) > 1 and p.has_option(section, 'filename'):
path = os.path.realpath( p.get(section, 'path') )
filename = path + os.sep + p.get( section, 'filename' )
self.files[ section[1:] ] = open( filename , 'a')
else:
opts = copy.copy( self.defaults )
opts.update( p.items( section ) )
self.formats[section] = opts['format']
del opts['format']
del opts['path']
opts['write'] = [ item.strip() for item in opts['write'].split(',') ]
self.events[ section ] = opts
def format(self, event):
return self.formats[ event.command ] % event.__dict__
def load_module(irc):
l = Logger( irc.dispatcher )