From c7fed61539590337e017cf41b9220f051b3899a3 Mon Sep 17 00:00:00 2001 From: LittleCoder Date: Sat, 12 Nov 2016 16:07:21 +0800 Subject: [PATCH] Add logging functions --- itchat/__init__.py | 3 ++- itchat/components/login.py | 2 +- itchat/components/messages.py | 2 +- itchat/core.py | 8 +------ itchat/log.py | 40 +++++++++++++++++++++++++++++------ 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/itchat/__init__.py b/itchat/__init__.py index a3caa7d6..679cb9dc 100644 --- a/itchat/__init__.py +++ b/itchat/__init__.py @@ -1,5 +1,6 @@ from .core import Core from . import content +from .log import set_logging __version__ = '1.1.21' @@ -56,7 +57,7 @@ def new_instance(): msg_register = originInstance.msg_register run = originInstance.run # other functions -set_logging = originInstance.set_logging search_friends = originInstance.search_friends search_chatrooms = originInstance.search_chatrooms search_mps = originInstance.search_mps +set_logging = set_logging diff --git a/itchat/components/login.py b/itchat/components/login.py index 2907c4bc..dee9fb29 100644 --- a/itchat/components/login.py +++ b/itchat/components/login.py @@ -94,7 +94,7 @@ def check_login(self, uuid=None): regx = r'window.code=(\d+)' data = re.search(regx, r.text) if data and data.group(1) == '200': - process_login_info(self, r.content) + process_login_info(self, r.text) return '200' elif data and data.group(1) == '201': return '201' diff --git a/itchat/components/messages.py b/itchat/components/messages.py index 64556286..38dbc679 100644 --- a/itchat/components/messages.py +++ b/itchat/components/messages.py @@ -286,7 +286,7 @@ def send_file(self, fileDir, mediaId=None, toUserName=None): data=json.dumps(data, ensure_ascii=False).encode('utf8')) return ReturnValue(rawResponse=r) -def send_image(self, fileDir=None, mediaId=None, toUserName=None): +def send_image(self, fileDir, mediaId=None, toUserName=None): if toUserName is None: toUserName = self.storageClass.userName if mediaId is None: r = self.upload_file(fileDir, isPicture=not fileDir[-4:] == '.gif') diff --git a/itchat/core.py b/itchat/core.py index b1e31909..660595e5 100644 --- a/itchat/core.py +++ b/itchat/core.py @@ -147,7 +147,7 @@ def send_file(self, fileDir, mediaId=None, toUserName=None): * will be initialized in messages ''' raise NotImplementedError() - def send_image(self, fileDir=None, mediaId=None, toUserName=None): + def send_image(self, fileDir, mediaId=None, toUserName=None): ''' place for docs * will be initialized in messages ''' @@ -197,12 +197,6 @@ def run(self, debug=True): * will be initialized in messages ''' raise NotImplementedError() - def set_logging(self, showOnCmd=True, loggingFile=None, - loggingLevel=logging.DEBUG): - ''' place for docs - * will be initialized in messages - ''' - raise NotImplementedError() def search_friends(self, name=None, userName=None, remarkName=None, nickName=None, wechatAccount=None): return self.storageClass.search_friends(name, userName, remarkName, diff --git a/itchat/log.py b/itchat/log.py index f88a2293..e29c8d89 100644 --- a/itchat/log.py +++ b/itchat/log.py @@ -1,10 +1,36 @@ import logging -logger = logging.getLogger('itchat') +class LogSystem(object): + handlerList = [] + showOnCmd = True + loggingLevel = logging.INFO + loggingFile = None + def __init__(self): + self.logger = logging.getLogger('itchat') + self.logger.addHandler(logging.NullHandler()) + self.logger.setLevel(self.loggingLevel) + self.cmdHandler = logging.StreamHandler() + self.fileHandler = None + self.logger.addHandler(self.cmdHandler) + def set_logging(self, showOnCmd=True, loggingFile=None, + loggingLevel=logging.INFO): + if showOnCmd != self.showOnCmd: + if showOnCmd: + self.logger.addHandler(self.cmdHandler) + else: + self.logger.removeHandler(self.cmdHandler) + self.showOnCmd = showOnCmd + if loggingFile != self.loggingFile: + if self.loggingFile is not None: # clear old fileHandler + self.logger.removeHandler(self.fileHandler) + self.fileHandler.close() + if loggingFile is not None: # add new fileHandler + self.fileHandler = logging.FileHandler(loggingFile) + self.logger.addHandler(self.fileHandler) + self.loggingFile = loggingFile + if loggingLevel != self.loggingLevel: + self.logger.setLevel(loggingLevel) + self.loggingLevel = loggingLevel -logger.setLevel(logging.DEBUG) - -cmdHandler = logging.StreamHandler() -cmdHandler.setLevel(logging.DEBUG) - -logger.addHandler(cmdHandler) +ls = LogSystem() +set_logging = ls.set_logging