From f4a3fce2e6dc1eeac5cfa9d5e889ee4d2082f543 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 9a64186..b411a2b 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] +## [1.8.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. + ## [1.7.2] - 2020-11-29 ### Improved - Constructor can now be called without passing the global `$wpdb`, it'll be used by default. @@ -61,7 +65,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 No changelog had been maintained up to this point. Refer to the GIT commit history for more details. -[Unreleased]: https://github.com/bradmkjr/monolog-wordpress/compare/1.7.2...v1 +[Unreleased]: https://github.com/bradmkjr/monolog-wordpress/compare/1.8.0...v1 +[1.8.0]: https://github.com/bradmkjr/monolog-wordpress/tree/1.8.0 [1.7.2]: https://github.com/bradmkjr/monolog-wordpress/tree/1.7.2 [1.7.1]: https://github.com/bradmkjr/monolog-wordpress/tree/1.7.1 [1.7.0]: https://github.com/bradmkjr/monolog-wordpress/tree/1.7.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 3c02765..36c9daf 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": "1.7.2", + "version": "1.8.0", "authors": [ { "name": "Bradford Knowlton", diff --git a/src/WordPressHandler/WordPressHandler.php b/src/WordPressHandler/WordPressHandler.php index 1db554b..f1edadc 100644 --- a/src/WordPressHandler/WordPressHandler.php +++ b/src/WordPressHandler/WordPressHandler.php @@ -274,7 +274,29 @@ protected function write(array $record) $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(); + } } }