-
Notifications
You must be signed in to change notification settings - Fork 108
/
mylog.py
93 lines (70 loc) · 3.31 KB
/
mylog.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/python3
import os, sys, time, json
import logging, logging.handlers
#---------- SetupLogger --------------------------------------------------------
def SetupLogger(logger_name, log_file, level=logging.DEBUG, stream = False):
logger = logging.getLogger(logger_name)
# remove existing logg handlers
for handler in logger.handlers[:]: # make a copy of the list
logger.removeHandler(handler)
logger.setLevel(level)
formatter = logging.Formatter('%(asctime)s : [%(levelname)s] (%(threadName)-10s) %(message)s')
if log_file != "":
rotate = logging.handlers.RotatingFileHandler(log_file, mode='a',maxBytes=50000,backupCount=5)
rotate.setFormatter(formatter)
logger.addHandler(rotate)
if stream: # print to screen also?
streamHandler = logging.StreamHandler()
# Dont format stream log messages
logger.addHandler(streamHandler)
return logging.getLogger(logger_name)
#------------ MyLog class -----------------------------------------------------
class MyLog(object):
def __init__(self):
self.log = None
self.console = None
pass
#--------------------------------------------------------------------------
def LogDebug(self, Message, LogLine = False):
if ((not LogLine) and (not self.log == None)):
self.log.debug(Message)
elif (not self.log == None):
self.log.debug(Message + " : " + self.GetErrorLine())
# self.LogConsole(Message)
#--------------------------------------------------------------------------
def LogInfo(self, Message, LogLine = False):
if ((not LogLine) and (not self.log == None)):
self.log.info(Message)
elif (not self.log == None):
self.log.info(Message + " : " + self.GetErrorLine())
# self.LogConsole(Message)
#---------------------------------------------------------------------------
def LogWarn(self, Message, LogLine = False):
if ((not LogLine) and (not self.log == None)):
self.log.warn(Message)
elif (not self.log == None):
self.log.warn(Message + " : " + self.GetErrorLine())
# self.LogConsole(Message)
#---------------------MyLog::LogConsole------------------------------------
def LogConsole(self, Message):
if not self.console == None:
self.console.error(Message)
#---------------------MyLog::LogError------------------------------------
def LogError(self, Message):
if (not self.log == None):
self.log.error(Message)
#---------------------MyLog::FatalError----------------------------------
def FatalError(self, Message):
if not self.log == None:
self.log.critical("FATAL: " + Message)
raise Exception(Message)
#---------------------MyLog::LogErrorLine--------------------------------
def LogErrorLine(self, Message):
if (not self.log == None):
self.log.error(Message + " : " + self.GetErrorLine())
#---------------------MyLog::GetErrorLine--------------------------------
def GetErrorLine(self):
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
lineno = exc_tb.tb_lineno
return fname + ":" + str(lineno)