Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refactor logs command library #2862

Merged
merged 9 commits into from
May 13, 2021
Merged
19 changes: 4 additions & 15 deletions samcli/commands/logs/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,13 @@ def do_cli(function_name, stack_name, filter_pattern, tailing, start_time, end_t
filter_pattern=filter_pattern,
start_time=start_time,
end_time=end_time,
# output_file is not yet supported by CLI
output_file=None,
) as context:

if tailing:
events_iterable = context.fetcher.tail(
context.log_group_name, filter_pattern=context.filter_pattern, start=context.start_time
)
context.fetcher.tail(start_time=context.start_time, filter_pattern=context.filter_pattern)
else:
events_iterable = context.fetcher.fetch(
context.log_group_name,
context.fetcher.load_time_period(
start_time=context.start_time,
end_time=context.end_time,
filter_pattern=context.filter_pattern,
start=context.start_time,
end=context.end_time,
)

formatted_events = context.formatter.do_format(events_iterable)

for event in formatted_events:
# New line is not necessary. It is already in the log events sent by CloudWatch
click.echo(event, nl=False)
18 changes: 18 additions & 0 deletions samcli/commands/logs/console_consumers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Consumers that will print out events to console
"""

import click

from samcli.lib.observability.cw_logs.cw_log_event import CWLogEvent
from samcli.lib.observability.observability_info_puller import ObservabilityEventConsumer


class CWConsoleEventConsumer(ObservabilityEventConsumer[CWLogEvent]):
"""
Consumer implementation that will consume given event as outputting into console
"""

# pylint: disable=R0201
def consume(self, event: CWLogEvent):
click.echo(event.message, nl=False)
48 changes: 25 additions & 23 deletions samcli/commands/logs/logs_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
"""

import logging

import boto3
import botocore

from samcli.commands.exceptions import UserException
from samcli.lib.logs.fetcher import LogsFetcher
from samcli.lib.logs.formatter import LogsFormatter, LambdaLogMsgFormatters, JSONMsgFormatter, KeywordHighlighter
from samcli.lib.logs.provider import LogGroupProvider
from samcli.commands.logs.console_consumers import CWConsoleEventConsumer
from samcli.lib.observability.cw_logs.cw_log_formatters import (
CWColorizeErrorsFormatter,
CWJsonFormatter,
CWKeywordHighlighterFormatter,
CWPrettyPrintFormatter,
)
from samcli.lib.observability.cw_logs.cw_log_group_provider import LogGroupProvider
from samcli.lib.observability.cw_logs.cw_log_puller import CWLogPuller
from samcli.lib.observability.observability_info_puller import ObservabilityEventConsumerDecorator
from samcli.lib.utils.colors import Colored
from samcli.lib.utils.time import to_utc, parse_date

Expand Down Expand Up @@ -97,26 +105,20 @@ def __exit__(self, *args):

@property
def fetcher(self):
return LogsFetcher(self._logs_client)

@property
def formatter(self):
"""
Creates and returns a Formatter capable of nicely formatting Lambda function logs

Returns
-------
LogsFormatter
"""
formatter_chain = [
LambdaLogMsgFormatters.colorize_errors,
# Format JSON "before" highlighting the keywords. Otherwise, JSON will be invalid from all the
# ANSI color codes and fail to pretty print
JSONMsgFormatter.format_json,
KeywordHighlighter(self._filter_pattern).highlight_keywords,
]

return LogsFormatter(self.colored, formatter_chain)
return CWLogPuller(
logs_client=self._logs_client,
consumer=ObservabilityEventConsumerDecorator(
mappers=[
CWColorizeErrorsFormatter(self.colored),
CWJsonFormatter(),
CWKeywordHighlighterFormatter(self.colored, self._filter_pattern),
CWPrettyPrintFormatter(self.colored),
],
consumer=CWConsoleEventConsumer(),
),
cw_log_group=self.log_group_name,
resource_name=self._function_name,
)

@property
def start_time(self):
Expand Down
72 changes: 0 additions & 72 deletions samcli/lib/logs/event.py

This file was deleted.

145 changes: 0 additions & 145 deletions samcli/lib/logs/fetcher.py

This file was deleted.

Loading