How to use retrieve_logs method in autotest

Best Python code snippet using autotest_python

logs.py

Source:logs.py Github

copy

Full Screen

...7from chalice.awsclient import TypedAWSClient # noqa8def display_logs(retriever, max_entries,9 include_lambda_messages, stream):10 # type: (LogRetriever, int, bool, IO[str]) -> None11 events = retriever.retrieve_logs(12 include_lambda_messages=include_lambda_messages,13 max_entries=max_entries)14 for event in events:15 stream.write('%s %s %s\n' % (16 event['timestamp'],17 event['logShortId'],18 event['message'].strip()))19class LogRetriever(object):20 def __init__(self, client, log_group_name):21 # type: (TypedAWSClient, str) -> None22 self._client = client23 self._log_group_name = log_group_name24 @classmethod25 def create_from_arn(cls, client, lambda_arn):26 # type: (TypedAWSClient, str) -> LogRetriever27 """Create a LogRetriever from a client and lambda arn.28 :type client: botocore.client.Logs29 :param client: A ``logs`` client.30 :type lambda_arn: str31 :param lambda_arn: The ARN of the lambda function.32 :return: An instance of ``LogRetriever``.33 """34 lambda_name = lambda_arn.split(':')[6]35 log_group_name = '/aws/lambda/%s' % lambda_name36 return cls(client, log_group_name)37 def _is_lambda_message(self, event):38 # type: (Dict[str, Any]) -> bool39 # Lambda will also inject log messages into your log streams.40 # They look like:41 # START RequestId: guid Version: $LATEST42 # END RequestId: guid43 # REPORT RequestId: guid Duration: 0.35 ms Billed Duration: ...44 # By default, these message are included in retrieve_logs().45 # But you can also request that retrieve_logs() filter out46 # these message so that we only include log messages generated47 # by your chalice app.48 msg = event['message'].strip()49 return msg.startswith(('START RequestId',50 'END RequestId',51 'REPORT RequestId'))52 def retrieve_logs(self, include_lambda_messages=True, max_entries=None):53 # type: (bool, Optional[int]) -> Iterator[Dict[str, Any]]54 """Retrieve logs from a log group.55 :type include_lambda_messages: boolean56 :param include_lambda_messages: Include logs generated by the AWS57 Lambda service. If this value is False, only chalice logs will be58 included.59 :type max_entries: int60 :param max_entries: Maximum number of log messages to include.61 :rtype: iterator62 :return: An iterator that yields event dicts. Each event63 dict has these keys:64 * logStreamName -> (string) The name of the log stream.65 * timestamp -> (datetime.datetime) - The timestamp for the msg.66 * message -> (string) The data contained in the log event....

Full Screen

Full Screen

6138_test_logs.py

Source:6138_test_logs.py Github

copy

Full Screen

