Anyone have experience with AWS Swift Lambda's not printing to the console?

I have this code:

print("Parsed endpoint: \(endpoint.rawValue)")

But it never prints to the CloudWatch logs. However, if I replace it with this:

context.logger.log(level: .info, "Parsed endpoint: \(endpoint.rawValue)")

Then it prints every single time.

I tried it in another runtime (Python) and using the basic print command provided by Python logs to CloudWatch just fine. The problem is context.logger.log(level:_:) includes more info then I want so I have to filter it when processing the console output. I would prefer to just be able to use print statements like you can in any other runtime. THIS IS DISCRIMINATION!!! :upside_down_face:

Anyone know why using print() in Swift Lambda's doesn't work? There's no issue with the \(endpoint.rawValue) code.

1 Like

You probably need to flush the buffer with

fflush(stdout)
1 Like

So you're saying I need to run that statement after each print statement to ensure that all print statements and context.logger print statements are printed in order?

Well, I gave it a try immediately after the print statement and definitely did not work:

print("called")
fflush(stdout)

Any logger should do the flushing for you, it's only if you use print you'll need it (and if you don't they'll get logged eventually, on Lambda that's likely at the end of the invocation)

Why it's not appearing you probably need to look at the configuration of the lambda and the logger, it might be piping stdout somewhere else etc.

As an aside, if you combine the logger with the metadata it makes it very easy to filter and find everything, e.g.

context.logger.log(level: .info, "Parsed endpoint", metadata: ["endpoint": "\(endpoint.rawValue)"])

You can then filter on "parsed endpoint" without needing to worry about the different metadata

1 Like