Serverless-offline: Body disappears when using Python logging

Created on 29 Nov 2019  路  5Comments  路  Source: dherault/serverless-offline

Bug Report

Current Behavior
When adding a logger.info('Logging message') anywhere in my code, the request returns a response without a body. When I supress the logger.info calls, the body comes back.

Sample Code

  • file: serverless.yml
service: image-to-template-api
provider:
  name: aws
  runtime: python3.7
  region: ${env:REGION}
  stage: ${env:STAGE}
functions:
  process:
    handler: handler.process
    events:
      - http:
          path: process
          method: post
exclude:
  - node_modules/**
  - tests/**
plugins:
  - serverless-dotenv-plugin
  - serverless-python-requirements
  - serverless-offline
  • file: handler.js
import json
import os
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)

def process(event, context):
    logger.info('## ENVIRONMENT VARIABLES')
    logger.info(os.environ)
    logger.info('## EVENT')
    logger.info(event)
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

Expected behavior/code
The body is still returned after adding loggers.

Environment

  • serverless version: v1.58.0
  • serverless-offline version: 6.0.0-alpha.50
  • node.js version: v10.13.0
  • OS: macOS 10.15.1
  • python version: v3.7.3
bug python

Most helpful comment

@georgesbiaux @benjaminsims it seems we ignored stdout (with e.g. print()) from python other than the handler payload. python logging in turn went to stderr and that ignored the handler payload as well.

both should be [hopefully] fixed now.

All 5 comments

thank you for filing this bug report @georgesbiaux ! yes, there seems to be an issue with python and logging.

@benjaminsims follow up from: #851

entry point for any "runner" (node, python, ruby) here:
https://github.com/dherault/serverless-offline/blob/ccb64177eb5a4d2b2d3b6c1f05045c1462e0bea7/src/lambda/handler-runner/HandlerRunner.js#L60

invoking this: https://github.com/dherault/serverless-offline/blob/ccb64177eb5a4d2b2d3b6c1f05045c1462e0bea7/src/lambda/handler-runner/PythonRunner.js#L52

which runs: https://github.com/dherault/serverless-offline/blob/v6.0.0-alpha.52/src/lambda/handler-runner/invoke.py

as I mentioned, all of this is more or less based on the serverless local invoke implementation, I only rewrote it to use execa and some other small-ish changes.

to distinguish between payload I added: https://github.com/dherault/serverless-offline/blob/3d8c35a16d0bc83453a5473ba6a9c503ae294597/src/lambda/handler-runner/invoke.py#L94

let me know if you have any other questions. there's some preliminary setup steps here: https://github.com/dherault/serverless-offline/blob/master/CONTRIBUTING.md (probably needs to be improved as well.) don't hesitate to ask.

Using sls invoke local, both logging output and the actual intended output was printed to stdout.

This only occurs when using standard Python logging; using print() statements will not cause the response to be empty (however the printed text will not appear in the sls offline logs, though it will when using invoke local).

I also tried removing the logging.basicConfig() call from invoke.py, but this made no difference.

I assume it's something to do with loggers fighting over stdout, but I haven't yet been able to get the serverless-offline running in development mode to investigate further.

@georgesbiaux @benjaminsims it seems we ignored stdout (with e.g. print()) from python other than the handler payload. python logging in turn went to stderr and that ignored the handler payload as well.

both should be [hopefully] fixed now.

@dnalborczyk Seems fixed, thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

conradoramalho picture conradoramalho  路  3Comments

Dong9769 picture Dong9769  路  4Comments

MEGApixel23 picture MEGApixel23  路  4Comments

adambiggs picture adambiggs  路  4Comments

dnalborczyk picture dnalborczyk  路  3Comments