Skip to content

Commit ca91daf

Browse files
authored
Merge pull request #1969 from brefphp/logs-formatter
Add a Monolog log formatter optimized for CloudWatch
2 parents d866b4b + b7f08f8 commit ca91daf

File tree

3 files changed

+110
-11
lines changed

3 files changed

+110
-11
lines changed

docs/environment/logs.mdx

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { NextSeo } from 'next-seo';
2+
import { Tab, Tabs } from 'nextra/components';
3+
import { Callout } from 'nextra/components';
24

35
<NextSeo description="Learn how to write and read PHP logs on AWS Lambda using Bref." />
46

@@ -29,22 +31,76 @@ Your application can write logs to CloudWatch:
2931
- [With the PHP-FPM runtime for web apps](../runtimes/fpm-runtime.mdx): write logs to `stderr`
3032
- [With the runtime for event-driven functions](../runtimes/function.mdx): write logs to `stdout` (using `echo` for example) or `stderr`
3133

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**.
3335

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.
3739

38-
$log->warning('This is a warning!');
39-
```
40+
It is recommended you enable Bref's logs formatter optimized for CloudWatch:
4041

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+
```
4247
43-
```php
44-
$log = new \Bref\Logger\StderrLogger();
48+
<Callout>
49+
This formatter will be enabled by default in Bref v3.
50+
</Callout>
4551
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>
48104

49105
### Reading logs
50106

docs/laravel/getting-started.mdx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,26 @@ php artisan config:clear && php artisan config:cache
8080

8181
In case your application is showing a blank page after being deployed, [have a look at the logs](../environment/logs.md).
8282

83+
## Logs
84+
85+
Thanks to the Bref integration, Laravel will automatically log to CloudWatch via `stderr`. You don't have to do anything.
86+
87+
You can learn more about logs in the [Logs guide](../environment/logs.md).
88+
89+
It is recommended you enable Bref's logs formatter optimized for CloudWatch:
90+
91+
```yaml filename="serverless.yml"
92+
provider:
93+
environment:
94+
LOG_STDERR_FORMATTER: Bref\Monolog\CloudWatchFormatter
95+
```
96+
97+
<Callout>
98+
This formatter will be enabled by default in Bref v3.
99+
</Callout>
100+
101+
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).
102+
83103
## Website assets
84104
85105
Have a look at the [Website guide](../use-cases/websites.mdx) to learn how to deploy a website with assets.

docs/symfony/getting-started.mdx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,29 @@ class Kernel extends BrefKernel
117117

118118
In case your application is showing a blank page after being deployed, [have a look at the logs](../environment/logs.md).
119119

120+
## Logs
121+
122+
Thanks to the Bref integration, Symfony will automatically log to CloudWatch via `stderr`. You don't have to do anything.
123+
124+
You can learn more about logs in the [Logs guide](../environment/logs.md).
125+
126+
It is recommended you enable Bref's logs formatter optimized for CloudWatch:
127+
128+
```yaml filename="config/packages/prod/monolog.yaml"
129+
monolog:
130+
handlers:
131+
file:
132+
type: stream
133+
level: info
134+
formatter: 'Bref\Monolog\CloudWatchFormatter'
135+
```
136+
137+
<Callout>
138+
This formatter will be enabled by default in Bref v3.
139+
</Callout>
140+
141+
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.
142+
120143
## Website assets
121144

122145
Have a look at the [Website guide](../use-cases/websites.mdx) to learn how to deploy a website with assets.

0 commit comments

Comments
 (0)