Skip to content

Commit

Permalink
feat(cli-logs): Configure and format SAM CLI logging to console (#1118)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfuss committed Jul 26, 2019
1 parent 879cad4 commit fb06abe
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
3 changes: 2 additions & 1 deletion samcli/cli/context.py
Expand Up @@ -45,7 +45,8 @@ def debug(self, value):

if self._debug:
# Turn on debug logging
logging.getLogger().setLevel(logging.DEBUG)
logging.getLogger('samcli').setLevel(logging.DEBUG)
logging.getLogger('aws_lambda_builders').setLevel(logging.DEBUG)

@property
def region(self):
Expand Down
9 changes: 8 additions & 1 deletion samcli/cli/main.py
Expand Up @@ -8,6 +8,7 @@

from samcli import __version__
from samcli.lib.telemetry.metrics import send_installed_metric
from samcli.lib.utils.sam_logging import SamCliLogger
from .options import debug_option, region_option, profile_option
from .context import Context
from .command import BaseCommand
Expand Down Expand Up @@ -79,7 +80,6 @@ def cli(ctx):
You can find more in-depth guide about the SAM specification here:
https://github.com/awslabs/serverless-application-model.
"""

if global_cfg.telemetry_enabled is None:
enabled = True

Expand All @@ -95,3 +95,10 @@ def cli(ctx):

except (IOError, ValueError) as ex:
LOG.debug("Unable to write telemetry flag", exc_info=ex)

sam_cli_logger = logging.getLogger('samcli')
sam_cli_formatter = logging.Formatter('%(message)s')
lambda_builders_logger = logging.getLogger('aws_lambda_builders')

SamCliLogger.configure_logger(sam_cli_logger, sam_cli_formatter, logging.INFO)
SamCliLogger.configure_logger(lambda_builders_logger, sam_cli_formatter, logging.INFO)
31 changes: 31 additions & 0 deletions samcli/lib/utils/sam_logging.py
@@ -0,0 +1,31 @@
"""
Configures a logger
"""
import logging


class SamCliLogger(object):

@staticmethod
def configure_logger(logger, formatter, level):
"""
Configure a Logger with the formatter provided.
Parameters
----------
logger logging.getLogger
Logger to configure
formatter logging.formatter
Formatter for the logger
Returns
-------
None
"""
log_stream_handler = logging.StreamHandler()
log_stream_handler.setLevel(logging.DEBUG)
log_stream_handler.setFormatter(formatter)

logger.setLevel(level)
logger.propagate = False
logger.addHandler(log_stream_handler)
3 changes: 2 additions & 1 deletion tests/unit/cli/test_context.py
Expand Up @@ -19,7 +19,8 @@ def test_must_set_get_debug_flag(self):

ctx.debug = True
self.assertEquals(ctx.debug, True, "debug must be set to True")
self.assertEquals(logging.getLogger().getEffectiveLevel(), logging.DEBUG)
self.assertEquals(logging.getLogger('samcli').getEffectiveLevel(), logging.DEBUG)
self.assertEquals(logging.getLogger('aws_lambda_builders').getEffectiveLevel(), logging.DEBUG)

def test_must_unset_get_debug_flag(self):
ctx = Context()
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/lib/utils/test_sam_logging.py
@@ -0,0 +1,25 @@
from unittest import TestCase
from mock import patch, Mock

from samcli.lib.utils.sam_logging import SamCliLogger


class TestSamCliLogger(TestCase):

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

stream_handler_mock = Mock()
logging_patch.StreamHandler.return_value = stream_handler_mock

SamCliLogger.configure_logger(logger_mock, formatter_mock, level=1)

self.assertFalse(logger_mock.propagate)

logger_mock.setLevel.assert_called_once_with(1)
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)

0 comments on commit fb06abe

Please sign in to comment.