Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not log silenced PHP errors #51

Merged
merged 4 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/03-a-deeper-look-at-wonolog.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Please keep in mind that such customization **must be done in an MU plugin**, be

## Wonolog PHP Error Handler

As mentioned before, by default, Wonolog logs all kinds of PHP errors.
As mentioned before, by default, Wonolog logs all kinds of PHP errors. It does not log silenced PHP errors.

This is possible because Wonolog registers custom error and exception handlers.

Expand Down Expand Up @@ -76,6 +76,10 @@ The **log channel** used for these events is `Channels::PHP_ERROR`, and the **lo

Refer to [Wonolog Customization](05-wonolog-customization.md) to learn how to customize or even disable this PHP error handler.

If you want to log also silenced PHP errors you can do so with a filter:
```
add_filter('wonolog.report-silenced-errors', '__return_true');
```

## Default Handler Minimum Log Level

Expand Down
19 changes: 18 additions & 1 deletion src/PhpErrorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ class PhpErrorController {
*/
public function on_error( $num, $str, $file, $line, $context = NULL ) {

$level = isset( self::$errors_level_map[ $num ] )
? self::$errors_level_map[ $num ]
: NULL;

$report_silenced = apply_filters(
'wonolog.report-silenced-errors',
error_reporting() !== 0,
$num,
$str,
$file,
$line
);

if ( $level === NULL || ! $report_silenced ) {
return FALSE;
}

$log_context = [];
if ( $context ) {
$skip_keys = array_merge( array_keys( $GLOBALS ), self::$super_globals_keys );
Expand All @@ -80,7 +97,7 @@ public function on_error( $num, $str, $file, $line, $context = NULL ) {
// Log the PHP error.
do_action(
\Inpsyde\Wonolog\LOG,
new Log( $str, self::$errors_level_map[ $num ], Channels::PHP_ERROR, $log_context )
new Log( $str, $level, Channels::PHP_ERROR, $log_context )
);

return FALSE;
Expand Down