...11 client = mock.Mock(spec=TypedAWSClient)12 log_message = message('first')13 client.iter_log_events.return_value = [log_message]14 retriever = logs.LogRetriever(client, 'loggroup')15 messages = list(retriever.retrieve_logs())16 expected = log_message.copy()17 # We also inject a logShortId.18 expected['logShortId'] = 'logStreamName'19 assert messages == [expected]20def test_can_support_max_entries():21 client = mock.Mock(spec=TypedAWSClient)22 client.iter_log_events.return_value = [message('first'), message('second')]23 retriever = logs.LogRetriever(client, 'loggroup')24 messages = list(retriever.retrieve_logs(max_entries=1))25 assert len(messages) == 126 assert messages[0]['message'] == 'first'27def test_can_exclude_lambda_messages():28 client = mock.Mock(spec=TypedAWSClient)29 client.iter_log_events.return_value = [30 message('START RequestId: id Version: $LATEST'),31 message('END RequestId: id'),32 message('REPORT RequestId: id Duration: 0.42 ms '33 'Billed Duration: 100 ms '34 'Memory Size: 128 MB Max Memory Used: 19 MB'),35 message('Not a lambda message'),36 ]37 retriever = logs.LogRetriever(client, 'loggroup')38 messages = list(retriever.retrieve_logs(include_lambda_messages=False))39 assert len(messages) == 140 assert messages[0]['message'] == 'Not a lambda message'41def test_can_parse_short_id():42 log_message = message(43 'Log Message',44 '2017/04/28/[$LATEST]fc219a0d613b40e9b5c58e6b8fd2320c'45 )46 client = mock.Mock(spec=TypedAWSClient)47 client.iter_log_events.return_value = [log_message]48 retriever = logs.LogRetriever(client, 'loggroup')49 messages = list(retriever.retrieve_logs(include_lambda_messages=False))50 assert len(messages) == 151 assert messages[0]['logShortId'] == 'fc219a'52def test_can_create_from_arn():53 retriever = logs.LogRetriever.create_from_arn(54 mock.sentinel.client,55 'arn:aws:lambda:us-east-1:123:function:my-function'56 )57 assert isinstance(retriever, logs.LogRetriever)58def test_can_display_logs():59 retriever = mock.Mock(spec=logs.LogRetriever)60 retriever.retrieve_logs.return_value = [61 {'timestamp': 'NOW', 'logShortId': 'shortId', 'message': 'One'},62 {'timestamp': 'NOW', 'logShortId': 'shortId', 'message': 'Two'},63 {'timestamp': 'NOW', 'logShortId': 'shortId', 'message': 'Three'},...

Full Screen

Full Screen

test_logs.py

Source:test_logs.py Github

copy

Full Screen

...11 client = mock.Mock(spec=TypedAWSClient)12 log_message = message('first')13 client.iter_log_events.return_value = [log_message]14 retriever = logs.LogRetriever(client, 'loggroup')15 messages = list(retriever.retrieve_logs())16 expected = log_message.copy()17 # We also inject a logShortId.18 expected['logShortId'] = 'logStreamName'19 assert messages == [expected]20def test_can_support_max_entries():21 client = mock.Mock(spec=TypedAWSClient)22 client.iter_log_events.return_value = [message('first'), message('second')]23 retriever = logs.LogRetriever(client, 'loggroup')24 messages = list(retriever.retrieve_logs(max_entries=1))25 assert len(messages) == 126 assert messages[0]['message'] == 'first'27def test_can_exclude_lambda_messages():28 client = mock.Mock(spec=TypedAWSClient)29 client.iter_log_events.return_value = [30 message('START RequestId: id Version: $LATEST'),31 message('END RequestId: id'),32 message('REPORT RequestId: id Duration: 0.42 ms '33 'Billed Duration: 100 ms '34 'Memory Size: 128 MB Max Memory Used: 19 MB'),35 message('Not a lambda message'),36 ]37 retriever = logs.LogRetriever(client, 'loggroup')38 messages = list(retriever.retrieve_logs(include_lambda_messages=False))39 assert len(messages) == 140 assert messages[0]['message'] == 'Not a lambda message'41def test_can_parse_short_id():42 log_message = message(43 'Log Message',44 '2017/04/28/[$LATEST]fc219a0d613b40e9b5c58e6b8fd2320c'45 )46 client = mock.Mock(spec=TypedAWSClient)47 client.iter_log_events.return_value = [log_message]48 retriever = logs.LogRetriever(client, 'loggroup')49 messages = list(retriever.retrieve_logs(include_lambda_messages=False))50 assert len(messages) == 151 assert messages[0]['logShortId'] == 'fc219a'52def test_can_create_from_arn():53 retriever = logs.LogRetriever.create_from_arn(54 mock.sentinel.client,55 'arn:aws:lambda:us-east-1:123:function:my-function'56 )57 assert isinstance(retriever, logs.LogRetriever)58def test_can_display_logs():59 retriever = mock.Mock(spec=logs.LogRetriever)60 retriever.retrieve_logs.return_value = [61 {'timestamp': 'NOW', 'logShortId': 'shortId', 'message': 'One'},62 {'timestamp': 'NOW', 'logShortId': 'shortId', 'message': 'Two'},63 {'timestamp': 'NOW', 'logShortId': 'shortId', 'message': 'Three'},...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful