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

fix: Implement context.log function for invoke local command on Python environment. #5391

Merged

Conversation

@aki-webii
Copy link

commented Oct 13, 2018

What did you implement:

Closes #4544

How did you implement it:

Add context.log function into FakeLambdaContext which class is used while running a handler by 'invoke local' command.
I implement context.log function as redirect function to standard output.

How can we verify it:

You can reproduce this problem as blow.

Prepare the source code like this.

❯ cat handler.py
import json

def hello(event, context):
    body = {
        "message": "Go Serverless v1.0! Your function executed successfully!",
        "input": event
    }

    response = {
        "statusCode": 200,
        "body": json.dumps(body)
    }

    context.log('log function experimental invoking.')

    return response

and execute it by invoke local, then you can see the exception.

❯ sls invoke local -t -f hello
Traceback (most recent call last):
  File "/Users/aki/.nodebrew/node/v8.11.3/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/invoke.py", line 67, in <module>

    result = handler(input['event'], context)
  File "./handler.py", line 14, in hello
    context.log('log function experimental invoking.')
AttributeError: 'FakeLambdaContext' object has no attribute 'log'

But when you execute same command by the new implementation.

~/Projects/webii/serverless-4544-verify-python
❯ ../serverless/bin/serverless invoke local -t -f hello
log function experimental invoking.
{
    "body": "{\"input\": {}, \"message\": \"Go Serverless v1.0! Your function executed successfully!\"}",
    "statusCode": 200
}

log function experimental invoking is the output of context.log.
So now that function works correctly.

Todos:

  • Write tests
    • There are no test code for invoke.py, and I don't have the knowledge for creating new one. Sorry.
  • Write documentation
    • context.log function is not documented at the AWS official document, but it's implicitly existing in the real AWS Lambda environment. So because of that, I think this function shouldn't be written in the document of Serverless framework as well.
  • Fix linting errors
  • Make sure code coverage hasn't dropped
  • Provide verification config / commands / resources
  • Enable "Allow edits from maintainers" for this PR
  • Update the messages below

Is this ready for review?: YES
Is it a breaking change?: NO

@horike37 horike37 added this to the 1.33.0 milestone Oct 13, 2018
Copy link
Member

left a comment

@aki-webii
Thank you for your contribution!
Works great as expected 👍 LGTM 💯
merging...

@horike37 horike37 merged commit fd8ec20 into serverless:master Oct 13, 2018
3 checks passed
3 checks passed
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage remained the same at 91.087%
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.