Skip to content

Commit

Permalink
fix: override botocore logging to a NullHandler (#1538)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfuss authored and sriram-mv committed Nov 23, 2019
1 parent f0edb60 commit 9940d27
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
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)
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())

0 comments on commit 9940d27

Please sign in to comment.