Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move logging into __name__-specific loggers from the root logger #206

Merged
merged 1 commit into from
Mar 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions googleapiclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@
# limitations under the License.

__version__ = "1.5.0"

# Set default logging handler to avoid "No handler found" warnings.
import logging

try: # Python 2.7+
from logging import NullHandler
except ImportError:
class NullHandler(logging.Handler):
def emit(self, record):
pass

logging.getLogger(__name__).addHandler(NullHandler())

This comment was marked as spam.

5 changes: 4 additions & 1 deletion googleapiclient/discovery_cache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import logging
import datetime


LOGGER = logging.getLogger(__name__)

DISCOVERY_DOC_MAX_AGE = 60 * 60 * 24 # 1 day


Expand All @@ -38,5 +41,5 @@ def autodetect():
from . import file_cache
return file_cache.cache
except Exception as e:
logging.warning(e, exc_info=True)
LOGGER.warning(e, exc_info=True)
return None
7 changes: 5 additions & 2 deletions googleapiclient/discovery_cache/appengine_memcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
from . import base
from ..discovery_cache import DISCOVERY_DOC_MAX_AGE


LOGGER = logging.getLogger(__name__)

NAMESPACE = 'google-api-client'


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

def set(self, url, content):
try:
memcache.set(url, content, time=int(self._max_age), namespace=NAMESPACE)
except Exception as e:
logging.warning(e, exc_info=True)
LOGGER.warning(e, exc_info=True)

cache = Cache(max_age=DISCOVERY_DOC_MAX_AGE)
12 changes: 6 additions & 6 deletions googleapiclient/discovery_cache/file_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from . import base
from ..discovery_cache import DISCOVERY_DOC_MAX_AGE

logger = logging.getLogger(__name__)
LOGGER = logging.getLogger(__name__)

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

Expand All @@ -100,10 +100,10 @@ def get(self, url):
return content
return None
else:
logger.debug('Could not obtain a lock for the cache file.')
LOGGER.debug('Could not obtain a lock for the cache file.')
return None
except Exception as e:
logger.warning(e, exc_info=True)
LOGGER.warning(e, exc_info=True)
finally:
f.unlock_and_close()

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

Expand Down
8 changes: 5 additions & 3 deletions googleapiclient/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
from oauth2client import util


LOGGER = logging.getLogger(__name__)

DEFAULT_CHUNK_SIZE = 512*1024

MAX_URI_LENGTH = 2048
Expand Down Expand Up @@ -85,7 +87,7 @@ def _retry_request(http, num_retries, req_type, sleep, rand, uri, method, *args,
for retry_num in range(num_retries + 1):
if retry_num > 0:
sleep(rand() * 2**retry_num)
logging.warning(
LOGGER.warning(
'Retry #%d for %s: %s %s%s' % (retry_num, req_type, method, uri,
', following status: %d' % resp.status if resp else ''))

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

Expand Down Expand Up @@ -1632,7 +1634,7 @@ def new_request(uri, method='GET', body=None, headers=None,
headers = {}
if method == 'PATCH':
if 'oauth_token' in headers.get('authorization', ''):
logging.warning(
LOGGER.warning(
'OAuth 1.0 request made with Credentials after tunnel_patch.')
headers['x-http-method-override'] = "PATCH"
method = 'POST'
Expand Down
32 changes: 17 additions & 15 deletions googleapiclient/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
from googleapiclient.errors import HttpError


LOGGER = logging.getLogger(__name__)

dump_request_response = False


Expand Down Expand Up @@ -105,18 +107,18 @@ class BaseModel(Model):
def _log_request(self, headers, path_params, query, body):
"""Logs debugging information about the request if requested."""
if dump_request_response:
logging.info('--request-start--')
logging.info('-headers-start-')
LOGGER.info('--request-start--')
LOGGER.info('-headers-start-')
for h, v in six.iteritems(headers):
logging.info('%s: %s', h, v)
logging.info('-headers-end-')
logging.info('-path-parameters-start-')
LOGGER.info('%s: %s', h, v)
LOGGER.info('-headers-end-')
LOGGER.info('-path-parameters-start-')
for h, v in six.iteritems(path_params):
logging.info('%s: %s', h, v)
logging.info('-path-parameters-end-')
logging.info('body: %s', body)
logging.info('query: %s', query)
logging.info('--request-end--')
LOGGER.info('%s: %s', h, v)
LOGGER.info('-path-parameters-end-')
LOGGER.info('body: %s', body)
LOGGER.info('query: %s', query)
LOGGER.info('--request-end--')

def request(self, headers, path_params, query_params, body_value):
"""Updates outgoing requests with a serialized body.
Expand Down Expand Up @@ -176,12 +178,12 @@ def _build_query(self, params):
def _log_response(self, resp, content):
"""Logs debugging information about the response if requested."""
if dump_request_response:
logging.info('--response-start--')
LOGGER.info('--response-start--')
for h, v in six.iteritems(resp):
logging.info('%s: %s', h, v)
LOGGER.info('%s: %s', h, v)
if content:
logging.info(content)
logging.info('--response-end--')
LOGGER.info(content)
LOGGER.info('--response-end--')

def response(self, resp, content):
"""Convert the response wire format into a Python object.
Expand All @@ -206,7 +208,7 @@ def response(self, resp, content):
return self.no_content_response
return self.deserialize(content)
else:
logging.debug('Content from bad request was: %s' % content)
LOGGER.debug('Content from bad request was: %s' % content)
raise HttpError(resp, content)

def serialize(self, body_value):
Expand Down
16 changes: 8 additions & 8 deletions tests/test_json_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ def __init__(self, items):
self.status = items['status']
for key, value in six.iteritems(items):
self[key] = value
old_logging = googleapiclient.model.logging
googleapiclient.model.logging = MockLogging()
old_logging = googleapiclient.model.LOGGER
googleapiclient.model.LOGGER = MockLogging()
googleapiclient.model.dump_request_response = True
model = JsonModel()
request_body = {
Expand All @@ -236,18 +236,18 @@ def __init__(self, items):
'response_field_2': 'response_value_2'}
response_body = model.response(MockResponse(response), body_string)
self.assertEqual(request_body, response_body)
self.assertEqual(googleapiclient.model.logging.info_record[:2],
self.assertEqual(googleapiclient.model.LOGGER.info_record[:2],
['--request-start--',
'-headers-start-'])
self.assertTrue('response_field_1: response_value_1' in
googleapiclient.model.logging.info_record)
googleapiclient.model.LOGGER.info_record)
self.assertTrue('response_field_2: response_value_2' in
googleapiclient.model.logging.info_record)
self.assertEqual(json.loads(googleapiclient.model.logging.info_record[-2]),
googleapiclient.model.LOGGER.info_record)
self.assertEqual(json.loads(googleapiclient.model.LOGGER.info_record[-2]),
request_body)
self.assertEqual(googleapiclient.model.logging.info_record[-1],
self.assertEqual(googleapiclient.model.LOGGER.info_record[-1],
'--response-end--')
googleapiclient.model.logging = old_logging
googleapiclient.model.LOGGER = old_logging

def test_no_data_wrapper_deserialize(self):
model = JsonModel(data_wrapper=False)
Expand Down