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

fix: Fix missing encoding when logging from Makefile #535

Merged
merged 3 commits into from
Aug 30, 2023
Merged
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
13 changes: 11 additions & 2 deletions aws_lambda_builders/workflows/custom_make/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io
import logging
import shutil
import sys
import threading

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -92,9 +93,17 @@ def run(self, args, env=None, cwd=None):

# Log every stdout line by iterating
for line in p.stdout:
decoded_line = line.decode("utf-8").strip()
LOG.info(decoded_line)
# Writing to stderr instead of using LOG.info
# since the logger library does not include ANSI
# formatting characters in the output
#
# stderr is used since stdout appears to be reserved
# for command responses
sys.stderr.buffer.write(line)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, sys.stderr.buffer.write won't include a newline at the end of the string which might make the next log statement messy. Should we add a newline after?

Copy link
Contributor Author

@lucashuy lucashuy Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ref:
https://docs.python.org/3/library/io.html#io.IOBase
https://docs.python.org/3/library/io.html#io.IOBase.readline

For the line that gets returned by the iterator (for line in stdout), it calls readline() under the hood and there should always be a new line character at the end of each line. So here, we should be fine to not include a new line of our own.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for digging in!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just can you add a comment why we used sys.stderr to log the terraform output instead of log.info.

sys.stderr.flush()

# Gather total stdout
decoded_line = line.decode("utf-8").strip()
stdout += decoded_line

# Wait for the process to exit and stderr thread to end.
Expand Down
Loading