/
logSys.py
163 lines (141 loc) · 4.36 KB
/
logSys.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import logging
import logging.config
import inspect
import ConfigParser
try:
logging.config.fileConfig("logging.conf")
logFile = "logging.conf"
except ConfigParser.NoSectionError:
logging.config.fileConfig("d:/tagfs/logging.conf")
logFile = "d:/tagfs/logging.conf"
simpleLogConfigCache = {}#Used to init a logger if there is no logger setting in the config file
logSys = logging
def openLogConfig(fileName):
try:
config = ConfigParser.RawConfigParser()
config.read(fileName)
except:
pass
return config
def loadLogConofig(fileName):
try:
config = openLogConfig(fileName)
loggerNames = getLoggerNames(config)
for i in loggerNames:
simpleLogConfigCache[i] = True
except:
pass
def getLoggerNames(config):
try:
keys = config.get('loggers', 'keys')
loggerNames = keys.split(',')
return loggerNames
except:
pass
def updateLogConfig(fileName, loggerName):
try:
config = openLogConfig(fileName)
config.set('loggers', 'keys', config.get('loggers', 'keys')+','+loggerName)
print 'new logger names:',config.get('loggers', 'keys')
config.add_section('logger_'+loggerName)
config.set('logger_'+loggerName, 'handlers', 'consoleHandler')
config.set('logger_'+loggerName, 'propagate', '0')
config.set('logger_'+loggerName, 'level', 'ERROR')
config.set('logger_'+loggerName, 'qualname', loggerName)
configfile = open(fileName, 'wb')
config.write(configfile)
except:
pass
loadLogConofig(logFile)
def printLog(*args):
logStr = ''
for i in args:
logStr += str(i)
logger.debug(logStr)
def pL(*args):
logStr = ''
for i in args[1:]:
logStr += str(i)
args[0].debug(logStr)
def smL(*args):
logStr = ''
for i in args[1:]:
logStr += str(i)
smartProxyLogger.error(logStr)
def whosdaddy():
try:
return inspect.stack()[2][3]
except:
return 'unknown'
def changeEncoding(s):
if type(s) == unicode:
return s.encode('utf8')
if type(s) == str:
return s
return str(s)
def cl(*args):
p = whosdaddy()
#print 'dady is',p
realLogger = logging.getLogger(p)
#print realLogger.findCaller()
if not simpleLogConfigCache.has_key(p):
#The logger name does not exist in the config file
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
#create formatter
formatter = logging.Formatter("%(message)s")
#add formatter to ch and fh
ch.setFormatter(formatter)
#add ch to logger
realLogger.addHandler(ch)
realLogger.propagate = False
simpleLogConfigCache[p] = True
updateLogConfig(logFile, p)
logStr = ''
for i in args:
logStr += changeEncoding(i)
#print 'calling debug'
realLogger.error(logStr)
def ncl(*args):
p = whosdaddy()
#print 'dady is',p
realLogger = logging.getLogger(p)
#print realLogger.findCaller()
if not simpleLogConfigCache.has_key(p):
#The logger name does not exist in the config file
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
#create formatter
formatter = logging.Formatter("%(message)s")
#add formatter to ch and fh
ch.setFormatter(formatter)
#add ch to logger
realLogger.addHandler(ch)
realLogger.propagate = False
simpleLogConfigCache[p] = True
updateLogConfig(logFile, p)
logStr = ''
for i in args:
logStr += str(i)
#print 'calling debug'
realLogger.debug(logStr+str(realLogger.findCaller()))
def testFunc():
cl('hello world')
ncl('hello world')
'''
Use case:
1. add a log simply:
cl('a','b','c')
2. remove a log simply.
ncl('a','b','c')
Requirement:
1. log system shall has a config file.
2. log system shall output the function name of the log.
3. log system shall output log when requested.
4. log system shall support turn off single log in every log output.
5. log system shall output logs when it is not turned off
6. log system shall support to record the turn off operation for a log command.
'''
if __name__ == "__main__":
#print loadLogConofig("logging.conf")
testFunc()