-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogginghelper.py
More file actions
53 lines (43 loc) · 1.34 KB
/
logginghelper.py
File metadata and controls
53 lines (43 loc) · 1.34 KB
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
import sys
import time
try:
import logging
import logging.handlers
import logging.config
if not logging.__author__ == 'Vinay Sajip <vinay_sajip@red-dove.com>':
raise AttributeError
except AttributeError, e:
print >> sys.stderr, 'Apparently wrong logging module installed'
exit()
except ImportError:
print >> sys.stderr, 'logging modul not found!'
exit()
class alreadySent(logging.Filter):
def __init__(self):
self.lastErrorTime = 0
self.lastErrorMessage = ""
self.ErrorInterval = 3600 #1 hour
def filter(self, rec):
if rec.message == self.lastErrorMessage:
if rec.created >= self.lastErrorTime + self.ErrorInterval:
self.lastErrorTime = rec.created
return True
else:
return False
else:
self.lastErrorMessage = rec.message
self.lastErrorTime = rec.created
return True
class SMTPHandlerlimited(logging.handlers.SMTPHandler):
def __init__(self, *args, **kwargs):
logging.handlers.SMTPHandler.__init__(self, *args, **kwargs)
self.addFilter(alreadySent())
logging.config.fileConfig("config.cfg")
tweetlog = logging.getLogger('tweetlogger')
hpgllog = logging.getLogger("hpgllogger")
def tweet(tweetobject):
tweetlog.info(tweetobject.text.replace("\n", ""), extra={'username': tweetobject.user.screen_name, 'id': tweetobject.id})
def hpgl(text):
hpgllog.info(text)
logging.tweet = tweet
logging.hpgl = hpgl