Skip to content

Commit

Permalink
fix new line delimiter (#23262)
Browse files Browse the repository at this point in the history
* fix new line delimiter

* update

* update
xiangyan99 authored Mar 1, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent fee3a91 commit 3f0c457
Showing 10 changed files with 111 additions and 73 deletions.
2 changes: 2 additions & 0 deletions sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -8,6 +8,8 @@

### Bugs Fixed

- use "\n" rather than "/n" for new line in log #23261

### Other Changes

- Log "WWW-Authenticate" header in `HttpLoggingPolicy` #22990
50 changes: 25 additions & 25 deletions sdk/core/azure-core/azure/core/pipeline/policies/_universal.py
Original file line number Diff line number Diff line change
@@ -283,29 +283,29 @@ def on_request(self, request): # pylint: disable=too-many-return-statements

try:
log_string = "Request URL: '{}'".format(http_request.url)
log_string += "/nRequest method: '{}'".format(http_request.method)
log_string += "/nRequest headers:"
log_string += "\nRequest method: '{}'".format(http_request.method)
log_string += "\nRequest headers:"
for header, value in http_request.headers.items():
log_string += "/n '{}': '{}'".format(header, value)
log_string += "/nRequest body:"
log_string += "\n '{}': '{}'".format(header, value)
log_string += "\nRequest body:"

# We don't want to log the binary data of a file upload.
if isinstance(http_request.body, types.GeneratorType):
log_string += "/nFile upload"
log_string += "\nFile upload"
_LOGGER.debug(log_string)
return
try:
if isinstance(http_request.body, types.AsyncGeneratorType):
log_string += "/nFile upload"
log_string += "\nFile upload"
_LOGGER.debug(log_string)
return
except AttributeError:
pass
if http_request.body:
log_string += "/n{}".format(str(http_request.body))
log_string += "\n{}".format(str(http_request.body))
_LOGGER.debug(log_string)
return
log_string += "/nThis request has no body"
log_string += "\nThis request has no body"
_LOGGER.debug(log_string)
except Exception as err: # pylint: disable=broad-except
_LOGGER.debug("Failed to log request: %r", err)
@@ -327,27 +327,27 @@ def on_response(self, request, response):
return

log_string = "Response status: '{}'".format(http_response.status_code)
log_string += "/nResponse headers:"
log_string += "\nResponse headers:"
for res_header, value in http_response.headers.items():
log_string += "/n '{}': '{}'".format(res_header, value)
log_string += "\n '{}': '{}'".format(res_header, value)

# We don't want to log binary data if the response is a file.
log_string += "/nResponse content:"
log_string += "\nResponse content:"
pattern = re.compile(r'attachment; ?filename=["\w.]+', re.IGNORECASE)
header = http_response.headers.get('content-disposition')

if header and pattern.match(header):
filename = header.partition('=')[2]
log_string += "/nFile attachments: {}".format(filename)
log_string += "\nFile attachments: {}".format(filename)
elif http_response.headers.get("content-type", "").endswith("octet-stream"):
log_string += "/nBody contains binary data."
log_string += "\nBody contains binary data."
elif http_response.headers.get("content-type", "").startswith("image"):
log_string += "/nBody contains image data."
log_string += "\nBody contains image data."
else:
if response.context.options.get('stream', False):
log_string += "/nBody is streamable."
log_string += "\nBody is streamable."
else:
log_string += "/n{}".format(http_response.text())
log_string += "\n{}".format(http_response.text())
_LOGGER.debug(log_string)
except Exception as err: # pylint: disable=broad-except
_LOGGER.debug("Failed to log response: %s", repr(err))
@@ -452,27 +452,27 @@ def on_request(self, request): # pylint: disable=too-many-return-statements
logger.info("No body was attached to the request")
return
log_string = "Request URL: '{}'".format(redacted_url)
log_string += "/nRequest method: '{}'".format(http_request.method)
log_string += "/nRequest headers:"
log_string += "\nRequest method: '{}'".format(http_request.method)
log_string += "\nRequest headers:"
for header, value in http_request.headers.items():
value = self._redact_header(header, value)
log_string += "/n '{}': '{}'".format(header, value)
log_string += "\n '{}': '{}'".format(header, value)
if isinstance(http_request.body, types.GeneratorType):
log_string += "/nFile upload"
log_string += "\nFile upload"
logger.info(log_string)
return
try:
if isinstance(http_request.body, types.AsyncGeneratorType):
log_string += "/nFile upload"
log_string += "\nFile upload"
logger.info(log_string)
return
except AttributeError:
pass
if http_request.body:
log_string += "/nA body is sent with the request"
log_string += "\nA body is sent with the request"
logger.info(log_string)
return
log_string += "/nNo body was attached to the request"
log_string += "\nNo body was attached to the request"
logger.info(log_string)

except Exception as err: # pylint: disable=broad-except
@@ -497,10 +497,10 @@ def on_response(self, request, response):
logger.info(" %r: %r", res_header, value)
return
log_string = "Response status: {}".format(http_response.status_code)
log_string += "/nResponse headers:"
log_string += "\nResponse headers:"
for res_header, value in http_response.headers.items():
value = self._redact_header(res_header, value)
log_string += "/n '{}': '{}'".format(res_header, value)
log_string += "\n '{}': '{}'".format(res_header, value)
logger.info(log_string)
except Exception as err: # pylint: disable=broad-except
logger.warning("Failed to log response: %s", repr(err))
Original file line number Diff line number Diff line change
@@ -51,8 +51,8 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 2
messages_request = mock_handler.messages[0].message.split("/n")
messages_response = mock_handler.messages[1].message.split("/n")
messages_request = mock_handler.messages[0].message.split("\n")
messages_response = mock_handler.messages[1].message.split("\n")
assert messages_request[0] == "Request URL: 'http://localhost/'"
assert messages_request[1] == "Request method: 'GET'"
assert messages_request[2] == 'Request headers:'
@@ -74,10 +74,10 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 4
messages_request1 = mock_handler.messages[0].message.split("/n")
messages_response1 = mock_handler.messages[1].message.split("/n")
messages_request2 = mock_handler.messages[2].message.split("/n")
messages_response2 = mock_handler.messages[3].message.split("/n")
messages_request1 = mock_handler.messages[0].message.split("\n")
messages_response1 = mock_handler.messages[1].message.split("\n")
messages_request2 = mock_handler.messages[2].message.split("\n")
messages_response2 = mock_handler.messages[3].message.split("\n")
assert messages_request1[0] == "Request URL: 'http://localhost/'"
assert messages_request1[1] == "Request method: 'GET'"
assert messages_request1[2] == 'Request headers:'
@@ -113,8 +113,8 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 2
messages_request = mock_handler.messages[0].message.split("/n")
messages_response = mock_handler.messages[1].message.split("/n")
messages_request = mock_handler.messages[0].message.split("\n")
messages_response = mock_handler.messages[1].message.split("\n")
assert messages_request[0] == "Request URL: 'http://localhost/?country=france&city=REDACTED'"
assert messages_request[1] == "Request method: 'GET'"
assert messages_request[2] == "Request headers:"
@@ -174,8 +174,8 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 2
messages_request = mock_handler.messages[0].message.split("/n")
messages_response = mock_handler.messages[1].message.split("/n")
messages_request = mock_handler.messages[0].message.split("\n")
messages_response = mock_handler.messages[1].message.split("\n")
assert messages_request[0] == "Request URL: 'http://localhost/'"
assert messages_request[1] == "Request method: 'GET'"
assert messages_request[2] == 'Request headers:'
@@ -199,10 +199,10 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 4
messages_request1 = mock_handler.messages[0].message.split("/n")
messages_response1 = mock_handler.messages[1].message.split("/n")
messages_request2 = mock_handler.messages[2].message.split("/n")
messages_response2 = mock_handler.messages[3].message.split("/n")
messages_request1 = mock_handler.messages[0].message.split("\n")
messages_response1 = mock_handler.messages[1].message.split("\n")
messages_request2 = mock_handler.messages[2].message.split("\n")
messages_response2 = mock_handler.messages[3].message.split("\n")
assert messages_request1[0] == "Request URL: 'http://localhost/'"
assert messages_request1[1] == "Request method: 'GET'"
assert messages_request1[2] == 'Request headers:'
@@ -249,8 +249,8 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 2
messages_request = mock_handler.messages[0].message.split("/n")
messages_response = mock_handler.messages[1].message.split("/n")
messages_request = mock_handler.messages[0].message.split("\n")
messages_response = mock_handler.messages[1].message.split("\n")
assert messages_request[0] == "Request URL: 'http://localhost/'"
assert messages_request[1] == "Request method: 'GET'"
assert messages_request[2] == 'Request headers:'
@@ -295,8 +295,8 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 2
messages_request = mock_handler.messages[0].message.split("/n")
messages_response = mock_handler.messages[1].message.split("/n")
messages_request = mock_handler.messages[0].message.split("\n")
messages_response = mock_handler.messages[1].message.split("\n")
assert messages_request[0] == "Request URL: 'http://localhost/'"
assert messages_request[1] == "Request method: 'GET'"
assert messages_request[2] == 'Request headers:'
36 changes: 18 additions & 18 deletions sdk/core/azure-core/tests/test_http_logging_policy.py
Original file line number Diff line number Diff line change
@@ -53,8 +53,8 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 2
messages_request = mock_handler.messages[0].message.split("/n")
messages_response = mock_handler.messages[1].message.split("/n")
messages_request = mock_handler.messages[0].message.split("\n")
messages_response = mock_handler.messages[1].message.split("\n")
assert messages_request[0] == "Request URL: 'http://localhost/'"
assert messages_request[1] == "Request method: 'GET'"
assert messages_request[2] == 'Request headers:'
@@ -76,10 +76,10 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 4
messages_request1 = mock_handler.messages[0].message.split("/n")
messages_response1 = mock_handler.messages[1].message.split("/n")
messages_request2 = mock_handler.messages[2].message.split("/n")
messages_response2 = mock_handler.messages[3].message.split("/n")
messages_request1 = mock_handler.messages[0].message.split("\n")
messages_response1 = mock_handler.messages[1].message.split("\n")
messages_request2 = mock_handler.messages[2].message.split("\n")
messages_response2 = mock_handler.messages[3].message.split("\n")
assert messages_request1[0] == "Request URL: 'http://localhost/'"
assert messages_request1[1] == "Request method: 'GET'"
assert messages_request1[2] == 'Request headers:'
@@ -115,8 +115,8 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 2
messages_request = mock_handler.messages[0].message.split("/n")
messages_response = mock_handler.messages[1].message.split("/n")
messages_request = mock_handler.messages[0].message.split("\n")
messages_response = mock_handler.messages[1].message.split("\n")
assert messages_request[0] == "Request URL: 'http://localhost/?country=france&city=REDACTED'"
assert messages_request[1] == "Request method: 'GET'"
assert messages_request[2] == "Request headers:"
@@ -177,8 +177,8 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 2
messages_request = mock_handler.messages[0].message.split("/n")
messages_response = mock_handler.messages[1].message.split("/n")
messages_request = mock_handler.messages[0].message.split("\n")
messages_response = mock_handler.messages[1].message.split("\n")
assert messages_request[0] == "Request URL: 'http://localhost/'"
assert messages_request[1] == "Request method: 'GET'"
assert messages_request[2] == 'Request headers:'
@@ -202,10 +202,10 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 4
messages_request1 = mock_handler.messages[0].message.split("/n")
messages_response1 = mock_handler.messages[1].message.split("/n")
messages_request2 = mock_handler.messages[2].message.split("/n")
messages_response2 = mock_handler.messages[3].message.split("/n")
messages_request1 = mock_handler.messages[0].message.split("\n")
messages_response1 = mock_handler.messages[1].message.split("\n")
messages_request2 = mock_handler.messages[2].message.split("\n")
messages_response2 = mock_handler.messages[3].message.split("\n")
assert messages_request1[0] == "Request URL: 'http://localhost/'"
assert messages_request1[1] == "Request method: 'GET'"
assert messages_request1[2] == 'Request headers:'
@@ -253,8 +253,8 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 2
messages_request = mock_handler.messages[0].message.split("/n")
messages_response = mock_handler.messages[1].message.split("/n")
messages_request = mock_handler.messages[0].message.split("\n")
messages_response = mock_handler.messages[1].message.split("\n")
assert messages_request[0] == "Request URL: 'http://localhost/'"
assert messages_request[1] == "Request method: 'GET'"
assert messages_request[2] == 'Request headers:'
@@ -298,8 +298,8 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 2
messages_request = mock_handler.messages[0].message.split("/n")
messages_response = mock_handler.messages[1].message.split("/n")
messages_request = mock_handler.messages[0].message.split("\n")
messages_response = mock_handler.messages[1].message.split("\n")
assert messages_request[0] == "Request URL: 'http://localhost/'"
assert messages_request[1] == "Request method: 'GET'"
assert messages_request[2] == 'Request headers:'
Original file line number Diff line number Diff line change
@@ -597,7 +597,10 @@ def test_logging_enabled(self, client, **kwargs):
for message in mock_handler.messages:
if message.levelname == "DEBUG" and message.funcName == "on_request":
# parts of the request are logged on new lines in a single message
request_sections = message.message.split("/n")
if "'/n" in message.message:
request_sections = message.message.split("/n")
else:
request_sections = message.message.split("\n")
for section in request_sections:
try:
# the body of the request should be JSON
@@ -627,7 +630,10 @@ def test_logging_disabled(self, client, **kwargs):
for message in mock_handler.messages:
if message.levelname == "DEBUG" and message.funcName == "on_request":
# parts of the request are logged on new lines in a single message
request_sections = message.message.split("/n")
if "'/n" in message.message:
request_sections = message.message.split("/n")
else:
request_sections = message.message.split("\n")
for section in request_sections:
try:
# the body of the request should be JSON
Original file line number Diff line number Diff line change
@@ -603,7 +603,10 @@ async def test_logging_enabled(self, client, **kwargs):
for message in mock_handler.messages:
if message.levelname == "DEBUG" and message.funcName == "on_request":
# parts of the request are logged on new lines in a single message
request_sections = message.message.split("/n")
if "'/n" in message.message:
request_sections = message.message.split("/n")
else:
request_sections = message.message.split("\n")
for section in request_sections:
try:
# the body of the request should be JSON
@@ -633,7 +636,10 @@ async def test_logging_disabled(self, client, **kwargs):
for message in mock_handler.messages:
if message.levelname == "DEBUG" and message.funcName == "on_request":
# parts of the request are logged on new lines in a single message
request_sections = message.message.split("/n")
if "'/n" in message.message:
request_sections = message.message.split("/n")
else:
request_sections = message.message.split("\n")
for section in request_sections:
try:
# the body of the request should be JSON
Loading

0 comments on commit 3f0c457

Please sign in to comment.