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

Log messages are neither printed in the console nor saved into log file #549

Closed
bigunyak opened this issue Jul 11, 2018 · 3 comments
Closed

Comments

@bigunyak
Copy link

In SAM v0.4.0 log messages logged with logging module (python 3.6) are neither shown in the console nor printed in the log file with the --log-file option.
Looks like a regression bug after migrating to python implementation as the same issue was reportedly fixed in #91 and #124
Trivial code to reproduce the issue is below.

import logging

logging.basicConfig(level=logging.INFO, format='[%(asctime)s] [%(levelname)s] %(message)s')
log = logging.getLogger(__name__)

def handler(event, context):
    log.info("Received S3 event: " + json.dumps(event, indent=2))
    print("TESTTTTTTTTTTTTTTTTTT")

Print function works as expected but logging messages are missing.

@mhart
Copy link
Contributor

mhart commented Jul 17, 2018

I don't get any output to the logs on a production Lambda with this code – do you?

Edit: sorry, I mean, I get the print() output, but not the log.info output – so I think SAM is acting correctly here

@mhart
Copy link
Contributor

mhart commented Jul 17, 2018

Here's the code I'm testing:

import logging

logging.basicConfig(level=logging.INFO, format='[%(asctime)s] [%(levelname)s] %(message)s')
log = logging.getLogger(__name__)

def lambda_handler(event, context):
    log.info("From log.info")
    context.log("From context.log\n")
    print("From print")

And the output:

START RequestId: 3b08f65c-89e1-11e8-9e88-514fc42a5b51 Version: $LATEST
From context.log
From print
END RequestId: 3b08f65c-89e1-11e8-9e88-514fc42a5b51
REPORT RequestId: 3b08f65c-89e1-11e8-9e88-514fc42a5b51	Duration: 0.54 ms	Billed Duration: 100 ms 	Memory Size: 128 MB	Max Memory Used: 21 MB	

@jfuss
Copy link
Contributor

jfuss commented Jul 17, 2018

@bigunyak @mhart is correct. What you have does not work in the Lambda Service and therefore will not work locally. By some testing, it doesn't appear that logging.basicConfig is respected by the Lambda Runtime. I was able to get this to work:

import logging

log = logging.getLogger(__name__)
log.setLevel(logging.INFO)


def lambda_handler(event, context):
    log.info("From log.info")
    context.log("From context.log\n")
    print("From print")

So instead of using basicConfig, you should use addHandler, setLevel, etc on the logger directly.

Closing as this is not specific to the CLI and the CLI matches the Lambda Service.

@jfuss jfuss closed this as completed Jul 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants