aws / sagemaker-python-sdk

A library for training and deploying machine learning models on Amazon SageMaker
https://sagemaker.readthedocs.io/
Apache License 2.0
2.11k stars 1.14k forks source link

Library logs to stdout on import #4387

Open derricw opened 10 months ago

derricw commented 10 months ago

Describe the bug

Logging to stdout is an undesirable default for a library, especially since tools that use the library will want to control what is written to stdout. For example, imagine a command line program that fetches records from FeatureStore or metadata about training runs and writes the data to stdout so that it can be parsed by other tools like grep or jq or awk.

This is most likely caused by this line.

To reproduce

import sagemaker

This writes to stdout:

>>> import sagemaker
sagemaker.config INFO - Not applying SDK defaults from location: /etc/xdg/sagemaker/config.yaml
sagemaker.config INFO - Not applying SDK defaults from location: /home/username/.config/sagemaker/config.yaml

Expected behavior I would expect this to be written to stderr so that it doesn't interfere with regular program output.

Screenshots or logs If applicable, add screenshots or logs to help explain your problem.

System information A description of your system. Please provide:

Additional context Add any other context about the problem here.

derricw commented 10 months ago

Workaround I used:

import logging

# have to do this before importing sagemaker otherwise it logs
# config info to stdout
logging.getLogger("sagemaker.config").addHandler(logging.NullHandler())
logging.getLogger("sagemaker").addHandler(logging.NullHandler())

import sagemaker