Skip to content

Commit

Permalink
Add logging functions
Browse files Browse the repository at this point in the history
  • Loading branch information
littlecodersh committed Nov 12, 2016
1 parent 6cd5ae4 commit c7fed61
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
3 changes: 2 additions & 1 deletion itchat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .core import Core
from . import content
from .log import set_logging

__version__ = '1.1.21'

Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion itchat/components/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion itchat/components/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
8 changes: 1 addition & 7 deletions itchat/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
'''
Expand Down Expand Up @@ -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,
Expand Down
40 changes: 33 additions & 7 deletions itchat/log.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c7fed61

Please sign in to comment.