How to add a Drupal error message and log entry
Issue:
You would like to create a custom error message shown on the page. Also, if the error occurs, it should be properly logged in the recent log messages backend area (`/admin/reports/dblog`) of your site.
Solution:
Use the Drupal logger and messenger service to create the errors.