Skip to content

Commit

Permalink
Merge pull request #18 from BenceSzalai/v1
Browse files Browse the repository at this point in the history
Improve: Log into default php error log if database insert fails
  • Loading branch information
BenceSzalai authored Feb 9, 2022
2 parents 8197f0c + f4a3fce commit d8605e1
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]

## [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.
Expand Down Expand Up @@ -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
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": "1.7.2",
"version": "1.8.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 @@ -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();
}
}
}

0 comments on commit d8605e1

Please sign in to comment.