Logs
As explained in the storage documentation, the filesystem on AWS Lambda is:
- read-only, except for
/tmp
- not shared between lambda instances
- not persistent
Because of that, logs should not be stored on disk.
CloudWatch
The simplest solution is to push logs to AWS CloudWatch, AWS' service for logs.
PHP errors and warnings
By default, all PHP errors, warnings and notices emitted by PHP will be forwarded into CloudWatch.
That means that you don't have to configure anything to log errors, warnings or uncaught exceptions.
Writing logs
Your application can write logs to CloudWatch:
- With the PHP-FPM runtime for web apps: write logs to
stderr
- With the runtime for event-driven functions: write logs to
stdout
(usingecho
for example) orstderr
All logs written to stdout
or stderr
are automatically be sent to CloudWatch asynchronously by AWS Lambda, without performance impact on applications.
If you use Laravel, Bref will automatically configure Laravel to log to CloudWatch via stderr
. You don't have to do anything.
It is recommended you enable Bref's logs formatter optimized for CloudWatch:
provider:
environment:
LOG_STDERR_FORMATTER: Bref\Monolog\CloudWatchFormatter
This formatter will be enabled by default in Bref v3.
With this formatter, logs will contain structured data that can be filtered in CloudWatch Logs Insights. For example, you can filter by log level, exception class, or anything in the Laravel Context (opens in a new tab).
Reading logs
To read logs, either open the CloudWatch console (opens in a new tab) or the Bref Dashboard (opens in a new tab).
You can also use serverless logs
to view them in the CLI:
serverless logs -f <function-name>
# Tail logs:
serverless logs -f <function-name> --tail