#-*- 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()
|
|