hch
2018-12-24 c238e45c6695d627bcc776b34f8ff46823c57e71
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
164
165
166
167
168
169
170
171
172
173
174
#-*- coding: GBK -*-
#LOG¶¨ÒåÄ£¿é
import logging
from time import localtime
import traceback
 
__console = None
 
#±¨´íÓõ¯¿ò
def ErrorBox(title,msg):
    from CommFuncEx import (MessageBox, MB_ICONERROR, MB_TOPMOST)
    MessageBox("MongoDBServer:%s"%title.upper(),msg,MB_ICONERROR|MB_TOPMOST)
    
 
 
def InitMyLog( LogName, isDebug ):
    global __console
    # set up logging to file - see previous section for more details
    now = localtime()
    file = "%s-%d-%d-%d.log" % ( LogName, now.tm_year, now.tm_mon, now.tm_mday )
    openMode = 'a'
    logLevel = logging.INFO
    if isDebug:
        openMode = 'w'
        logLevel = logging.DEBUG
    logging.basicConfig(level=logLevel,
#                        format='%(name)-12s %(asctime)s %(name)-12s %(levelname)-8s %(message)s',
#                        format='%(name)-12s %(asctime)s %(levelname)-8s %(threadName)s %(module)s %(funcName)s %(lineno)d %(message)s',
                        format='%(levelname)-8s %(asctime)s %(threadName)s %(module)s %(funcName)s %(lineno)d: %(message)s',
                        datefmt='%Y-%m-%dT%H:%M:%S',
                        filename=file,
                        filemode=openMode)
    #ÈÕ־ͳһ²»Ð´µ½¿ØÖÆÌ¨£¬¿ØÖÆÌ¨ÓÃÀ´ÏÔʾ״̬
    # define a Handler which writes INFO messages or higher to the sys.stderr
    __console = logging.StreamHandler()
    __console.setLevel(logging.INFO)
    # set a format which is simpler for console use
    formatter = logging.Formatter('%(name)-12s %(asctime)s %(name)-12s %(levelname)-8s %(message)s')
    # tell the handler to use this format
    __console.setFormatter(formatter)
    # add the handler to the root logger
    logging.getLogger('').addHandler(__console)
 
def removeConsoleHandler():
    __console.close()
    logging.getLogger('').removeHandler(__console)
    
#-----------------------------------------------------------------------------------------------------------------------------------------
#²»Ö±½Óµ÷ÓÃloggingÄ£¿éµÄº¯Êý£¬¶øµ÷ÓÃÒÔϺ¯ÊýÒÔ±ã½øÐгö´íͳ¼Æ
#-----------------------------------------------------------------------------------------------------------------------------------------
#CRITICAL = 50
#FATAL = CRITICAL
#ERROR = 40
#WARNING = 30
#WARN = WARNING
#INFO = 20
#DEBUG = 10
#NOTSET = 0
 
try:
    import threading
except ImportError:
    import dummy_threading as threading 
    
_fatalCntLock = threading.Lock()
_fatalCnt = 0
def fatal(msg, showError = True):
    global _fatalCntLock
    global _fatalCnt
    _fatalCntLock.acquire()
    _fatalCnt += 1
    _fatalCntLock.release()
    
#    logging.info(traceback.format_stack())
    logging.critical("\ncallstack:\n%smsg:%s"%("".join(traceback.format_stack()), msg))
    if showError:
        ErrorBox("fatal",msg)
        return
    
_errorCntLock = threading.Lock()
_errorCnt = 0
def error(msg, showError = False):
    global _errorCntLock
    global _errorCnt    
    _errorCntLock.acquire()
    _errorCnt += 1
    _errorCntLock.release()
    
    logging.error("\ncallstack:\n%smsg:%s"%("".join(traceback.format_stack()), msg))
    if showError:
        ErrorBox("error",msg)
        return
    
_designErrorCntLock = threading.Lock()
_designErrorCnt = 0
def DesignError(msg, showError = True):
    global _designErrorCnt
    global _designErrorCntLock    
    _designErrorCntLock.acquire()
    _designErrorCnt += 1
    _designErrorCntLock.release()
    
    logging.error("To Design:%s"%msg)
    #µ÷Ê԰浯¿òÌáʾ
    if showError:
        ErrorBox("Design Error",msg)
        return
    
_DeployErrorCntLock = threading.Lock()
_DeployErrorCnt = 0
def DeployError(msg, showError):
    global _DeployErrorCntLock
    global _DeployErrorCnt
    _DeployErrorCntLock.acquire()
    _DeployErrorCnt += 1
    _DeployErrorCntLock.release()
    
    logging.error("To Deployer:%s"%msg)
    if showError:
        ErrorBox("Deploy Error", msg)
    
_warningCntLock = threading.Lock()
_warningCnt = 0
def warning(msg):
    global _warningCntLock
    global _warningCnt
    _warningCntLock.acquire()
    _warningCnt += 1
    _warningCntLock.release()
    
    logging.warning(msg)
    
def info(msg):
    logging.info(msg)
 
def debug(msg):
    logging.debug(msg)
 
def getLogStaticDict():
    dict = {}
    dict['fatal'] = _fatalCnt
    dict['error'] = _errorCnt
    dict['designError'] = _designErrorCnt
    dict['warning'] = _warningCnt
    return dict    
 
def test_override_funcs():
    InitMyLog('test1.log')
#    info('test')
#    error('err')
 
def test():
    InitMyLog("test.log")
        # Now, we can log to the root logger, or any other logger. First the root...
    error('Jackdaws love my big sphinx of quartz.')
    logging.error('Jackdaws love my big sphinx of quartz.')
 
    # Now, define a couple of other loggers which might represent areas in your
    # application:
 
    logger1 = logging.getLogger('myapp.area1')
    logger2 = logging.getLogger('myapp.area2')
 
    logger1.debug('Quick zephyrs blow, vexing daft Jim.')
    logger1.info('How quickly daft jumping zebras vex.')
    logger2.warning('Jail zesty vixen who grabbed pay from quack.')
    logger2.error('The five boxing wizards jump quickly.')
 
if __name__ == '__main__':
#    test_override_funcs()
    import os
    print os.getcwd()
    test()