Skip to content

Commit 9753343

Browse files
martinRenougoelakash
authored andcommitted
Using logging instead of prints
1 parent a9ac311 commit 9753343

File tree

12 files changed

+190
-177
lines changed

12 files changed

+190
-177
lines changed

src/sagemaker/base_predictor.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from __future__ import print_function, absolute_import
1515

1616
import abc
17+
import logging
1718
from typing import Any, Tuple
1819

1920
from sagemaker.deprecations import (
@@ -54,6 +55,9 @@
5455
from sagemaker.lineage.context import EndpointContext
5556

5657

58+
logger = logging.getLogger(__name__)
59+
60+
5761
class PredictorBase(abc.ABC):
5862
"""An object that encapsulates a deployed model."""
5963

@@ -452,7 +456,7 @@ def list_monitors(self):
452456
endpoint_name=self.endpoint_name
453457
)
454458
if len(monitoring_schedules_dict["MonitoringScheduleSummaries"]) == 0:
455-
print("No monitors found for endpoint. endpoint: {}".format(self.endpoint_name))
459+
logger.debug("No monitors found for endpoint. endpoint: %s", self.endpoint_name)
456460
return []
457461

458462
monitors = []

src/sagemaker/cli/compatibility/v2/files.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
from sagemaker.cli.compatibility.v2.ast_transformer import ASTTransformer
2424

25-
LOGGER = logging.getLogger(__name__)
25+
# Setting LOGGER for backward compatibility, in case users import this...
26+
logger = LOGGER = logging.getLogger(__name__)
2627

2728

2829
class FileUpdater(object):
@@ -59,7 +60,7 @@ def _make_output_dirs_if_needed(self):
5960
os.makedirs(output_dir)
6061

6162
if os.path.exists(self.output_path):
62-
LOGGER.warning("Overwriting file %s", self.output_path)
63+
logger.warning("Overwriting file %s", self.output_path)
6364

6465

6566
class PyFileUpdater(FileUpdater):

src/sagemaker/fw_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ def tar_and_upload_dir(
474474
if s3_resource is None:
475475
s3_resource = session.resource("s3", region_name=session.region_name)
476476
else:
477-
print("Using provided s3_resource")
477+
logger.debug("Using provided s3_resource")
478478

479479
s3_resource.Object(bucket, key).upload_file(tar_file, ExtraArgs=extra_args)
480480
finally:

src/sagemaker/inference_recommender/inference_recommender_mixin.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"mxnet": "MXNET",
2929
}
3030

31-
LOGGER = logging.getLogger("sagemaker")
31+
# Setting LOGGER for backward compatibility, in case users import it...
32+
logger = LOGGER = logging.getLogger("sagemaker")
3233

3334

3435
class Phase:
@@ -145,10 +146,10 @@ def right_size(
145146
)
146147

147148
if endpoint_configurations or traffic_pattern or stopping_conditions or resource_limit:
148-
LOGGER.info("Advanced Job parameters were specified. Running Advanced job...")
149+
logger.info("Advanced Job parameters were specified. Running Advanced job...")
149150
job_type = "Advanced"
150151
else:
151-
LOGGER.info("Advanced Job parameters were not specified. Running Default job...")
152+
logger.info("Advanced Job parameters were not specified. Running Default job...")
152153
job_type = "Default"
153154

154155
self._init_sagemaker_session_if_does_not_exist()
@@ -173,7 +174,7 @@ def right_size(
173174
vpc_config=self.vpc_config,
174175
enable_network_isolation=self.enable_network_isolation(),
175176
)
176-
LOGGER.warning("Attempting to create new model with name %s", self.name)
177+
logger.warning("Attempting to create new model with name %s", self.name)
177178
self.sagemaker_session.create_model(**create_model_args)
178179

