Open Data Catalog v
LoggerTrait.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Drupal\common;
4 
5 use Drupal\Core\Logger\LoggerChannelFactory;
6 use Psr\Log\LogLevel;
7 
11 trait LoggerTrait {
12 
21  private $loggerName = 'dkan';
22 
28  private $loggerService;
29 
30  private $debug = FALSE;
31 
38  public function setLoggerName(string $name) {
39  $this->loggerName = $name;
40  }
41 
48  public function setLoggerFactory(LoggerChannelFactory $loggerService) {
49  $this->loggerService = $loggerService;
50  }
51 
55  private function log($loggerName, $message, $variables = [], $level = LogLevel::ERROR) {
56  if ($this->loggerService) {
57  $this->loggerService->get($loggerName)->log($level, $message, $variables);
58  }
59  }
60 
64  private function error(string $message, array $context = []) {
65 
66  if ($this->loggerService) {
67  $this->loggerService->get($this->loggerName)->error($message, $context);
68  }
69  }
70 
74  private function warning(string $message, array $context = []) {
75 
76  if ($this->loggerService) {
77  $this->loggerService->get($this->loggerName)->warning($message, $context);
78  }
79  }
80 
84  private function notice(string $message, array $context = []) {
85 
86  if ($this->loggerService) {
87  $this->loggerService->get($this->loggerName)->notice($message, $context);
88  }
89  }
90 
94  private function showDebug() {
95  $this->debug = TRUE;
96  }
97 
101  private function debug(string $message = "", array $context = []) {
102  if ($this->loggerService && $this->debug) {
103  $m = "@class @function: " . $message;
104  $c = array_merge($context,
105  [
106  '@class' => static::class,
107  '@function' => debug_backtrace()[1]['function'],
108  '@message' => $message,
109  ]
110  );
111 
112  $this->loggerService->get($this->loggerName)->notice($m, $c);
113  }
114  }
115 
116 }
Drupal\common\LoggerTrait
trait LoggerTrait
Definition: LoggerTrait.php:11
Drupal\common\setLoggerName
setLoggerName(string $name)
Definition: LoggerTrait.php:38
Drupal\common\setLoggerFactory
setLoggerFactory(LoggerChannelFactory $loggerService)
Definition: LoggerTrait.php:48
Drupal\common