Skip to content

Commit f3b0451

Browse files
author
wallisyan
authored
Merge pull request aliyun#181 from wallisyan/add_logger
Add python logger
2 parents 5b71af6 + 8f83e21 commit f3b0451

File tree

11 files changed

+100
-48
lines changed

11 files changed

+100
-48
lines changed
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
1-
__version__ = "2.12.1"
1+
__version__ = "2.13.0"
22

3+
import logging
4+
5+
6+
class NullHandler(logging.Handler):
7+
def emit(self, record):
8+
pass
9+
10+
11+
logging.getLogger('aliyunsdkcore').addHandler(NullHandler())

aliyun-python-sdk-core/aliyunsdkcore/auth/signers/access_key_signer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
# specific language governing permissions and limitations
2020
# under the License.
2121

22+
import logging
2223
from aliyunsdkcore.auth.signers.signer import Signer
2324

25+
logger = logging.getLogger(__name__)
26+
2427

2528
class AccessKeySigner(Signer):
2629
def __init__(self, access_key_credential):

aliyun-python-sdk-core/aliyunsdkcore/auth/signers/ecs_ram_role_signer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
# under the License.
2121

2222
import time
23-
23+
import logging
24+
import json
2425

2526
from aliyunsdkcore.vendored.six.moves.urllib.request import urlopen
26-
2727
from aliyunsdkcore.auth.signers.signer import Signer
2828
from aliyunsdkcore.acs_exception.exceptions import ServerException
2929

30-
import json
30+
logger = logging.getLogger(__name__)
3131

3232

3333
class EcsRamRoleSigner(Signer):

aliyun-python-sdk-core/aliyunsdkcore/auth/signers/ram_role_arn_signer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import time
2323
import json
24+
import logging
2425

2526
from aliyunsdkcore.auth.signers.signer import Signer
2627
from aliyunsdkcore.auth.signers.access_key_signer import AccessKeySigner
@@ -31,6 +32,8 @@
3132
from aliyunsdkcore.request import CommonRequest
3233
from aliyunsdkcore.compat import ensure_string
3334

35+
logger = logging.getLogger(__name__)
36+
3437

3538
class RamRoleArnSigner(Signer):
3639
_SESSION_PERIOD = 3600

aliyun-python-sdk-core/aliyunsdkcore/auth/signers/rsa_key_pair_signer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from aliyunsdkcore.request import RpcRequest
1212
from aliyunsdkcore.auth.algorithm import sha_hmac256
1313

14+
logger = logging.getLogger(__name__)
15+
1416

1517
class RsaKeyPairSigner(Signer):
1618
_MIN_SESSION_PERIOD = 900

aliyun-python-sdk-core/aliyunsdkcore/auth/signers/sts_token_signer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
1919
# specific language governing permissions and limitations
2020
# under the License.
2121

22+
import logging
23+
2224
from aliyunsdkcore.auth.signers.signer import Signer
2325

26+
logger = logging.getLogger(__name__)
27+
2428

2529
class StsTokenSigner(Signer):
2630
def __init__(self, sts_credential):

aliyun-python-sdk-core/aliyunsdkcore/client.py

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
import time
2222
import warnings
2323
import json
24-
import aliyunsdkcore
24+
import logging
2525
import jmespath
26+
import copy
27+
28+
import aliyunsdkcore
2629
from aliyunsdkcore.vendored.six.moves.urllib.parse import urlencode
2730
from aliyunsdkcore.vendored.requests import codes
2831

@@ -53,8 +56,11 @@
5356
# TODO: replace it with TimeoutHandler
5457
_api_timeout_config_data = aliyunsdkcore.utils._load_json_from_data_dir("timeout_config.json")
5558

59+
logger = logging.getLogger(__name__)
60+
5661

5762
class AcsClient:
63+
LOG_FORMAT = '%(thread)d %(asctime)s %(name)s %(levelname)s %(message)s'
5864

