Files
CyberPanel/plogical/CyberCPLogFileWriter.py

56 lines
1.6 KiB
Python
Raw Normal View History

2017-10-24 19:16:36 +05:00
import subprocess
import time
2018-08-28 23:11:33 +05:00
2017-10-24 19:16:36 +05:00
class CyberCPLogFileWriter:
2017-12-09 22:30:10 +05:00
fileName = "/home/cyberpanel/error-logs.txt"
2017-10-24 19:16:36 +05:00
@staticmethod
def writeToFile(message):
try:
file = open(CyberCPLogFileWriter.fileName,'a')
file.writelines("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "] "+ message + "\n")
file.close()
2018-08-21 13:10:40 +05:00
2017-10-24 19:16:36 +05:00
except IOError,msg:
return "Can not write to error file."
2018-06-01 02:08:21 +05:00
@staticmethod
def writeforCLI(message, level, method):
try:
file = open(CyberCPLogFileWriter.fileName, 'a')
file.writelines("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "] [" + level + ":" + method + "] " + message + "\n")
file.close()
file.close()
except IOError:
return "Can not write to error file!"
2017-10-24 19:16:36 +05:00
@staticmethod
def readLastNFiles(numberOfLines,fileName):
try:
2019-03-26 16:19:03 +05:00
lastFewLines = subprocess.check_output(["tail", "-n",str(numberOfLines),fileName])
2017-10-24 19:16:36 +05:00
return lastFewLines
except subprocess.CalledProcessError,msg:
return "File was empty"
2018-08-22 00:37:43 +05:00
@staticmethod
2018-11-10 16:05:40 +05:00
def statusWriter(tempStatusPath, mesg, append = None):
2018-08-22 00:37:43 +05:00
try:
2018-11-10 16:05:40 +05:00
if append == None:
statusFile = open(tempStatusPath, 'w')
else:
statusFile = open(tempStatusPath, 'a')
2019-07-03 13:15:26 +05:00
statusFile.writelines(mesg + '\n')
2018-08-22 00:37:43 +05:00
statusFile.close()
2019-07-03 13:15:26 +05:00
print(mesg + '\n')
2018-08-22 00:37:43 +05:00
except BaseException, msg:
2019-10-06 18:41:09 +05:00
CyberCPLogFileWriter.writeToFile(str(msg) + ' [statusWriter]')
2019-07-24 22:37:37 +05:00
#print str(msg)
2018-08-22 00:37:43 +05:00