-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.py
55 lines (46 loc) · 1.82 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
# Simple logging class.
# This module is designed to make a centralized location for all logs. It will
# be stored in the main bot object and can be accessed by different modules to
# log data in persistent manner.
#class LogStruct:
# def __init__(fi
# TODO
# - Make file opening more resilient.
# - Fix args for write(...) method
import os
from datetime import datetime
# All filepaths are relative to this folder
LOG_DIRECTORY = "logs/"
class Logger:
def __init__(self, mainLogfile):
self.logfiles = {}
self.logfiles["main"] = open("logs/" + mainLogfile, 'a')
def addLogfile(self, name, filepath):
self.logfiles[name] = open("logs/" + filepath, 'a')
def timestamp(self):
return datetime.today().strftime("%Y-%m-%d %H:%M:%S\t")
# Forces the logger to flush the stream and write the log to disk
def writeToFile(self, log = None):
if log:
self.logfiles[log].flush()
os.fsync(self.logfiles[log])
else:
for logfile in self.logfiles:
self.logfiles[logfile].flush()
os.fsync(self.logfiles[logfile])
# Closes a file and removes it from the list
def removeLogfile(self, name):
self.logfiles[log].close()
del self.logfiles[log]
# Writes a line to the specified log (by default "main")
# ensureWrite specifies whether to automatically call writeToFile after
# writing the line, thus ensuring the changes are saved.
def write(self, message, log="main", ensureWrite=True, echo=True, trailingNewline=True):
message = message.strip()
if trailingNewline:
message += "\n"
if echo:
print message,
self.logfiles[log].write("%s\t%s" % (self.timestamp(), message))
if ensureWrite:
self.writeToFile(log)