Ejemplo n.º 1
0
def cli(ctx):
    """
    AWS Serverless Application Model (SAM) CLI

    The AWS Serverless Application Model extends AWS CloudFormation to provide a simplified way of defining the
    Amazon API Gateway APIs, AWS Lambda functions, and Amazon DynamoDB tables needed by your serverless application.
    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

        try:
            global_cfg.telemetry_enabled = enabled

            if enabled:
                click.secho(TELEMETRY_PROMPT, fg="yellow", err=True)

                # When the Telemetry prompt is printed, we can safely assume that this is the first time someone
                # is installing SAM CLI on this computer. So go ahead and send the `installed` metric
                send_installed_metric()

        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)
Ejemplo n.º 2
0
    def test_must_unset_get_debug_flag(self):
        ctx = Context()

        message_record = logging.makeLogRecord({"msg": "hello world"})
        timestamp_log_regex = re.compile(r"^[0-9:\- ,]+ \| .*$")

        sam_cli_logger = logging.getLogger(SAM_CLI_LOGGER_NAME)
        lambda_builders_logger = logging.getLogger(LAMBDA_BULDERS_LOGGER_NAME)
        SamCliLogger.configure_logger(sam_cli_logger, SAM_CLI_FORMATTER,
                                      logging.INFO)
        SamCliLogger.configure_logger(lambda_builders_logger,
                                      SAM_CLI_FORMATTER, logging.INFO)

        handlers = [
            logging.getLogger(SAM_CLI_LOGGER_NAME).handlers[0],
            logging.getLogger(LAMBDA_BULDERS_LOGGER_NAME).handlers[0],
        ]
        # timestamp should not be output
        for handler in handlers:
            self.assertNotRegex(handler.formatter.format(message_record),
                                timestamp_log_regex)

        ctx.debug = True
        self.assertEqual(ctx.debug, True, "debug must be set to True")
        # timestamp should be output
        for handler in handlers:
            self.assertRegex(
                handler.formatter.format(message_record),
                timestamp_log_regex,
                "debug log record should contain timestamps",
            )

        # Flipping from True to False
        ctx.debug = False
        self.assertEqual(ctx.debug, False, "debug must be set to False")
Ejemplo n.º 3
0
    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())
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    def debug(self, value):
        """
        Turn on debug logging if necessary.

        :param value: Value of debug flag
        """
        self._debug = value

        if self._debug:
            # Turn on debug logging and display timestamps
            sam_cli_logger = logging.getLogger(SAM_CLI_LOGGER_NAME)
            lambda_builders_logger = logging.getLogger(
                LAMBDA_BULDERS_LOGGER_NAME)
            SamCliLogger.configure_logger(sam_cli_logger,
                                          SAM_CLI_FORMATTER_WITH_TIMESTAMP,
                                          logging.DEBUG)
            SamCliLogger.configure_logger(lambda_builders_logger,
                                          SAM_CLI_FORMATTER_WITH_TIMESTAMP,
                                          logging.DEBUG)