- Drupal 8 Module Development
- Daniel Sipos
- 484字
- 2021-07-02 12:22:43
Logging for Hello World
Now that we have all the tools at our disposal, and more importantly, understand how logging works in Drupal 8, let's add some logging to our module.
There is one place where we can log an action that may prove helpful. Let's log an info message when an administrator changes the greeting message via the form we wrote. This should naturally happen in the submit handler of SalutationConfigurationForm.
If you remember my rant in the previous chapter, there is no way we should use a service statically if we can instead inject it, and we can easily inject services into our form. So, let's do this now.
First of all, FormBase already implements ContainerInjectionInterface, so we don't need to implement it in our class, as we are extending from it somewhere down the line. Second of all, the ConfigFormBase class we are directly extending already has config.factory injected, so this complicates things for us a bit—well, not really. All we need to do is copy over the constructor and create() method, add our own service, store it in a property, and pass the services the parent needs to the parent constructor call. It will look like this:
/**
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* SalutationConfigurationForm constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Logger\LoggerChannelInterface $logger
* The logger.
*/
public function __construct(ConfigFactoryInterface $config_factory, LoggerChannelInterface $logger) {
parent::__construct($config_factory);
$this->logger = $logger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('hello_world.logger.channel.hello_world')
);
}
And the relevant use statements at the top:
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
As you can see, we get all the services that any of the parents need, plus the one we want (the logger channel) via the create() method. Also, in our constructor, we store the channel as a property and then pass the parent arguments to the parent constructor. Now, we have our hello_world logger channel available in our configuration form class. So, let's use it.
At the end of the submitForm() method, let's add the following line:
$this->logger->info('The Hello World salutation has been changed to @message.', ['@message' => $form_state->getValue('salutation')]);
We are logging a regular information message. However, since we also want to log the message that has been set, we use the second argument, which represents an array of context values. Under the hood, the database logger will extract the context variables that start with @, !, or % with the values from the entire context array. This is done using the LogMessageParser service but we'll see more of this in a later chapter when we discuss internationalization. If you implement your own logger plugin, you will have to handle this yourself as well—but we'll see that in action soon.
And now we are done with logging a message when the salutation configuration form is saved.
- AngularJS Testing Cookbook
- 微服務(wù)與事件驅(qū)動架構(gòu)
- 架構(gòu)不再難(全5冊)
- 區(qū)塊鏈架構(gòu)與實(shí)現(xiàn):Cosmos詳解
- Java編程指南:基礎(chǔ)知識、類庫應(yīng)用及案例設(shè)計(jì)
- 從程序員到架構(gòu)師:大數(shù)據(jù)量、緩存、高并發(fā)、微服務(wù)、多團(tuán)隊(duì)協(xié)同等核心場景實(shí)戰(zhàn)
- Scratch真好玩:教小孩學(xué)編程
- Mastering Scientific Computing with R
- Gradle for Android
- Learning JavaScript Data Structures and Algorithms
- MongoDB,Express,Angular,and Node.js Fundamentals
- 新一代SDN:VMware NSX 網(wǎng)絡(luò)原理與實(shí)踐
- Citrix XenServer企業(yè)運(yùn)維實(shí)戰(zhàn)
- JavaScript應(yīng)用開發(fā)實(shí)踐指南
- Go語言開發(fā)實(shí)戰(zhàn)(慕課版)