How to Log Messages in Drupal 8

How to Log Messages in Drupal 8

14 فبراير 2023

Keeping a logbook is necessary for the correct operation of the system, so it is prudent to add an algorithm to the code. Thanks to this solution, changes will be made automatically, and all employees with the necessary level of access will have access to them.
Previously, to activate the function used, two protocols were offered by the software. Today we no longer have to face such difficulties because the new Drupal 8 has merged two options into one. Change notifications appear in the "Reports" section, allowing viewing of the content in the user interface. More details about how to log messages in Drupal 8 and Drupal 8 module development will be discussed in our material. 

Registering actions in the log: What information needs to be saved?

Creating a log allows you to keep track of any changes that are related to your site. This way, employees can get information about user actions and detect problems. The size of the detected problems does not affect the result of the log.
Examining the collected data allows you to check the system's performance at any stage. Data about Drupal error logs and user actions are saved automatically and thus are available for analyzing the situation and finding a suitable solution.
Tracking important events ensures that the necessary operations are carried out quickly, and the root causes are eliminated. First and foremost, this applies to site data that has been altered throughout the use. The term information storage is defined based on their requirements and work specifics.

What information will be logged after the activation of the function?

The primary purpose of the developer who uses the log is to be able to assess if necessary. For this purpose, the registration of system changes led to one or another consequence. Below we will consider how to log early messages in Drupal 8. To make the work easier, it is worth providing the ability to save:

  • the actions of all users on the site;
  • notifications of the detection of errors;
  • information on the algorithm used to fix it;
  • the level of load on the site at different times of the day;
  • the peculiarities of the system.

The developer must remain invisible to the other participants, so the possibility of automatic tracking will come in handy. To get information about the current state of affairs, it's enough to get access rights and carefully examine the data in the "Reports" tab.
Access to the information will allow the timely detection of large and small inaccuracies that affect the entire system. A root cause search with saved data is also performed, so the developer has all the tools to detect and correct problems.

How to use a Drupal 8 feature?

Registration of messages in the system is of particular interest, but the implementation path depends largely on the requirements. To be able to save data, the following algorithm must be followed:

  1. SubmitForm. The service center shell is the basis of the form. You need to specify the data through a colon for a specific class. This creates a link to a class that is outside the system.
  2. Registration Service. To store the data, it is necessary to activate Drupal watchdog, and the developer can create copies of existing databases or use new ones. Using the service allows you to run multiple registrars, which will be needed in the future.
  3. Level of importance. Error notifications can come in silent mode until an importance level is set. Detecting problems, in this case, will cause a notification to be sent to the developer via a popup message.
  4. Log message type. The line implies fillers, which serve as the developer's information source.
  5. Fillers. The administrator must create a pool of keys to get information about the problems. The fillers are created with possible complications in mind. The message in the final form will contain the error number and its description.

Formatting of keys and values allows you to get information about the errors, considering the requirements quickly. The administrator reviews the detailed description of the detected complexities and decides based on the available options.

Viewing messages and filler types

The system administrator receives information about all detected errors, which can be interrelated or vice versa. Objective evaluation of the situation is possible only if full access to the data stored by the system registrar.
Access to the operation reporting is convenient. A user with administrator rights should open the appropriate section and go to the page with the description. Several types of placeholders are used to indicate the reason:

  1. @xxxxx – used more often than others and allows you to convert text to HTML object format;
  2. %xxxxx – the placeholder allows you to pass text through internal channels and jump to HTML-formatted text that is enclosed in tags;
  3. :xxxx – values are filtered and passed in a safe variant.

Fillers allow you to filter the information. Once you have navigated to the reporting section, you can use pointers to find essential issues quickly. In addition, filtering by notification type is available. In this way, you can review the data in a few minutes.

How to create a recorder

Practical skills will simplify many operations. This often plays a vital role in Drupal 8 watchdog notifications. Practice should be carried out taking into account the system's features and the requirements placed on it.
The algorithm for doing this involves registering the Drupal 8 logger as a new service. Setting up the storage location, type, and severity of notifications is necessary for the timely detection of problems and their causes.
Examples for developers of how to create a logger are posted in the core. Here, you can see several options and explore the specifics of task execution in the system modules. Contacts, aggregators, system logs, and other modules will allow you to get the necessary information and create an effective function watchdog for successful use.

Email registration

