.BANCHO. Add logHelper

This commit is contained in:
Nyo 2016-06-02 21:50:10 +02:00
parent 611216fea2
commit 3100853903
4 changed files with 53 additions and 2 deletions

View File

@ -6,7 +6,7 @@ from objects import glob
from objects import fokabot
from constants import exceptions
from constants import messageTemplates
from time import gmtime, strftime
from helpers import generalFunctions
def handle(userToken, packetData):
"""
@ -51,7 +51,7 @@ def handle(userToken, packetData):
# Log to file
with open(".data/chatlog_private.txt", "a") as f:
f.write("[{date}] {fro} -> {to}: {message}\n".format(date=strftime("%Y-%m-%d %H:%M:%S", gmtime()), fro=username, to=packetData["to"], message=str(packetData["message"].encode("utf-8"))[2:-1]))
f.write("[{date}] {fro} -> {to}: {message}\n".format(date=generalFunctions.getTimestamp(), fro=username, to=packetData["to"], message=str(packetData["message"].encode("utf-8"))[2:-1]))
except exceptions.tokenNotFoundException:
# Token not found, user disconnected
consoleHelper.printColored("[!] {} tried to send a message to {}, but their token couldn't be found".format(username, packetData["to"]), bcolors.RED)

View File

@ -1,5 +1,6 @@
"""Some functions that don't fit in any other file"""
from constants import mods
from time import gmtime, strftime
def stringToBool(s):
"""
@ -55,3 +56,10 @@ def readableMods(__mods):
def strContains(s, w):
return (' ' + w + ' ') in (' ' + s + ' ')
def getTimestamp():
"""
Return current time in YYYY-MM-DD HH:MM:SS format.
Used in logs.
"""
return strftime("%Y-%m-%d %H:%M:%S", gmtime())

41
helpers/logHelper.py Normal file
View File

@ -0,0 +1,41 @@
from constants import bcolors
from helpers import discordBotHelper
from helpers import generalFunctions
def logMessage(message, alertType, messageColor, discord = False, alertDev = False, of = None):
if alertType == "INFO":
typeColor = bcolors.GREEN
elif alertType == "WARNING":
typeColor = bcolors.YELLOW
elif typeColor == "ERROR":
typeColor = bcolors.RED
else:
typeColor = bcolors.ENDC
finalMessage = "[{time}] {type} - {message}".format(time=generalFunctions.getTimestamp(), type=alertType, message=message)
finalMessageConsole = "{typeColor}[{time}] {type}{endc} - {messageColor}{message}{endc}".format(
time=generalFunctions.getTimestamp(),
type=alertType,
message=message,
typeColor=typeColor,
messageColor=messageColor,
endc=bcolors.ENDC)
# Always log to console
print(finalMessageConsole)
# Log to discord if needed
if discord == True:
discordBotHelper.sendConfidential(message, alertDev)
# Log to file if needed
if of != None:
with open(".data/{}".format(of), "a") as f:
f.write(finalMessage)
def warning(message, discord = False, alertDev = False):
logMessage(message, "WARNING", bcolors.YELLOW, discord, alertDev, "warnings.txt")
def error(message, discord = False, alertDev = True):
logMessage(message, "ERROR", bcolors.RED, discord, alertDev, "errors.txt")

2
pep.py
View File

@ -190,6 +190,8 @@ if __name__ == "__main__":
# Server start message and console output
discordBotHelper.sendConfidential("Server started!")
from helpers import logHelper
logHelper.warning("Test warning")
consoleHelper.printColored("> Tornado listening for clients on 127.0.0.1:{}...".format(serverPort), bcolors.GREEN)
# Start tornado