Skip to content

Commit 875551e

Browse files
Update readme.md
1 parent b757a95 commit 875551e

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

readme.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,50 @@ Caught exceptions can be captured, too.
5757

5858
```php
5959
try{
60-
echo 'hi';
60+
echo 'hi';
6161
}catch(Exception $e ){
62-
plugin_name_replace_me()->logger()->log_exception('error', $e);
62+
plugin_name_replace_me()->logger()->log_exception('error', $e);
6363
}
6464
```
6565

6666
By default, the logger will return a `Log_Item` class, but you can also _return_ a `WP_Error` object, instead with `log_as_error`
6767

6868
```php
6969
$wp_error_object = plugin_name_replace_me()->logger()->log_as_error(
70-
'error',
71-
'error_code',
72-
'error_message',
73-
['arbitrary' => 'data', 'that' => 'is relevant']
70+
'error',
71+
'error_code',
72+
'error message',
73+
['arbitrary' => 'data', 'that' => 'is relevant']
7474
);
7575

7676
var_dump($wp_error_object); // WP_Error...
7777
```
7878

79+
**NOTE:** It is considered a bad practice to make your error message contain dynamic data. This can mess with logger utilities that use logged events and cause un-necessary bloat for logging utilities that check for identical events that have happened in the past. Instead, put related data in the `data` array in the 4th argument.
80+
81+
**Bad:**
82+
83+
84+
```php
85+
$wp_error_object = plugin_name_replace_me()->logger()->log_as_error(
86+
'error',
87+
'invalid_product',
88+
'The product ID ' . $id . ' is invalid'
89+
);
90+
```
91+
92+
**Good:**
93+
94+
```php
95+
$wp_error_object = plugin_name_replace_me()->logger()->log_as_error(
96+
'error',
97+
'invalid_product',
98+
'An invalid product was referenced',
99+
['product_id' => $id]
100+
);
101+
```
102+
103+
79104
### Gather Errors
80105

81106
Sometimes, you will run several functions in a row that could potentially return an error. Gather errors will lump them

0 commit comments

Comments
 (0)