forked from Tratos/BFBC2_MasterServer
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLogger.py
51 lines (35 loc) · 1.5 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
from colorama import init
from time import gmtime, strftime
from os import remove
from Config import readFromConfig
class Log(object):
def __init__(self, messageFrom, messageColor):
init()
self.messageFrom = messageFrom
self.messageColor = messageColor
self.logFile = readFromConfig("debug", "create_log")
self.logTimestamp = readFromConfig("debug", "put_timestamp_in_log")
self.fileLogLevel = int(readFromConfig("debug", "file_log_level"))
self.consoleLogLevel = int(readFromConfig("debug", "console_log_level"))
self.useColors = readFromConfig("console", "use_colors")
def new_message(self, message, level=0):
timestamp = str(strftime("%H:%M:%S", gmtime()))
if self.logFile:
if level <= self.fileLogLevel:
saveToLog = ""
if self.logTimestamp:
saveToLog += "[" + timestamp + "]"
saveToLog += "[" + self.messageFrom + "]"
saveToLog += " " + message + "\n"
with open("server.log", "a") as logfile:
logfile.write(saveToLog)
if level <= self.consoleLogLevel:
consoleMessage = "[" + timestamp + "]" + "[" + self.messageFrom + "] " + message
if self.useColors:
consoleMessage = self.messageColor + consoleMessage + "\33[0m"
print(consoleMessage)
def clean_log(self):
try:
remove("server.log")
except:
pass