- Drupal 8 Module Development
- Daniel Sipos
- 296字
- 2021-07-02 15:45:19
Our own logger channel
I mentioned earlier how we can define our own logger channel so that we don't have to always inject the entire factory. So, let's take a look at how to create one for the Hello World module we're now writing.
Most of the time, all we have to do is add the following definition to our hello_world.services.yml file:
hello_world.logger.channel.hello_world:
parent: logger.channel_base
arguments: ['hello_world']
Before talking about the actual logger channel, let's see what this weird service definition actually means because this is not something we've seen before--I mean, where's the class?
The parent key means that our service will inherit the definition from another service. In our case, the parent key is logger.channel_base, and this means that the class used will be Drupal\Core\Logger\LoggerChannel (the default). If we look closely at the logger.channel_base service definition in core.services.yml, we'll also see a factory key, which means that this service class is not being instantiated by the service container but by another service, namely logger.factory service's get() method.
The arguments key is also slightly different. First of all, we don't have the @ sign. That is because this sign is used to denote a service name, whereas our argument is a simple string. As a bonus tidbit, if the string is preceded and followed by a %, it denotes a parameter that can be defined in any *.services.yml file.
Getting back to our example then, if you remember the logger theory, this service definition will mean that requesting this service will perform, under the hood, the following task:
\Drupal::service('logger.factory')->get('hello_world');
It uses the logger factory to load a channel with a certain argument. So, now we can inject our hello_world.logger.channel.hello_world service and call any of the LoggerInterface methods on it directly in our client code.
- 演進式架構(gòu)(原書第2版)
- JSP網(wǎng)絡(luò)編程(學習筆記)
- C程序設(shè)計簡明教程(第二版)
- 實用防銹油配方與制備200例
- Python爬蟲開發(fā)與項目實戰(zhàn)
- 新編Premiere Pro CC從入門到精通
- Kotlin編程實戰(zhàn):創(chuàng)建優(yōu)雅、富于表現(xiàn)力和高性能的JVM與Android應(yīng)用程序
- Multithreading in C# 5.0 Cookbook
- Unity&VR游戲美術(shù)設(shè)計實戰(zhàn)
- Learning Material Design
- UML2面向?qū)ο蠓治雠c設(shè)計(第2版)
- Unity 2017 Game AI Programming(Third Edition)
- Mastering OAuth 2.0
- SQL Server實例教程(2008版)
- Learning Alfresco Web Scripts