When I run start-api using this command (or variants):
sam local start-api --log-file ./out.log
Variants tried:
sam local start-api --log-file=./out.log
sam local start-api --log-file out.log
sam local start-api --log-file=out.log
sam local start-api -l ./out.log
no output file is written (nor created), and no error message is displayed.
My handler's return value is displayed in the console as expected.
template.yaml:
AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: My first serverless application.
Resources:
Products:
Type: AWS::Serverless::Function
Properties:
Handler: products.handler
Runtime: python3.6
FunctionName: products_handler
CodeUri: ./dist
Events:
ListProducts:
Type: Api
Properties:
Path: /products
Method: get
Product:
Type: Api
Properties:
Path: /products/{product}
Method: any
Test case:
import logging
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.INFO)
def handler(event, context):
LOGGER.error("Test message")
return {'body': "some output\n"}
Command:
curl http://localhost:3000/products
If I set my logger to log to stdout/stderr (using logging.StreamHandler), I do see log message on the console,
but not using the aforementioned method (nor using any file-based ones, either, which I wouldn't expect to work anyway.)
Everything else works.
What am I missing?
Can you try print("foo") and tell me if it appears in log file? This should work unless the Docker container somehow intercepts the Logger output stream. If not, there is a bug in setting up the log stream.
Running with sam local start-api --log-file ./out.log
print('foo')
goes directly to the console
I can confirm that no log file is created. I also tried several ways to generated logs and none of them are written to a file.
If I search through the code, I actually can't find any reference to this option, other than in main.go where it is listed as a possible option. Perhaps am I overlooking it, but it seems like during some refactoring this got removed.
I just opened a related issue https://github.com/awslabs/aws-sam-local/issues/176
Edit: Found how to fix it, submitting a pull request
Change merged. Pending release
Most helpful comment
Change merged. Pending release