Skip to content

Commit 46046b0

Browse files
authored
Merge pull request microsoft#109 from Microsoft/users/tedchamb/dev
update logging
2 parents 12a4e1a + c870574 commit 46046b0

File tree

4 files changed

+33
-26
lines changed

4 files changed

+33
-26
lines changed

vsts/vsts/_file_cache.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import collections
1515

1616

17+
logger = logging.getLogger(__name__)
18+
19+
1720
class FileCache(collections.MutableMapping):
1821
"""A simple dict-like class that is backed by a JSON file.
1922
@@ -33,20 +36,20 @@ def load(self):
3336
try:
3437
if os.path.isfile(self.file_name):
3538
if self.max_age > 0 and os.stat(self.file_name).st_mtime + self.max_age < time.clock():
36-
logging.debug('Cache file expired: %s', file=self.file_name)
39+
logger.debug('Cache file expired: %s', file=self.file_name)
3740
os.remove(self.file_name)
3841
else:
39-
logging.debug('Loading cache file: %s', self.file_name)
42+
logger.debug('Loading cache file: %s', self.file_name)
4043
self.data = get_file_json(self.file_name, throw_on_empty=False) or {}
4144
else:
42-
logging.debug('Cache file does not exist: %s', self.file_name)
45+
logger.debug('Cache file does not exist: %s', self.file_name)
4346
except Exception as ex:
44-
logging.exception(ex)
47+
logger.debug(ex, exc_info=True)
4548
# file is missing or corrupt so attempt to delete it
4649
try:
4750
os.remove(self.file_name)
4851
except Exception as ex2:
49-
logging.exception(ex2)
52+
logger.debug(ex2, exc_info=True)
5053
self.initial_load_occurred = True
5154

5255
def save(self):
@@ -71,10 +74,10 @@ def save_with_retry(self, retries=5):
7174

7275
def clear(self):
7376
if os.path.isfile(self.file_name):
74-
logging.info("Deleting file: " + self.file_name)
77+
logger.info("Deleting file: " + self.file_name)
7578
os.remove(self.file_name)
7679
else:
77-
logging.info("File does not exist: " + self.file_name)
80+
logger.info("File does not exist: " + self.file_name)
7881

7982
def get(self, key, default=None):
8083
self._check_for_initial_load()
@@ -144,12 +147,12 @@ def read_file_content(file_path, allow_binary=False):
144147
for encoding in ['utf-8-sig', 'utf-8', 'utf-16', 'utf-16le', 'utf-16be']:
145148
try:
146149
with codecs_open(file_path, encoding=encoding) as f:
147-
logging.debug("attempting to read file %s as %s", file_path, encoding)
150+
logger.debug("attempting to read file %s as %s", file_path, encoding)
148151
return f.read()
149152
except UnicodeDecodeError:
150153
if allow_binary:
151154
with open(file_path, 'rb') as input_file:
152-
logging.debug("attempting to read file %s as binary", file_path)
155+
logger.debug("attempting to read file %s as binary", file_path)
153156
return base64.b64encode(input_file.read()).decode("utf-8")
154157
else:
155158
raise

vsts/vsts/exceptions.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from msrest.exceptions import (
77
ClientException,
8-
TokenExpiredError,
98
ClientRequestError,
109
AuthenticationError,
1110
)

vsts/vsts/vss_client.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
from ._file_cache import OPTIONS_CACHE as OPTIONS_FILE_CACHE
2121

2222

23+
logger = logging.getLogger(__name__)
24+
25+
2326
class VssClient(object):
2427
"""VssClient.
2528
:param str base_url: Service URL
@@ -50,11 +53,11 @@ def _send_request(self, request, headers=None, content=None, **operation_config)
5053
"""
5154
if TRACE_ENV_VAR in os.environ and os.environ[TRACE_ENV_VAR] == 'true':
5255
print(request.method + ' ' + request.url)
53-
logging.debug('%s %s', request.method, request.url)
54-
logging.debug('Request content: %s', content)
56+
logger.debug('%s %s', request.method, request.url)
57+
logger.debug('Request content: %s', content)
5558
response = self._client.send(request=request, headers=headers,
5659
content=content, **operation_config)
57-
logging.debug('Response content: %s', response.content)
60+
logger.debug('Response content: %s', response.content)
5861
if response.status_code < 200 or response.status_code >= 300:
5962
self._handle_error(request, response)
6063
return response
@@ -71,11 +74,11 @@ def _send(self, http_method, location_id, version, route_values=None,
7174
version)
7275

7376
if version != negotiated_version:
74-
logging.info("Negotiated api version from '%s' down to '%s'. This means the client is newer than the server.",
75-
version,
76-
negotiated_version)
77+
logger.info("Negotiated api version from '%s' down to '%s'. This means the client is newer than the server.",
78+
version,
79+
negotiated_version)
7780
else:
78-
logging.debug("Api version '%s'", negotiated_version)
81+
logger.debug("Api version '%s'", negotiated_version)
7982

8083
# Construct headers
8184
headers = {'Content-Type': media_type + '; charset=utf-8',
@@ -112,7 +115,7 @@ def _create_request_message(self, http_method, location_id, route_values=None,
112115
route_values['resource'] = location.resource_name
113116
route_template = self._remove_optional_route_parameters(location.route_template,
114117
route_values)
115-
logging.debug('Route template: %s', location.route_template)
118+
logger.debug('Route template: %s', location.route_template)
116119
url = self._client.format_url(route_template, **route_values)
117120
request = ClientRequest()
118121
request.url = self._client.format_url(url)
@@ -150,14 +153,14 @@ def _get_resource_locations(self, all_host_types):
150153
# Next check for options cached on disk
151154
if not all_host_types and OPTIONS_FILE_CACHE[self.normalized_url]:
152155
try:
153-
logging.debug('File cache hit for options on: %s', self.normalized_url)
156+
logger.debug('File cache hit for options on: %s', self.normalized_url)
154157
self._locations = self._base_deserialize.deserialize_data(OPTIONS_FILE_CACHE[self.normalized_url],
155158
'[ApiResourceLocation]')
156159
return self._locations
157160
except DeserializationError as ex:
158-
logging.exception(str(ex))
161+
logger.debug(ex, exc_info=True)
159162
else:
160-
logging.debug('File cache miss for options on: %s', self.normalized_url)
163+
logger.debug('File cache miss for options on: %s', self.normalized_url)
161164

162165
# Last resort, make the call to the server
163166
options_uri = self._combine_url(self.config.base_url, '_apis')
@@ -184,7 +187,7 @@ def _get_resource_locations(self, all_host_types):
184187
try:
185188
OPTIONS_FILE_CACHE[self.normalized_url] = wrapper.value
186189
except SerializationError as ex:
187-
logging.exception(str(ex))
190+
logger.debug(ex, exc_info=True)
188191
return returned_locations
189192

190193
@staticmethod

vsts/vsts/vss_connection.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from .location.v4_0.location_client import LocationClient
1212
from .vss_client_configuration import VssClientConfiguration
1313

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

1517
class VssConnection(object):
1618
"""VssConnection.
@@ -77,14 +79,14 @@ def _get_resource_areas(self, force=False):
7779
location_client = LocationClient(self.base_url, self._creds)
7880
if not force and RESOURCE_FILE_CACHE[location_client.normalized_url]:
7981
try:
80-
logging.debug('File cache hit for resources on: %s', location_client.normalized_url)
82+
logger.debug('File cache hit for resources on: %s', location_client.normalized_url)
8183
self._resource_areas = location_client._base_deserialize.deserialize_data(RESOURCE_FILE_CACHE[location_client.normalized_url],
8284
'[ResourceAreaInfo]')
8385
return self._resource_areas
8486
except Exception as ex:
85-
logging.exception(str(ex))
87+
logger.debug(ex, exc_info=True)
8688
elif not force:
87-
logging.debug('File cache miss for resources on: %s', location_client.normalized_url)
89+
logger.debug('File cache miss for resources on: %s', location_client.normalized_url)
8890
self._resource_areas = location_client.get_resource_areas()
8991
if self._resource_areas is None:
9092
# For OnPrem environments we get an empty collection wrapper.
@@ -94,7 +96,7 @@ def _get_resource_areas(self, force=False):
9496
'[ResourceAreaInfo]')
9597
RESOURCE_FILE_CACHE[location_client.normalized_url] = serialized
9698
except Exception as ex:
97-
logging.exception(str(ex))
99+
logger.debug(ex, exc_info=True)
98100
return self._resource_areas
99101

100102
@staticmethod

0 commit comments

Comments
 (0)