Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0969915

Browse files
committed
move logging into __name__-specific loggers from the root logger
1 parent 12b7cd3 commit 0969915

File tree

7 files changed

+57
-35
lines changed

7 files changed

+57
-35
lines changed

googleapiclient/__init__.py

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

1515
__version__ = "1.5.0"
16+
17+
# Set default logging handler to avoid "No handler found" warnings.
18+
import logging
19+
20+
try: # Python 2.7+
21+
from logging import NullHandler
22+
except ImportError:
23+
class NullHandler(logging.Handler):
24+
def emit(self, record):
25+
pass
26+
27+
logging.getLogger(__name__).addHandler(NullHandler())

googleapiclient/discovery_cache/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import logging
2020
import datetime
2121

22+
23+
LOGGER = logging.getLogger(__name__)
24+
2225
DISCOVERY_DOC_MAX_AGE = 60 * 60 * 24 # 1 day
2326

2427

@@ -38,5 +41,5 @@ def autodetect():
3841
from . import file_cache
3942
return file_cache.cache
4043
except Exception as e:
41-
logging.warning(e, exc_info=True)
44+
LOGGER.warning(e, exc_info=True)
4245
return None

googleapiclient/discovery_cache/appengine_memcache.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
from . import base
2424
from ..discovery_cache import DISCOVERY_DOC_MAX_AGE
2525

26+
27+
LOGGER = logging.getLogger(__name__)
28+
2629
NAMESPACE = 'google-api-client'
2730

2831

@@ -41,12 +44,12 @@ def get(self, url):
4144
try:
4245
return memcache.get(url, namespace=NAMESPACE)
4346
except Exception as e:
44-
logging.warning(e, exc_info=True)
47+
LOGGER.warning(e, exc_info=True)
4548

4649
def set(self, url, content):
4750
try:
4851
memcache.set(url, content, time=int(self._max_age), namespace=NAMESPACE)
4952
except Exception as e:
50-
logging.warning(e, exc_info=True)
53+
LOGGER.warning(e, exc_info=True)
5154

5255
cache = Cache(max_age=DISCOVERY_DOC_MAX_AGE)

googleapiclient/discovery_cache/file_cache.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from . import base
3535
from ..discovery_cache import DISCOVERY_DOC_MAX_AGE
3636

37-
logger = logging.getLogger(__name__)
37+
LOGGER = logging.getLogger(__name__)
3838

3939
FILENAME = 'google-api-python-client-discovery-doc.cache'
4040
EPOCH = datetime.datetime.utcfromtimestamp(0)
@@ -84,7 +84,7 @@ def __init__(self, max_age):
8484
# If we can not obtain the lock, other process or thread must
8585
# have initialized the file.
8686
except Exception as e:
87-
logging.warning(e, exc_info=True)
87+
LOGGER.warning(e, exc_info=True)
8888
finally:
8989
f.unlock_and_close()
9090

@@ -100,10 +100,10 @@ def get(self, url):
100100
return content
101101
return None
102102
else:
103-
logger.debug('Could not obtain a lock for the cache file.')
103+
LOGGER.debug('Could not obtain a lock for the cache file.')
104104
return None
105105
except Exception as e:
106-
logger.warning(e, exc_info=True)
106+
LOGGER.warning(e, exc_info=True)
107107
finally:
108108
f.unlock_and_close()
109109

@@ -122,9 +122,9 @@ def set(self, url, content):
122122
f.file_handle().seek(0)
123123
json.dump(cache, f.file_handle())
124124
else:
125-
logger.debug('Could not obtain a lock for the cache file.')
125+
LOGGER.debug('Could not obtain a lock for the cache file.')
126126
except Exception as e:
127-
logger.warning(e, exc_info=True)
127+
LOGGER.warning(e, exc_info=True)
128128
finally:
129129
f.unlock_and_close()
130130

googleapiclient/http.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
from oauth2client import util
5858

5959

60+
LOGGER = logging.getLogger(__name__)
61+
6062
DEFAULT_CHUNK_SIZE = 512*1024
6163