Each owner of a site or blog in Drupal 8 can allow visitors to register with a personal email. Users can authorize the system by entering the email address and the invented password in the fields provided.
Most web resources have long since abandoned the idea of a "user name," coming up with the idea to apply only email. A special module provided in Drupal 8 will allow users to register by email. It's pretty simple since the fewer fields in the form, the easier it is.
The module can be configured in a special way to allow the use of a username where it is needed. These options involve logging in with a username or email address. Usually, the user name is assigned using the client name in the email address and its identifier. Web resources that want to create custom names can implement hook_email_registration_name.
It is easy to organize email registration. First, you need to run the module in its usual mode. Then you should change the welcome email address through the main menu - Administration – User Management and User Settings. In the open window, replace the token !username with !mailto.
The automatically generated name can still be displayed for posts or comments. Each blog owner can permit their visitors to change their username. This feature allows users to change their name in the "My Account" menu item.

How to work with email in Drupal 8?

You need to implement additional modules to send all sorts of messages from Drupal 8 effectively. But many experts recommend setting up sending mail messages via SMTP, as it makes debugging easier.
You don't need to resort to complicated methods to send a Plain Text email. You need to install the SMTP Authentication Support module, which is optimal for sending simple text messages and make the necessary settings depending on the type of SMTP server used.
Next, you need to modify hook_mail by filling the $message[subject] and $message[body] arrays with values. The usual strings are added to the $message[body] array as long as ordinary text messages are sent. But using the $params[messages] method in Makeup:create is more reliable.
But it is a bit more complicated to send HTML-formatted emails. First, install the Mail System and the Swift Mailer modules. The first module needs a particular configuration with which Swift Mailer will send emails. You also need to disable SMTP Authentication Support, but you do not have to remove it.
To send HTML mails, you need the following:

  1. Implement a hook_theme option to register the theme with the system. It should contain information on which variables will be sent to the template.
  2. To insert the Twig template. The user_name updates_nodes variables must be prescribed in it.
  3. Insert Hook_Mail. The main thing is that the Content_Type parameter in this code must correspond to text/html, otherwise, the system will send plain text messages.

It's also worth adding the code used to call hook_mail.
Note that nowadays, there are more efficient methods of communicating with users than sending notifications via email. Sometimes the system will send the mail to spam because it is often suspicious to any newcomer in the sphere of sender and receiver of messages. Email is still frequently used, so there is no reason to avoid this communication channel.

How to set up an account?

One of the most interesting features of Drupal 8 is the creation or removal of users who can leave comments or publish their content. Drupal 8 has a user moderation module.
On the main options pages, it is possible to change the settings of newly created users, along with notifications sent to the mail by users who perform different actions. In addition, the administrator can extend the registration, login, or password recovery procedure. It's also possible to change user settings using additional modules.
In Drupal 8, there is an updated module contact, which allows you to make a contact form for each user. In the settings section "Anonymous users," you can register a user who is not yet registered, for example, "Guest" or "Anonymous."
You'd better consider another nuance. If you create a resource only for the company and the information will be added only by the business owner, you can turn off adding new users. Usually, users are allowed to register on the portal, but they will be blocked until the owner includes them in the general customer list.
If the owner has left the option to register, then anyone can do so. But to avoid robot attacks, it is better to provide a captcha to prevent errors on the site.

How to create modules in Drupal 8

By modules, we mean small functional components that allow site owners to add something to the existing functionality. The module system is divided into several components:
Module Types. This centralized area allows you to set how modules will be found and applied.

  • Modul Discovery. This is the process that the module manager uses to find specific modules declared in all modules.
  • Module Factory. This library is responsible for defining modules for a specific application.
  • Usually, modules consist of these elements:
  • Annotations. Usually, they are defined in comments. You can convey elementary plug-ins knowledge without prescribing codes and methods.
  • Interface. In this component, the module methods used are prescribed.
  • Base. This option, called an abstract class, allows methods to set default values. This is needed to get the information from the annotation.
  • Module Manager. This is referred to as the class that describes where to look for the module. This is also where the hook_alter registration can be made, which can change the information about the module through such an array.

Each of these elements performs its tasks, and they are separated. Modules are declared and used in modules. Developers have the ability to develop their types of modules for their own needs. Modules and services are very similar to each other because they solve the same problems, but it is worth distinguishing when you need to connect a service and when you need to connect a module.

There is nothing easy about this topic. You will need the experience to figure out where to use hook_watchdog() removed and how to log messages in Drupal 8. Don't hesitate to contact us if you need Drupal development services!