-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.py
74 lines (69 loc) · 2.39 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
import collections
import subprocess
import time
import os
import errno
import threading
class Logger(object):
LoggingDir = ""
fd = None
is_started = False
@staticmethod
def Start():
try :
Logger.is_started = True
if (Logger.fd is None):
print "Test Logging started"
currentTime = time.strftime("%Y%m%d-%H%M%S")
if Logger.LoggingDir == "":
Logger.LoggingDir = "Logs_"+currentTime+"/"
file_name = Logger.LoggingDir + "TestLog.log"
if not os.path.exists(os.path.dirname(file_name)):
try:
os.makedirs(os.path.dirname(file_name))
except OSError as exc: # guard against race condition
if exc.errno != errno.EEXIST:
raise
Logger.fd = open(file_name,"a+")
except Exception as e:
print "Exception when starting logger : " , e
@staticmethod
def IsStarted():
return Logger.is_started
@staticmethod
def WriteToLog(level,message):
lock = threading.Lock()
lock.acquire()
try :
Logger.fd.write(time.strftime("%Y-%m-%d-%H:%M:%S ")+ "----- "+ level.upper() +" -----" + message +"\n")
except Exception as e:
print "Failure writing to file : ", message, "Exception : ", e
finally:
lock.release()
@staticmethod
def error(message):
try :
Logger.WriteToLog("error", message)
except Exception as e:
print "Failure writing to file : ", message, "Exception : ", e
@staticmethod
def info(message):
try :
Logger.WriteToLog("info", message)
except Exception as e:
print "Failure writing to file : ", message, "Exception : ", e
@staticmethod
def warning(message):
try :
Logger.WriteToLog("warning", message)
except Exception as e:
print "Failure writing to file : ", message, "Exception : ", e
@staticmethod
def Stop():
#print "Stopping logs"
if (Logger.fd is not None):
Logger.fd.close()
Logger.fd = None
else:
print "Logger.fd stopped already"