5965
def __init__(
6066
self,
@@ -253,7 +259,6 @@ def _handle_retry_and_timeout(self, endpoint, request, signer):
253259

254260
request_timeout = self._get_request_timeout(request)
255261

256-
retryable = RetryCondition.SHOULD_RETRY
257262
retries = 0
258263

259264
while True:
@@ -266,6 +271,8 @@ def _handle_retry_and_timeout(self, endpoint, request, signer):
266271
retryable = self._retry_policy.should_retry(retry_policy_context)
267272
if retryable & RetryCondition.NO_RETRY:
268273
break
274+
logger.debug("Retry needed. Request:%s Retries :%d",
275+
request.get_action_name(), retries)
269276
retry_policy_context.retryable = retryable
270277
time_to_sleep = self._retry_policy.compute_delay_before_next_retry(retry_policy_context)
271278
time.sleep(time_to_sleep / 1000.0)
@@ -278,25 +285,22 @@ def _handle_retry_and_timeout(self, endpoint, request, signer):
278285

279286
def _handle_single_request(self, endpoint, request, timeout, signer):
280287
http_response = self._make_http_response(endpoint, request, timeout, signer)
281-
282-
exception = None
288+
params = copy.deepcopy(request.get_query_params())
289+
params.pop('AccessKeyId', None)
290+
logger.debug('Request received. Product:%s Endpoint:%s Params: %s',
291+
request.get_product(), endpoint, str(params))
283292

284293
# Do the actual network thing
285294
try:
286295
status, headers, body = http_response.get_response_object()
287296
except IOError as e:
288-
error_message = str(e)
289-
error_message += "\nEndpoint: " + endpoint
290-
error_message += "\nProduct: " + str(request.get_product())
291-
error_message += "\nSdkCoreVersion: " + aliyunsdkcore.__version__
292-
error_message += "\nHttpUrl: " + str(http_response.get_url())
293-
error_message += "\nHttpHeaders: " + \
294-
str(http_response.get_headers())
295-
296-
exception = ClientException(error_code.SDK_HTTP_ERROR, error_message)
297+
298+
exception = ClientException(error_code.SDK_HTTP_ERROR, str(e))
299+
logger.error("HttpError occurred. Host:%s SDK-Version:%s ClientException:%s",
300+
endpoint, aliyunsdkcore.__version__, str(exception))
297301
return None, None, None, exception
298302

299-
exception = self._get_server_exception(status, body)
303+
exception = self._get_server_exception(status, body, endpoint)
300304
return status, headers, body, exception
301305

302306
@staticmethod
@@ -312,11 +316,11 @@ def _parse_error_info_from_response_body(response_body):
312316
error_message_to_return = body_obj['Message']
313317
except ValueError:
314318
# failed to parse body as json format
315-
pass
319+
logger.warning('Failed to parse response as json format. Response:%s', response_body)
316320

317321
return error_code_to_return, error_message_to_return
318322

319-
def _get_server_exception(self, http_status, response_body):
323+
def _get_server_exception(self, http_status, response_body, endpoint):
320324
request_id = None
321325

322326
try:
@@ -325,29 +329,35 @@ def _get_server_exception(self, http_status, response_body):
325329
except (ValueError, TypeError, AttributeError):
326330
# in case the response body is not a json string, return the raw
327331
# data instead
328-
pass
332+
logger.warning('Failed to parse response as json format. Response:%s', response_body)
329333

330334
if http_status < codes.OK or http_status >= codes.MULTIPLE_CHOICES:
331335

332336
server_error_code, server_error_message = self._parse_error_info_from_response_body(
333337
response_body.decode('utf-8'))
334-
return ServerException(
338+
339+
exception = ServerException(
335340
server_error_code,
336341
server_error_message,
337342
http_status=http_status,
338343
request_id=request_id)
339344

345+
logger.error("ServerException occurred. Host:%s SDK-Version:%s ServerException:%s",
346+
endpoint, aliyunsdkcore.__version__, str(exception))
347+
348+
return exception
349+
340350
def do_action_with_exception(self, acs_request):
341351

342352
# set server response format as json, because this function will
343353
# parse the response so which format doesn't matter
344354
acs_request.set_accept_format('JSON')
345-
346355
status, headers, body, exception = self._implementation_of_do_action(acs_request)
347356

348357
if exception:
349358
raise exception
350-
359+
logger.debug('Response received. Product:%s Response-body: %s',
360+
acs_request.get_product(), body)
351361
return body
352362

353363
def _resolve_endpoint(self, request):
@@ -372,3 +382,24 @@ def get_response(self, acs_request):
372382
def add_endpoint(self, region_id, product_code, endpoint):
373383
self._endpoint_resolver.put_endpoint_entry(
374384
region_id, product_code, endpoint)
385+
386+
def set_stream_logger(self, log_level=logging.DEBUG, logger_name='aliyunsdkcore', stream=None,
387+
format_string=None):
388+
log = logging.getLogger(logger_name)
389+
log.setLevel(log_level)
390+
ch = logging.StreamHandler(stream)
391+
ch.setLevel(log_level)
392+
if format_string is None:
393+
format_string = self.LOG_FORMAT
394+
formatter = logging.Formatter(format_string)
395+
ch.setFormatter(formatter)
396+
log.addHandler(ch)
397+
398+
def set_file_logger(self, path, log_level=logging.DEBUG, logger_name='aliyunsdkcore'):
399+
log = logging.getLogger(logger_name)
400+
log.setLevel(log_level)
401+
fh = logging.FileHandler(path)
402+
fh.setLevel(log_level)
403+
formatter = logging.Formatter(self.LOG_FORMAT)
404+
fh.setFormatter(formatter)
405+
log.addHandler(fh)

aliyun-python-sdk-core/aliyunsdkcore/retry/retry_condition.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
# limitations under the License.
1414

1515
import jmespath
16+
import logging
1617

1718
import aliyunsdkcore.utils
1819
import aliyunsdkcore.utils.validation as validation
1920
from aliyunsdkcore.acs_exception.exceptions import ClientException, ServerException
2021
import aliyunsdkcore.acs_exception.error_code as error_code
2122

23+
logger = logging.getLogger(__name__)
24+
2225

2326
def _find_data_in_retry_config(key_name, request, retry_config):
2427
if request.get_product() is None:
@@ -55,9 +58,12 @@ def __init__(self, max_retry_times):
5558
self.max_retry_times = max_retry_times
5659

5760
def should_retry(self, retry_policy_context):
61+
5862
if retry_policy_context.retries_attempted < self.max_retry_times:
5963
return RetryCondition.SHOULD_RETRY
6064
else:
65+
logger.debug("Reached the maximum number of retry. Attempts:%d",
66+
retry_policy_context.retries_attempted)
6167
return RetryCondition.NO_RETRY
6268

6369

@@ -72,6 +78,9 @@ def should_retry(self, retry_policy_context):
7278

7379
if isinstance(exception, ClientException):
7480
if exception.get_error_code() == error_code.SDK_HTTP_ERROR:
81+
82+
logger.debug("Retryable ClientException occurred. ClientException:%s",
83+
exception)
7584
return RetryCondition.SHOULD_RETRY
7685

7786
if isinstance(exception, ServerException):
@@ -80,14 +89,18 @@ def should_retry(self, retry_policy_context):
8089
request,
8190
self.retry_config)
8291
if isinstance(normal_errors, list) and error_code_ in normal_errors:
92+
logger.debug("Retryable ServerException occurred. ServerException:%s",
93+
exception)
8394
return RetryCondition.SHOULD_RETRY
8495

8596
throttling_errors = _find_data_in_retry_config("RetryableThrottlingErrors",
8697
request,
8798
self.retry_config)
8899
if isinstance(throttling_errors, list) and error_code_ in throttling_errors:
100+
logger.debug("Retryable ThrottlingError occurred. ThrottlingError:%s",
101+
exception)
89102
return RetryCondition.SHOULD_RETRY | \
90-
RetryCondition.SHOULD_RETRY_WITH_THROTTLING_BACKOFF
103+
RetryCondition.SHOULD_RETRY_WITH_THROTTLING_BACKOFF
91104

92105
return RetryCondition.NO_RETRY
93106

@@ -106,6 +119,9 @@ def __init__(self, retryable_http_status_list=None):
106119

107120
def should_retry(self, retry_policy_context):
108121
if retry_policy_context.http_status_code in self.retryable_http_status_list:
122+
logger.debug(
123+
"Retryable HTTP error occurred. HTTP status code: %s",
124+
retry_policy_context.http_status_code)
109125
return RetryCondition.SHOULD_RETRY
110126
else:
111127
return RetryCondition.NO_RETRY

aliyun-python-sdk-core/aliyunsdkcore/vendored/requests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,6 @@ def emit(self, record):
7272
pass
7373

7474
logging.getLogger(__name__).addHandler(NullHandler())
75+
logging.getLogger(__name__).setLevel(logging.CRITICAL)
7576

7677

python-sdk-functional-test/credentials_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def test_call_roa_request_with_sts_token(self):
7878
def test_ecs_ram_role(self):
7979
# push ecs
8080
from aliyunsdkcore.auth.credentials import EcsRamRoleCredential
81-
ecs_ram_role_credential = EcsRamRoleCredential("TestRole")
81+
ecs_ram_role_credential = EcsRamRoleCredential("EcsRamRoleTest")
8282
acs_client = AcsClient(region_id="cn-hangzhou", credential=ecs_ram_role_credential)
8383
request = DescribeRegionsRequest()
8484
response = acs_client.do_action_with_exception(request)

0 commit comments

Comments
 (0)