Skip to content

Commit 1778aa4

Browse files
authored
Merge pull request #123 from MuriloChianfa/master
Adding a new tip to discover all events fired in one request
2 parents c0d4954 + 78555d4 commit 1778aa4

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Or if you want the Chinese version:
2222
[中文版本](https://github.com/Lysice/laravel-tips-chinese/blob/master/README-zh.md)
2323

2424
---
25-
**Update 2 December 2022**: Currently there are **343 tips** divided into 14 sections.
25+
**Update 20 December 2022**: Currently there are **344 tips** divided into 14 sections.
2626

2727
## Table of contents
2828

@@ -37,6 +37,6 @@ Or if you want the Chinese version:
3737
- [Mail](mail.md) (7 tips)
3838
- [Artisan](artisan.md) (7 tips)
3939
- [Factories](factories.md) (8 tips)
40-
- [Log and debug](log-and-debug.md) (7 tips)
40+
- [Log and debug](log-and-debug.md) (8 tips)
4141
- [API](api.md) (9 tips)
4242
- [Other](other.md) (71 tips)

log-and-debug.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [Log with context](#log-with-context)
1010
- [Quickly output an Eloquent query in its SQL form](#quickly-output-an-eloquent-query-in-its-sql-form)
1111
- [Log all the database queries during development](#log-all-the-database-queries-during-development)
12+
- [Discover all events fired in one request](#discover-all-events-fired-in-one-request)
1213

1314
### Logging with parameters
1415

@@ -113,3 +114,32 @@ public function boot()
113114

114115
Tip given by [@mmartin_joo](https://twitter.com/mmartin_joo/status/1473262634405449730)
115116

117+
### Discover all events fired in one request
118+
119+
If you want to implement a new listener to a specific event but you don't know its name, you can log all events fired during the request.
120+
121+
You can use the `\Illuminate\Support\Facades\Event::listen()` method on `boot()` method of `app/Providers/EventServiceProvider.php` to catch all events fired.
122+
123+
**Important:** If you use the `Log` facade within this event listener then you will need to exclude events named `Illuminate\Log\Events\MessageLogged` to avoid an infinite loop.
124+
(Example: `if ($event == 'Illuminate\\Log\\Events\\MessageLogged') return;`)
125+
126+
```php
127+
// Include Event...
128+
use Illuminate\Support\Facades\Event;
129+
130+
// In your EventServiceProvider class...
131+
public function boot()
132+
{
133+
parent::boot();
134+
135+
Event::listen('*', function ($event, array $data) {
136+
// Log the event class
137+
error_log($event);
138+
139+
// Log the event data delegated to listener parameters
140+
error_log(json_encode($data, JSON_PRETTY_PRINT));
141+
});
142+
}
143+
```
144+
145+
Tip given by [@MuriloChianfa](https://github.com/MuriloChianfa)

0 commit comments

Comments
 (0)