Skip to content

Commit 8c7d9f5

Browse files
authored
optimize log to reduce useless logging (#42942)
1 parent 531710c commit 8c7d9f5

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

eng/tools/azure-sdk-tools/packaging_tools/generate_utils.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import os
44
import re
5+
import sys
56

67
try:
78
# py 311 adds this library natively
@@ -28,6 +29,10 @@
2829
from jinja2 import Environment, FileSystemLoader
2930

3031

32+
logging.basicConfig(
33+
stream=sys.stdout,
34+
format="[%(levelname)s] %(message)s",
35+
)
3136
_LOGGER = logging.getLogger(__name__)
3237
_SDK_FOLDER_RE = re.compile(r"^(sdk/[\w-]+)/(azure[\w-]+)/", re.ASCII)
3338

@@ -539,21 +544,28 @@ def gen_typespec(
539544
_LOGGER.info(f"generation cmd: {cmd}")
540545
output = check_output(cmd, stderr=STDOUT, shell=True)
541546
except CalledProcessError as e:
542-
_LOGGER.error("Error occurred when call tsp-client:")
543-
for item in e.output.decode("utf-8").split("\n"):
544-
if "Error: " in item:
545-
_LOGGER.error(item)
546-
_LOGGER.info(f"whole output when fail to call tsp-client: {e.output.decode('utf-8')}")
547-
raise e
547+
output = e.output.decode("utf-8")
548+
output_lines = output.split("\n")
549+
try:
550+
start_idx = [i for i, line in enumerate(output_lines) if "error stack start" in line][0]
551+
end_idx = [i for i, line in enumerate(output_lines) if "error stack end" in line][0]
552+
error_position = "python codegen"
553+
except:
554+
start_idx = -1
555+
end_idx = -1
556+
error_position = "tsp compiler"
548557

549-
decode_output = output.decode("utf-8")
550-
# before https://github.com/Azure/azure-sdk-tools/issues/8815, have to check output to judge whether sdk generation succeeds
551-
if " - error " in decode_output:
552-
_LOGGER.error(f"Failed to generate sdk from typespec:")
553-
for item in decode_output.split("\n"):
554-
if " - error " in item:
558+
_LOGGER.error(f"====== Error occurred in {error_position} (error stack start) ======")
559+
if start_idx != -1 and end_idx != -1:
560+
for item in output_lines[start_idx + 1 : end_idx]:
555561
_LOGGER.error(item)
556-
raise Exception(f"Complete output when fail to generate sdk from typespec: {decode_output}")
562+
else:
563+
for item in output_lines:
564+
if "- error " in item:
565+
_LOGGER.error(item)
566+
_LOGGER.error(f"====== Error occurred in {error_position} (error stack end) ======")
567+
568+
raise e
557569

558570
with open(Path("eng/emitter-package.json"), "r") as file_in:
559571
data = json.load(file_in)

eng/tools/azure-sdk-tools/packaging_tools/sdk_generator.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343

4444
logging.basicConfig(
4545
stream=sys.stdout,
46-
format="%(asctime)s [%(levelname)s] %(message)s",
47-
datefmt="%Y-%m-%d %X",
46+
format="[%(levelname)s] %(message)s",
4847
)
4948
_LOGGER = logging.getLogger(__name__)
5049

0 commit comments

Comments
 (0)