-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.py
32 lines (28 loc) · 1.13 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
import machine
import uos as os
import TimeUtils
class Logger():
def __init__(self, hk_days):
self.hk_days = hk_days
self.TimeUtils = TimeUtils.TimeUtils()
if not "logs" in os.listdir():
os.mkdir("/logs")
def LogMessage(self, message):
print(message)
dt = machine.RTC().datetime()
file = open(("/logs/%04d-%02d-%02d.txt" % (dt[0], dt[1], dt[2])), "a")
file.write(self.TimeUtils.DateTimeNow() + ";" + message + "\n")
file.close()
def Housekeeping(self):
for file in os.listdir("/logs"):
if (file.endswith(".txt")):
fd = file.split('.')[0].split('-')
if (self.TimeUtils.IsOlderThanDays(fd, self.hk_days)):
os.remove("/logs/" + file)
self.LogMessage("Housekeeping: deleted logfile " + file)
def LastLogs(self):
dt = machine.RTC().datetime()
file = open(("/logs/%04d-%02d-%02d.txt" % (dt[0], dt[1], dt[2])), "r")
lines = file.readlines()
file.close()
return "<br>".join(lines[-10:])