- Drupal 8 Module Development
- Daniel Sipos
- 412字
- 2021-07-02 15:45:20
Our own logger
Now that we have a channel for our module, let's assume that we want to also log messages elsewhere. They are fine to be stored in the database, but let's also send an email whenever we encounter an error log. In this section, we will only cover the logging architecture needed for this and defer the actual mailing implementation to the second part of this chapter when we discuss mailing.
The first thing that we will need to create is LoggerInterface implementation, which typically goes in the Logger folder of our namespace. So, let's call ours MailLogger; the following is the code for it:
namespace Drupal\hello_world\Logger;
use Drupal\Core\Logger\RfcLoggerTrait;
use Psr\Log\LoggerInterface;
/**
* A logger that sends an email when the log type is error.
*/
class MailLogger implements LoggerInterface {
use RfcLoggerTrait;
/**
* {@inheritdoc}
*/
public function log($level, $message, array $context = array()) {
// Log our message to our logging system.
}
}
The first thing to note is that we are implementing the PSR-3 LoggerInterface. This will require a bunch of methods, but we will take care of most of them via RfcLoggerTrait. The only one left to implement is the log() method, which will be responsible for doing the actual logging. For now, we will keep it empty.
By itself, having this class does nothing. We will need to register it as a tagged service so that LoggingChannelFactory picks it up and passes it to the logging channel when something needs to be logged. Let's take a look at what that definition looks like:
hello_world.logger.hello_world:
class: Drupal\hello_world\Logger\MailLogger
tags:
- { name: logger }
As it stands, our logger doesn't need any dependencies. However, you will note a new property called tags via which we literally tag this service with logger tag. This will register it as a specific service that another service (called a collector) looks for. In this case, the latter isLoggingChannelFactory, if you remember.
Clearing the cache now should enable our logger. This means that when a message is being logged, via any channel, our logger is also used, together with any other enabled loggers (by default, the database one). The preceding service definition that we wrote is used to toggle that. So, if we want our logger to be the only one, we will need to disable the DB Log module from Drupal core.
We will continue working on this class later in this chapter when we will cover sending out emails programmatically.
- 企業(yè)級(jí)Java EE架構(gòu)設(shè)計(jì)精深實(shí)踐
- C語(yǔ)言程序設(shè)計(jì)案例教程(第2版)
- 深入理解Django:框架內(nèi)幕與實(shí)現(xiàn)原理
- Android Studio Essentials
- 控糖控脂健康餐
- Python程序設(shè)計(jì)案例教程
- 編程數(shù)學(xué)
- Nginx實(shí)戰(zhàn):基于Lua語(yǔ)言的配置、開(kāi)發(fā)與架構(gòu)詳解
- Mastering Android Game Development
- JSP程序設(shè)計(jì)與案例實(shí)戰(zhàn)(慕課版)
- C語(yǔ)言程序設(shè)計(jì)
- Apache Solr PHP Integration
- jQuery從入門到精通(微課精編版)
- 百萬(wàn)在線:大型游戲服務(wù)端開(kāi)發(fā)
- R語(yǔ)言實(shí)戰(zhàn)(第2版)