# Buggy code
def acr_build_task_logs(
    cmd,
    client,  # cf_acr_builds
    registry_name,
    build_id=None,
    build_task_name=None,
    image=None,
    resource_group_name=None,
):
    _, resource_group_name = validate_managed_registry(
        cmd.cli_ctx, registry_name, resource_group_name, BUILD_TASKS_NOT_SUPPORTED
    )

    if not build_id:
        # show logs for the last build
        paged_builds = acr_build_task_list_builds(
            cmd,
            client,
            registry_name,
            top=1,
            build_task_name=build_task_name,
            image=image,
        )
        try:
            build_id = paged_builds.get(0)[0].build_id
            logger.warning(
                _get_list_builds_message(
                    base_message="Showing logs of the last created build",
                    build_task_name=build_task_name,
                    image=image,
                )
            )
            logger.warning("Build ID: %s", build_id)
        except (AttributeError, KeyError, TypeError, IndexError):
            raise CLIError(
                _get_list_builds_message(
                    base_message="Could not find the last created build",
                    build_task_name=build_task_name,
                    image=image,
                )
            )

    return acr_build_show_logs(client, build_id, registry_name, resource_group_name)


# Traceback (UnicodeEncodeError)
az acr build -r myreg --no-push false -t myreg.azurecr.io/repo/myapp:master -f ./Dockerfile .
Sending build context (43.398 KiB) to ACR.
Queued a build with build ID: aah
Waiting for a build agent...
time="2018-05-22T21:11:59Z" level=info msg="Running command docker login -u 00000000-0000-0000-0000-000000000000 --password-stdin myreg.azurecr.io"
Login Succeeded
time="2018-05-22T21:12:01Z" level=info msg="Untarring to /root/acr-builder/src"
time="2018-05-22T21:12:01Z" level=info msg="Running command docker build --pull -f ./Dockerfile -t myreg.azurecr.io/repo/myapp:master ."
Sending build context to Docker daemon  220.2kB

Step 1/8 : FROM node:carbon
carbon: Pulling from library/node
3d77ce4481b1: Already exists
534514c83d69: Already exists
d562b1c3ac3f: Already exists
4b85e68dc01d: Already exists
f6a66c5de9db: Already exists
7a4e7d9a081d: Already exists
49f75af57b99: Pulling fs layer
454abe4fb374: Pulling fs layer
454abe4fb374: Verifying Checksum
454abe4fb374: Download complete
49f75af57b99: Verifying Checksum
49f75af57b99: Download complete
49f75af57b99: Pull complete
454abe4fb374: Pull complete
Digest: sha256:19efc0ee886057870334b4b4b4a505ea8a158d135f592cc367ab8840b684f6c0
Status: Downloaded newer image for node:carbon
---> 037acc6a00bc
Step 2/8 : WORKDIR /usr/src/app
Removing intermediate container ade8e4293757
---> 5fa54edc3ae3
Step 3/8 : COPY package*.json ./
---> 295c9eeaf7fc
Step 4/8 : RUN npm install
---> Running in 6846edad6a09
npm WARN deprecatedERROR: 'ascii' codec can't encode character u'\U0001f64c' in position 29: ordinal not in range(128)
Traceback (most recent call last):
File "/usr/lib64/az/lib/python2.7/site-packages/knack/cli.py", line 197, in invoke
cmd_result = self.invocation.execute(args)
File "/usr/lib64/az/lib/python2.7/site-packages/azure/cli/core/commands/__init__.py", line 332, in execute
six.reraise(*sys.exc_info())
File "/usr/lib64/az/lib/python2.7/site-packages/azure/cli/core/commands/__init__.py", line 306, in execute
result = cmd(params)
File "/usr/lib64/az/lib/python2.7/site-packages/azure/cli/core/commands/__init__.py", line 167, in __call__
return super(AzCliCommand, self).__call__(*args, **kwargs)
File "/usr/lib64/az/lib/python2.7/site-packages/knack/commands.py", line 109, in __call__
return self.handler(*args, **kwargs)
File "/usr/lib64/az/lib/python2.7/site-packages/azure/cli/core/__init__.py", line 420, in default_command_handler
result = op(**command_args)
File "/usr/lib64/az/lib/python2.7/site-packages/azure/cli/command_modules/acr/build.py", line 330, in acr_build
return acr_build_show_logs(client, build_id, registry_name, resource_group_name, True)
File "/usr/lib64/az/lib/python2.7/site-packages/azure/cli/command_modules/acr/build.py", line 71, in acr_build_show_logs
raise_error_on_failure=raise_error_on_failure)
File "/usr/lib64/az/lib/python2.7/site-packages/azure/cli/command_modules/acr/build.py", line 132, in _stream_logs
print(flush.decode('utf-8', errors='ignore'))
File "/usr/lib64/az/lib/python2.7/site-packages/colorama/ansitowin32.py", line 40, in write
self.__convertor.write(text)
File "/usr/lib64/az/lib/python2.7/site-packages/colorama/ansitowin32.py", line 141, in write
self.write_and_convert(text)
File "/usr/lib64/az/lib/python2.7/site-packages/colorama/ansitowin32.py", line 166, in write_and_convert
self.write_plain_text(text, cursor, start)
File "/usr/lib64/az/lib/python2.7/site-packages/colorama/ansitowin32.py", line 174, in write_plain_text
self.wrapped.write(text[start:end])
UnicodeEncodeError: 'ascii' codec can't encode character u'\U0001f64c' in position 29: ordinal not in range(128)

# Fixed code
def acr_build_task_logs(
    cmd,
    client,  # cf_acr_builds
    registry_name,
    build_id=None,
    build_task_name=None,
    image=None,
    no_format=False,
    resource_group_name=None,
):
    _, resource_group_name = validate_managed_registry(
        cmd.cli_ctx, registry_name, resource_group_name, BUILD_TASKS_NOT_SUPPORTED
    )

    if not build_id:
        # show logs for the last build
        paged_builds = acr_build_task_list_builds(
            cmd,
            client,
            registry_name,
            top=1,
            build_task_name=build_task_name,
            image=image,
        )
        try:
            build_id = paged_builds.get(0)[0].build_id
            logger.warning(
                _get_list_builds_message(
                    base_message="Showing logs of the last created build",
                    build_task_name=build_task_name,
                    image=image,
                )
            )
            logger.warning("Build ID: %s", build_id)
        except (AttributeError, KeyError, TypeError, IndexError):
            raise CLIError(
                _get_list_builds_message(
                    base_message="Could not find the last created build",
                    build_task_name=build_task_name,
                    image=image,
                )
            )

    return acr_build_show_logs(
        client, build_id, registry_name, resource_group_name, no_format
    )