forked from theyosh/TerrariumPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterrariumLogging.py
83 lines (65 loc) · 2.69 KB
/
terrariumLogging.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
import logging
import logging.handlers
import logging.config
import zipfile
import codecs
import sys
import os
import os.path
import time
import glob
import shutil
from terrariumConfig import terrariumConfig
from terrariumNotification import terrariumNotification
class TimedCompressedRotatingFileHandler(logging.handlers.TimedRotatingFileHandler):
"""
Extended version of TimedRotatingFileHandler that compress logs on rollover.
"""
def doRollover(self):
"""
do a rollover; in this case, a date/time stamp is appended to the filename
when the rollover happens. However, you want the file to be named for the
start of the interval, not the current time. If there is a backup count,
then we have to get a list of matching filenames, sort them and remove
the one with the oldest suffix.
"""
# get the time that this sequence started at and make it a TimeTuple
t = self.rolloverAt - self.interval
timeTuple = time.localtime(t)
dfn = self.baseFilename + '.' + time.strftime(self.suffix, timeTuple)
if os.path.exists(dfn):
os.remove(dfn)
self.stream.close()
shutil.copyfile(os.path.realpath(self.baseFilename), os.path.abspath(dfn))
# Empty source file for new day
open(self.baseFilename, 'w').close()
if self.encoding:
self.stream = codecs.open(self.baseFilename, 'w', self.encoding)
else:
self.stream = open(self.baseFilename, 'w')
self.rolloverAt = self.rolloverAt + self.interval
if self.backupCount > 0:
# find the oldest log file and delete it
s = glob.glob(self.baseFilename + '.20*')
if len(s) > self.backupCount:
s.sort()
os.remove(s[0])
if os.path.exists(dfn + '.zip'):
os.remove(dfn + '.zip')
log_archive = zipfile.ZipFile(dfn + '.zip', 'w')
log_archive.write(dfn, os.path.basename(dfn), zipfile.ZIP_DEFLATED)
log_archive.close()
os.remove(dfn)
class NotificationLogger(logging.StreamHandler):
def __init__(self,trafficlights,*args, **kwargs):
super(NotificationLogger,self).__init__(*args, **kwargs)
terrariumpi_config = terrariumConfig()
self.notification = terrariumNotification(trafficlights,terrariumpi_config.get_profile_image())
def emit(self,data):
if data.name not in ['terrariumTranslations']:
self.notification.message('system_' + str(data.levelname).lower() , {'message':data.getMessage()} )
if os.path.isfile('logging.custom.cfg'):
logging.config.fileConfig('logging.custom.cfg')
else:
logging.config.fileConfig('logging.cfg')