Commit 25300fa5 authored by fendouai's avatar fendouai

add customize of logging level

parent 6612ae14
...@@ -6,15 +6,21 @@ from jpush import common ...@@ -6,15 +6,21 @@ from jpush import common
_jpush = jpush.JPush(app_key, master_secret) _jpush = jpush.JPush(app_key, master_secret)
push = _jpush.create_push() push = _jpush.create_push()
# the default logging level is WARNING,if you set the logging level to "DEBUG",the it will show the debug logging
_jpush.set_logging("DEBUG")
push.audience = jpush.all_ push.audience = jpush.all_
push.notification = jpush.notification(alert="hello python jpush api") push.notification = jpush.notification(alert="hello python jpush api")
push.platform = jpush.all_ push.platform = jpush.all_
core.logger.debug('logger debug message test')
try: try:
response=push.send() response=push.send()
except common.Unauthorized: except common.Unauthorized:
raise common.Unauthorized("Unauthorized") raise common.Unauthorized("Unauthorized")
except common.APIConnectionException: except common.APIConnectionException:
raise common.APIConnectionException("conn") raise common.APIConnectionException("conn")
except common.JPushFailure:
print "JPushFailure"
except:
print "Exception"
\ No newline at end of file
...@@ -11,11 +11,6 @@ from .schedule import Schedule ...@@ -11,11 +11,6 @@ from .schedule import Schedule
logger = logging.getLogger('jpush') logger = logging.getLogger('jpush')
logging.basicConfig(level=logging.DEBUG,
format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt = '%a, %d %b %Y %H:%M:%S',
filename = 'jpush.log',
filemode = 'a')
class JPush(object): class JPush(object):
...@@ -34,9 +29,7 @@ class JPush(object): ...@@ -34,9 +29,7 @@ class JPush(object):
headers['content-type'] = 'application/json;charset:utf-8' headers['content-type'] = 'application/json;charset:utf-8'
logger.debug("Making %s request to %s. Headers:\n\t%s\nBody:\n\t%s", logger.debug("Making %s request to %s. Headers:\n\t%s\nBody:\n\t%s",
method, url, '\n\t'.join( method, url, '\n\t'.join('%s: %s' % (key, value) for (key, value) in headers.items()), body)
'%s: %s' % (key, value) for (key, value) in headers.items()),
body)
try: try:
response = self.session.request(method, url, data=body, params=params, headers=headers, timeout=30) response = self.session.request(method, url, data=body, params=params, headers=headers, timeout=30)
except requests.exceptions.ConnectTimeout: except requests.exceptions.ConnectTimeout:
...@@ -44,11 +37,8 @@ class JPush(object): ...@@ -44,11 +37,8 @@ class JPush(object):
except: except:
raise common.APIConnectionException("Connection to api.jpush.cn error.") raise common.APIConnectionException("Connection to api.jpush.cn error.")
logger.debug("Received %s response. Headers:\n\t%s\nBody:\n\t%s", logger.debug("Received %s response. Headers:\n\t%s\nBody:\n\t%s", response.status_code, '\n\t'.join(
response.status_code, '\n\t'.join( '%s: %s' % (key, value) for (key, value) in response.headers.items()), response.content)
'%s: %s' % (key, value) for (key, value)
in response.headers.items()),
response.content)
if response.status_code == 401: if response.status_code == 401:
raise common.Unauthorized("Please check your AppKey and Master Secret") raise common.Unauthorized("Please check your AppKey and Master Secret")
...@@ -66,8 +56,25 @@ class JPush(object): ...@@ -66,8 +56,25 @@ class JPush(object):
"JPush.push() is deprecated. See documentation on upgrading.", "JPush.push() is deprecated. See documentation on upgrading.",
DeprecationWarning) DeprecationWarning)
body = json.dumps(payload) body = json.dumps(payload)
self._request('POST', body, common.PUSH_URL, self._request('POST', body, common.PUSH_URL, 'application/json', version=1)
'application/json', version=1)
def set_logging(self, level):
level_list= ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "NOTSET"]
if level in level_list:
if(level == "CRITICAL"):
logging.basicConfig(level=logging.CRITICAL)
if (level == "ERROR"):
logging.basicConfig(level=logging.ERROR)
if (level == "WARNING"):
logging.basicConfig(level=logging.WARNING)
if (level == "INFO"):
logging.basicConfig(level=logging.INFO)
if (level == "DEBUG"):
logging.basicConfig(level=logging.DEBUG)
if (level == "NOTSET"):
logging.basicConfig(level=logging.NOTSET)
else:
print "set logging level failed ,the level is invalid."
def create_push(self): def create_push(self):
"""Create a Push notification.""" """Create a Push notification."""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment