From c0491bbcebee383c2c8ebfcb408279e3a8835702 Mon Sep 17 00:00:00 2001 From: BenceSzalai Date: Wed, 9 Feb 2022 19:20:55 +0100 Subject: [PATCH] Improve: Log into default php error log if database insert fails --- CHANGELOG.md | 7 +++++- LICENSE | 1 + composer.json | 2 +- src/WordPressHandler/WordPressHandler.php | 26 +++++++++++++++++++++-- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebeecec..57a9c31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. @@ -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 diff --git a/LICENSE b/LICENSE index 3ed1f31..76eac73 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/composer.json b/composer.json index fbd460b..b03cbbe 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/WordPressHandler/WordPressHandler.php b/src/WordPressHandler/WordPressHandler.php index 548f168..65a9c02 100644 --- a/src/WordPressHandler/WordPressHandler.php +++ b/src/WordPressHandler/WordPressHandler.php @@ -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(); + } } }