Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions samcli/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def cli(ctx):
sam_cli_logger = logging.getLogger("samcli")
sam_cli_formatter = logging.Formatter("%(message)s")
lambda_builders_logger = logging.getLogger("aws_lambda_builders")
botocore_logger = logging.getLogger("botocore")

SamCliLogger.configure_logger(sam_cli_logger, sam_cli_formatter, logging.INFO)
SamCliLogger.configure_logger(lambda_builders_logger, sam_cli_formatter, logging.INFO)
SamCliLogger.configure_null_logger(botocore_logger)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this silence all logs, including debug logs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://docs.python.org/3.6/howto/logging.html#configuring-logging-for-a-library explains it. Basically it will prevent the library’s logged event being output to sys.stderr and is documented as "A do-nothing handler". This is also what boto3 is doing but botocore is not (for whatever reason).

From reading the docs, my understanding is yes.

19 changes: 19 additions & 0 deletions samcli/lib/utils/sam_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,22 @@ def configure_logger(logger, formatter, level):
logger.setLevel(level)
logger.propagate = False
logger.addHandler(log_stream_handler)

@staticmethod
def configure_null_logger(logger):
"""
Configure a Logger with a NullHandler

Useful for libraries that do not follow: https://docs.python.org/3.6/howto/logging.html#configuring-logging-for-a-library

Parameters
----------
logger logging.getLogger
Logger to configure

Returns
-------
None
"""
logger.propagate = False
logger.addHandler(logging.NullHandler())
10 changes: 10 additions & 0 deletions tests/unit/lib/utils/test_sam_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,13 @@ def test_configure_samcli_logger(self, logging_patch):
logger_mock.addHandler.assert_called_once_with(stream_handler_mock)
stream_handler_mock.setLevel.assert_called_once_with(2)
stream_handler_mock.setFormatter.assert_called_once_with(formatter_mock)

@patch("samcli.lib.utils.sam_logging.logging")
def test_configure_samcli_logger(self, logging_patch):
logger_mock = Mock()

SamCliLogger.configure_null_logger(logger_mock)

self.assertFalse(logger_mock.propagate)

logger_mock.addHandler.assert_called_once_with(logging_patch.NullHandler())