|
1 | 1 | import { NextSeo } from 'next-seo'; |
| 2 | +import { Tab, Tabs } from 'nextra/components'; |
| 3 | +import { Callout } from 'nextra/components'; |
2 | 4 |
|
3 | 5 | <NextSeo description="Learn how to write and read PHP logs on AWS Lambda using Bref." /> |
4 | 6 |
|
@@ -29,22 +31,76 @@ Your application can write logs to CloudWatch: |
29 | 31 | - [With the PHP-FPM runtime for web apps](../runtimes/fpm-runtime.mdx): write logs to `stderr` |
30 | 32 | - [With the runtime for event-driven functions](../runtimes/function.mdx): write logs to `stdout` (using `echo` for example) or `stderr` |
31 | 33 |
|
32 | | -For example with [Monolog](https://github.com/Seldaek/monolog): |
| 34 | +All logs written to `stdout` or `stderr` are automatically be sent to CloudWatch asynchronously by AWS Lambda, **without performance impact on applications**. |
33 | 35 |
|
34 | | -```php |
35 | | -$log = new Monolog\Logger('name'); |
36 | | -$log->pushHandler(new StreamHandler('php://stderr', Logger::WARNING)); |
| 36 | +<Tabs items={['Laravel', 'Symfony', 'PHP']}> |
| 37 | + <Tab> |
| 38 | + If you use Laravel, Bref will automatically configure Laravel to log to CloudWatch via `stderr`. You don't have to do anything. |
37 | 39 |
|
38 | | -$log->warning('This is a warning!'); |
39 | | -``` |
| 40 | + It is recommended you enable Bref's logs formatter optimized for CloudWatch: |
40 | 41 |
|
41 | | -For simple needs, you can replace Monolog with [Bref's logger](https://github.com/brefphp/logger), a PSR-3 logger designed for AWS Lambda: |
| 42 | + ```yaml filename="serverless.yml" |
| 43 | + provider: |
| 44 | + environment: |
| 45 | + LOG_STDERR_FORMATTER: Bref\Monolog\CloudWatchFormatter |
| 46 | + ``` |
42 | 47 |
|
43 | | -```php |
44 | | -$log = new \Bref\Logger\StderrLogger(); |
| 48 | + <Callout> |
| 49 | + This formatter will be enabled by default in Bref v3. |
| 50 | + </Callout> |
45 | 51 |
|
46 | | -$log->warning('This is a warning!'); |
47 | | -``` |
| 52 | + 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](https://laravel.com/docs/12.x/context). |
| 53 | + </Tab> |
| 54 | + <Tab> |
| 55 | + If you use Symfony, Bref will automatically configure Symfony to log to CloudWatch via `stderr`. You don't have to do anything. |
| 56 | + |
| 57 | + It is recommended you enable Bref's logs formatter optimized for CloudWatch: |
| 58 | + |
| 59 | + ```yaml filename="config/packages/prod/monolog.yaml" |
| 60 | + monolog: |
| 61 | + handlers: |
| 62 | + file: |
| 63 | + type: stream |
| 64 | + level: info |
| 65 | + formatter: 'Bref\Monolog\CloudWatchFormatter' |
| 66 | + ``` |
| 67 | + |
| 68 | + <Callout> |
| 69 | + This formatter will be enabled by default in Bref v3. |
| 70 | + </Callout> |
| 71 | + |
| 72 | + With this formatter, logs will contain structured data that can be filtered in CloudWatch Logs Insights. For example, you can filter by log level or exception class. |
| 73 | + </Tab> |
| 74 | + <Tab> |
| 75 | + You can use [Monolog](https://github.com/Seldaek/monolog) to write logs to CloudWatch via `stderr`: |
| 76 | + |
| 77 | + ```php |
| 78 | + $log = new Monolog\Logger('default'); |
| 79 | + $log->pushHandler(new StreamHandler('php://stderr', Logger::INFO)); |
| 80 | +
|
| 81 | + $log->warning('This is a warning!'); |
| 82 | + ``` |
| 83 | + |
| 84 | + Bref provides a formatter optimized for CloudWatch, it is highly recommended to use it: |
| 85 | + |
| 86 | + ```php |
| 87 | + $log = new Monolog\Logger('default'); |
| 88 | + $handler = new StreamHandler('php://stderr', Logger::INFO); |
| 89 | + $handler->setFormatter(new Bref\Logs\CloudWatchFormatter); |
| 90 | + $log->pushHandler($handler); |
| 91 | +
|
| 92 | + $log->warning('This is a warning!'); |
| 93 | + ``` |
| 94 | + |
| 95 | + For simple needs, you can replace Monolog with [Bref's logger](https://github.com/brefphp/logger), a PSR-3 logger designed for AWS Lambda: |
| 96 | + |
| 97 | + ```php |
| 98 | + $log = new \Bref\Logger\StderrLogger(); |
| 99 | +
|
| 100 | + $log->warning('This is a warning!'); |
| 101 | + ``` |
| 102 | + </Tab> |
| 103 | +</Tabs> |
48 | 104 |
|
49 | 105 | ### Reading logs |
50 | 106 |
|
|
0 commit comments