Skip to content

Commit

Permalink
Improve: Log into default php error log if database insert fails
Browse files Browse the repository at this point in the history
  • Loading branch information
BenceSzalai committed Feb 9, 2022
1 parent 30994a5 commit c0491bb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased v2]

## [2.2.0] - 2022-02-09
### Improved
- The handler did not check if inserting the records into the db was successful or not. This could cause important log messages to be lost unnoticed. The handler now logs a warning into the default PHP error log and also logs the failed record there for reference.

## [2.1.2] - 2020-11-29
### Improved
- Constructor can now be called without passing the global `$wpdb`, it'll be used by default.
Expand Down Expand Up @@ -69,7 +73,8 @@ V1 is continued to be updated for continued support for Monolog v1 and PHP versi
No changelog had been maintained up to this point. Refer to the GIT commit history for more details.


[Unreleased v2]: https://github.com/bradmkjr/monolog-wordpress/compare/2.1.2...HEAD
[Unreleased v2]: https://github.com/bradmkjr/monolog-wordpress/compare/2.2.0...HEAD
[2.2.0]: https://github.com/bradmkjr/monolog-wordpress/tree/2.2.0
[2.1.2]: https://github.com/bradmkjr/monolog-wordpress/tree/2.1.2
[2.1.1]: https://github.com/bradmkjr/monolog-wordpress/tree/2.1.1
[2.1.0]: https://github.com/bradmkjr/monolog-wordpress/tree/2.1.0
Expand Down
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
The MIT License (MIT)

Copyright (c) 2020-2022 Bence Szalai
Copyright (c) 2016 bradmkjr

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": ["wordpress", "log", "logging", "monolog", "mysql", "database"],
"homepage": "https://github.com/bradmkjr/monolog-wordpress",
"license": "MIT",
"version": "2.1.2",
"version": "2.2.0",
"authors": [
{
"name": "Bradford Knowlton",
Expand Down
26 changes: 24 additions & 2 deletions src/WordPressHandler/WordPressHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,29 @@ protected function write(array $record): void

$table_name = $this->get_table_name();

$this->wpdb->insert( $table_name, $contentArray );
$this->maybe_truncate();
if (!$this->wpdb->insert( $table_name, $contentArray )) {

// E_USER_ERROR would terminate PHP so we must only use WARNING or NOTICE
$php_error_level = ($record['level'] <= Logger::NOTICE) ? E_USER_NOTICE : E_USER_WARNING;

if ( '' === $this->wpdb->last_error ) {
trigger_error('WordPressHandler failed to write a log record into the database and wpdb returned no error message. This typically happens in WordPress versions prior v5.9 when the message, or a context or an extra field is too long or contains invalid data. Since WordPress v5.9 too long or invalid data triggers a specific error message. If you are using WordPress v5.9 or later the root cause of the issue is unknown.', E_USER_WARNING);
}
else {
trigger_error('WordPressHandler failed to write a log record into the database. ' . $this->wpdb->last_error, E_USER_WARNING);
}

trigger_error(
'WordPressHandler failed to log the following record.'.
' Time: '.$contentArray['time'].
' Channel: '.$contentArray['channel'].
' Level: '.$contentArray['level'].
' Message: `'.$contentArray['message'].'`',
$php_error_level
);
}
else {
$this->maybe_truncate();
}
}
}

0 comments on commit c0491bb

Please sign in to comment.