179180
ret_name = self.sagemaker_session.create_inference_recommendations_job(
@@ -281,23 +282,23 @@ def _update_params_for_right_size(
281282
if accelerator_type:
282283
raise ValueError("accelerator_type is not compatible with right_size().")
283284
if instance_type or initial_instance_count:
284-
LOGGER.warning(
285+
logger.warning(
285286
"instance_type or initial_instance_count specified."
286287
"Overriding right_size() recommendations."
287288
)
288289
return None
289290
if async_inference_config:
290-
LOGGER.warning(
291+
logger.warning(
291292
"async_inference_config is specified. Overriding right_size() recommendations."
292293
)
293294
return None
294295
if serverless_inference_config:
295-
LOGGER.warning(
296+
logger.warning(
296297
"serverless_inference_config is specified. Overriding right_size() recommendations."
297298
)
298299
return None
299300
if explainer_config:
300-
LOGGER.warning(
301+
logger.warning(
301302
"explainer_config is specified. Overriding right_size() recommendations."
302303
)
303304
return None
@@ -363,7 +364,7 @@ def _update_params_for_recommendation_id(
363364
"""
364365

365366
if instance_type is not None and initial_instance_count is not None:
366-
LOGGER.warning(
367+
logger.warning(
367368
"Both instance_type and initial_instance_count are specified,"
368369
"overriding the recommendation result."
369370
)

src/sagemaker/local/entities.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,10 @@ def start(self, **kwargs):
669669
execution = _LocalPipelineExecution(execution_id, self.pipeline, **kwargs)
670670

671671
self._executions[execution_id] = execution
672-
print(
673-
f"Starting execution for pipeline {self.pipeline.name}. Execution ID is {execution_id}"
672+
logger.info(
673+
"Starting execution for pipeline %s. Execution ID is %s",
674+
self.pipeline.name,
675+
execution_id,
674676
)
675677
self.last_modified_time = datetime.datetime.now().timestamp()
676678

@@ -733,31 +735,32 @@ def update_execution_success(self):
733735
"""Mark execution as succeeded."""
734736
self.status = _LocalExecutionStatus.SUCCEEDED.value
735737
self.last_modified_time = datetime.datetime.now().timestamp()
736-
print(f"Pipeline execution {self.pipeline_execution_name} SUCCEEDED")
738+
logger.info("Pipeline execution %s SUCCEEDED", self.pipeline_execution_name)
737739

738740
def update_execution_failure(self, step_name, failure_message):
739741
"""Mark execution as failed."""
740742
self.status = _LocalExecutionStatus.FAILED.value
741743
self.failure_reason = f"Step '{step_name}' failed with message: {failure_message}"
742744
self.last_modified_time = datetime.datetime.now().timestamp()
743-
print(
744-
f"Pipeline execution {self.pipeline_execution_name} FAILED because step "
745-
f"'{step_name}' failed."
745+
logger.info(
746+
"Pipeline execution %s FAILED because step '%s' failed.",
747+
self.pipeline_execution_name,
748+
step_name,
746749
)
747750

748751
def update_step_properties(self, step_name, step_properties):
749752
"""Update pipeline step execution output properties."""
750753
self.step_execution.get(step_name).update_step_properties(step_properties)
751-
print(f"Pipeline step '{step_name}' SUCCEEDED.")
754+
logger.info("Pipeline step '%s' SUCCEEDED.", step_name)
752755

753756
def update_step_failure(self, step_name, failure_message):
754757
"""Mark step_name as failed."""
755-
print(f"Pipeline step '{step_name}' FAILED. Failure message is: {failure_message}")
758+
logger.info("Pipeline step '%s' FAILED. Failure message is: %s", step_name, failure_message)
756759
self.step_execution.get(step_name).update_step_failure(failure_message)
757760

758761
def mark_step_executing(self, step_name):
759762
"""Update pipelines step's status to EXECUTING and start_time to now."""
760-
print(f"Starting pipeline step: '{step_name}'")
763+
logger.info("Starting pipeline step: '%s'", step_name)
761764
self.step_execution.get(step_name).mark_step_executing()
762765

763766
def _initialize_step_execution(self, steps):

src/sagemaker/local/image.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def process(
230230

231231
# Print our Job Complete line to have a similar experience to training on SageMaker where
232232
# you see this line at the end.
233-
print("===== Job Complete =====")
233+
logger.info("===== Job Complete =====")
234234

235235
def train(self, input_data_config, output_data_config, hyperparameters, environment, job_name):
236236
"""Run a training job locally using docker-compose.
@@ -310,7 +310,7 @@ def train(self, input_data_config, output_data_config, hyperparameters, environm
310310

311311
# Print our Job Complete line to have a similar experience to training on SageMaker where
312312
# you see this line at the end.
313-
print("===== Job Complete =====")
313+
logger.info("===== Job Complete =====")
314314
return artifacts
315315

316316
def serve(self, model_dir, environment):

src/sagemaker/model.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@
7373
InferenceRecommenderMixin,
7474
)
7575

76-
LOGGER = logging.getLogger("sagemaker")
76+
# Setting LOGGER for backward compatibility, in case users import it...
77+
logger = LOGGER = logging.getLogger("sagemaker")
7778

7879
NEO_ALLOWED_FRAMEWORKS = set(
7980
["mxnet", "tensorflow", "keras", "pytorch", "onnx", "xgboost", "tflite"]
@@ -709,7 +710,7 @@ def _upload_code(self, key_prefix: str, repack: bool = False) -> None:
709710
script_name=os.path.basename(self.entry_point),
710711
)
711712

712-
LOGGER.info(
713+
logger.info(
713714
"Repacking model artifact (%s), script artifact "
714715
"(%s), and dependencies (%s) "
715716
"into single tar.gz file located at %s. "
@@ -1194,13 +1195,13 @@ def compile(
11941195
self.image_uri = job_status.get("InferenceImage", None)
11951196
self._is_compiled_model = True
11961197
else:
1197-
LOGGER.warning(
1198+
logger.warning(
11981199
"The instance type %s is not supported for deployment via SageMaker."
11991200
"Please deploy the model manually.",
12001201
target_instance_family,
12011202
)
12021203
else:
1203-
LOGGER.warning(
1204+
logger.warning(
12041205
"Devices described by Target Platform OS, Architecture and Accelerator are not"
12051206
"supported for deployment via SageMaker. Please deploy the model manually."
12061207
)
@@ -1387,7 +1388,7 @@ def deploy(
13871388
)
13881389

13891390
if instance_type and instance_type.startswith("ml.inf") and not self._is_compiled_model:
1390-
LOGGER.warning(
1391+
logger.warning(
13911392
"Your model is not compiled. Please compile your model before using Inferentia."
13921393
)
13931394

0 commit comments

Comments
 (0)