Skip to content

Commit

Permalink
chore: Make logger name into shared constants (aws#1773)
Browse files Browse the repository at this point in the history
  • Loading branch information
aahung committed Oct 10, 2020
1 parent cf88a49 commit 4c6ac99
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
8 changes: 6 additions & 2 deletions samcli/cli/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
import click

from samcli.commands.exceptions import CredentialsError
from samcli.lib.utils.sam_logging import SAM_CLI_FORMATTER_WITH_TIMESTAMP
from samcli.lib.utils.sam_logging import (
SAM_CLI_FORMATTER_WITH_TIMESTAMP,
SAM_CLI_LOGGER_NAME,
LAMBDA_BULDERS_LOGGER_NAME,
)


class Context:
Expand Down Expand Up @@ -52,7 +56,7 @@ def debug(self, value):

if self._debug:
# Turn on debug logging and display timestamps
for logger_name in ["samcli", "aws_lambda_builders"]:
for logger_name in [SAM_CLI_LOGGER_NAME, LAMBDA_BULDERS_LOGGER_NAME]:
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG)
for handler in logger.handlers:
Expand Down
11 changes: 8 additions & 3 deletions samcli/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@

from samcli import __version__
from samcli.lib.telemetry.metrics import send_installed_metric
from samcli.lib.utils.sam_logging import SamCliLogger, SAM_CLI_FORMATTER
from samcli.lib.utils.sam_logging import (
SamCliLogger,
SAM_CLI_FORMATTER,
SAM_CLI_LOGGER_NAME,
LAMBDA_BULDERS_LOGGER_NAME,
)
from .options import debug_option, region_option, profile_option
from .context import Context
from .command import BaseCommand
Expand Down Expand Up @@ -94,8 +99,8 @@ def cli(ctx):
except (IOError, ValueError) as ex:
LOG.debug("Unable to write telemetry flag", exc_info=ex)

sam_cli_logger = logging.getLogger("samcli")
lambda_builders_logger = logging.getLogger("aws_lambda_builders")
sam_cli_logger = logging.getLogger(SAM_CLI_LOGGER_NAME)
lambda_builders_logger = logging.getLogger(LAMBDA_BULDERS_LOGGER_NAME)
botocore_logger = logging.getLogger("botocore")

SamCliLogger.configure_logger(sam_cli_logger, SAM_CLI_FORMATTER, logging.INFO)
Expand Down
3 changes: 3 additions & 0 deletions samcli/lib/utils/sam_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
SAM_CLI_FORMATTER = logging.Formatter("%(message)s")
SAM_CLI_FORMATTER_WITH_TIMESTAMP = logging.Formatter("%(asctime)s | %(message)s")

SAM_CLI_LOGGER_NAME = "samcli"
LAMBDA_BULDERS_LOGGER_NAME = "aws_lambda_builders"


class SamCliLogger:
@staticmethod
Expand Down
19 changes: 13 additions & 6 deletions tests/unit/cli/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
from unittest.mock import patch, ANY

from samcli.cli.context import Context
from samcli.lib.utils.sam_logging import SamCliLogger, SAM_CLI_FORMATTER
from samcli.lib.utils.sam_logging import (
SamCliLogger,
SAM_CLI_FORMATTER,
SAM_CLI_LOGGER_NAME,
LAMBDA_BULDERS_LOGGER_NAME,
)


class TestContext(TestCase):
Expand All @@ -21,21 +26,23 @@ def test_must_set_get_debug_flag(self):

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

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("samcli")
lambda_builders_logger = logging.getLogger("aws_lambda_builders")
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("samcli").handlers + logging.getLogger("aws_lambda_builders").handlers
handlers = (
logging.getLogger(SAM_CLI_LOGGER_NAME).handlers + logging.getLogger(LAMBDA_BULDERS_LOGGER_NAME).handlers
)
# timestamp should not be output
for handler in handlers:
self.assertNotRegex(handler.formatter.format(message_record), timestamp_log_regex)
Expand Down

0 comments on commit 4c6ac99

Please sign in to comment.