6264
MAX_URI_LENGTH = 2048
@@ -85,7 +87,7 @@ def _retry_request(http, num_retries, req_type, sleep, rand, uri, method, *args,
8587
for retry_num in range(num_retries + 1):
8688
if retry_num > 0:
8789
sleep(rand() * 2**retry_num)
88-
logging.warning(
90+
LOGGER.warning(
8991
'Retry #%d for %s: %s %s%s' % (retry_num, req_type, method, uri,
9092
', following status: %d' % resp.status if resp else ''))
9193

@@ -882,7 +884,7 @@ def next_chunk(self, http=None, num_retries=0):
882884
for retry_num in range(num_retries + 1):
883885
if retry_num > 0:
884886
self._sleep(self._rand() * 2**retry_num)
885-
logging.warning(
887+
LOGGER.warning(
886888
'Retry #%d for media upload: %s %s, following status: %d'
887889
% (retry_num, self.method, self.uri, resp.status))
888890

@@ -1632,7 +1634,7 @@ def new_request(uri, method='GET', body=None, headers=None,
16321634
headers = {}
16331635
if method == 'PATCH':
16341636
if 'oauth_token' in headers.get('authorization', ''):
1635-
logging.warning(
1637+
LOGGER.warning(
16361638
'OAuth 1.0 request made with Credentials after tunnel_patch.')
16371639
headers['x-http-method-override'] = "PATCH"
16381640
method = 'POST'

googleapiclient/model.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
from googleapiclient.errors import HttpError
3434

3535

36+
LOGGER = logging.getLogger(__name__)
37+
3638
dump_request_response = False
3739

3840

@@ -105,18 +107,18 @@ class BaseModel(Model):
105107
def _log_request(self, headers, path_params, query, body):
106108
"""Logs debugging information about the request if requested."""
107109
if dump_request_response:
108-
logging.info('--request-start--')
109-
logging.info('-headers-start-')
110+
LOGGER.info('--request-start--')
111+
LOGGER.info('-headers-start-')
110112
for h, v in six.iteritems(headers):
111-
logging.info('%s: %s', h, v)
112-
logging.info('-headers-end-')
113-
logging.info('-path-parameters-start-')
113+
LOGGER.info('%s: %s', h, v)
114+
LOGGER.info('-headers-end-')
115+
LOGGER.info('-path-parameters-start-')
114116
for h, v in six.iteritems(path_params):
115-
logging.info('%s: %s', h, v)
116-
logging.info('-path-parameters-end-')
117-
logging.info('body: %s', body)
118-
logging.info('query: %s', query)
119-
logging.info('--request-end--')
117+
LOGGER.info('%s: %s', h, v)
118+
LOGGER.info('-path-parameters-end-')
119+
LOGGER.info('body: %s', body)
120+
LOGGER.info('query: %s', query)
121+
LOGGER.info('--request-end--')
120122

121123
def request(self, headers, path_params, query_params, body_value):
122124
"""Updates outgoing requests with a serialized body.
@@ -176,12 +178,12 @@ def _build_query(self, params):
176178
def _log_response(self, resp, content):
177179
"""Logs debugging information about the response if requested."""
178180
if dump_request_response:
179-
logging.info('--response-start--')
181+
LOGGER.info('--response-start--')
180182
for h, v in six.iteritems(resp):
181-
logging.info('%s: %s', h, v)
183+
LOGGER.info('%s: %s', h, v)
182184
if content:
183-
logging.info(content)
184-
logging.info('--response-end--')
185+
LOGGER.info(content)
186+
LOGGER.info('--response-end--')
185187

186188
def response(self, resp, content):
187189
"""Convert the response wire format into a Python object.
@@ -206,7 +208,7 @@ def response(self, resp, content):
206208
return self.no_content_response
207209
return self.deserialize(content)
208210
else:
209-
logging.debug('Content from bad request was: %s' % content)
211+
LOGGER.debug('Content from bad request was: %s' % content)
210212
raise HttpError(resp, content)
211213

212214
def serialize(self, body_value):

tests/test_json_model.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ def __init__(self, items):
219219
self.status = items['status']
220220
for key, value in six.iteritems(items):
221221
self[key] = value
222-
old_logging = googleapiclient.model.logging
223-
googleapiclient.model.logging = MockLogging()
222+
old_logging = googleapiclient.model.LOGGER
223+
googleapiclient.model.LOGGER = MockLogging()
224224
googleapiclient.model.dump_request_response = True
225225
model = JsonModel()
226226
request_body = {
@@ -236,18 +236,18 @@ def __init__(self, items):
236236
'response_field_2': 'response_value_2'}
237237
response_body = model.response(MockResponse(response), body_string)
238238
self.assertEqual(request_body, response_body)
239-
self.assertEqual(googleapiclient.model.logging.info_record[:2],
239+
self.assertEqual(googleapiclient.model.LOGGER.info_record[:2],
240240
['--request-start--',
241241
'-headers-start-'])
242242
self.assertTrue('response_field_1: response_value_1' in
243-
googleapiclient.model.logging.info_record)
243+
googleapiclient.model.LOGGER.info_record)
244244
self.assertTrue('response_field_2: response_value_2' in
245-
googleapiclient.model.logging.info_record)
246-
self.assertEqual(json.loads(googleapiclient.model.logging.info_record[-2]),
245+
googleapiclient.model.LOGGER.info_record)
246+
self.assertEqual(json.loads(googleapiclient.model.LOGGER.info_record[-2]),
247247
request_body)
248-
self.assertEqual(googleapiclient.model.logging.info_record[-1],
248+
self.assertEqual(googleapiclient.model.LOGGER.info_record[-1],
249249
'--response-end--')
250-
googleapiclient.model.logging = old_logging
250+
googleapiclient.model.LOGGER = old_logging
251251

252252
def test_no_data_wrapper_deserialize(self):
253253
model = JsonModel(data_wrapper=False)

0 commit comments

Comments
 (0)