annotate vendor/chi-teck/drupal-code-generator/templates/d8/service/logger.twig @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\{{ machine_name }}\Logger;
Chris@0 4
Chris@0 5 use Drupal\Core\Config\ConfigFactoryInterface;
Chris@0 6 use Drupal\Core\Datetime\DateFormatterInterface;
Chris@0 7 use Drupal\Core\Logger\LogMessageParserInterface;
Chris@0 8 use Drupal\Core\Logger\RfcLoggerTrait;
Chris@0 9 use Drupal\Core\Logger\RfcLogLevel;
Chris@0 10 use Psr\Log\LoggerInterface;
Chris@0 11
Chris@0 12 /**
Chris@4 13 * Redirects messages to a file.
Chris@0 14 */
Chris@0 15 class {{ class }} implements LoggerInterface {
Chris@0 16
Chris@0 17 use RfcLoggerTrait;
Chris@0 18
Chris@0 19 /**
Chris@0 20 * A configuration object containing system.file settings.
Chris@0 21 *
Chris@0 22 * @var \Drupal\Core\Config\Config
Chris@0 23 */
Chris@0 24 protected $config;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * The message's placeholders parser.
Chris@0 28 *
Chris@0 29 * @var \Drupal\Core\Logger\LogMessageParserInterface
Chris@0 30 */
Chris@0 31 protected $parser;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * The date formatter service.
Chris@0 35 *
Chris@0 36 * @var \Drupal\Core\Datetime\DateFormatterInterface
Chris@0 37 */
Chris@0 38 protected $dateFormatter;
Chris@0 39
Chris@0 40 /**
Chris@4 41 * Constructs {{ class|article }} object.
Chris@0 42 *
Chris@0 43 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@0 44 * The configuration factory object.
Chris@0 45 * @param \Drupal\Core\Logger\LogMessageParserInterface $parser
Chris@0 46 * The parser to use when extracting message variables.
Chris@0 47 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
Chris@0 48 * The date formatter service.
Chris@0 49 */
Chris@0 50 public function __construct(ConfigFactoryInterface $config_factory, LogMessageParserInterface $parser, DateFormatterInterface $date_formatter) {
Chris@0 51 $this->config = $config_factory->get('system.file');
Chris@0 52 $this->parser = $parser;
Chris@0 53 $this->dateFormatter = $date_formatter;
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * {@inheritdoc}
Chris@0 58 */
Chris@0 59 public function log($level, $message, array $context = []) {
Chris@0 60
Chris@0 61 // Populate the message placeholders and then replace them in the message.
Chris@0 62 $message_placeholders = $this->parser->parseMessagePlaceholders($message, $context);
Chris@0 63 $message = empty($message_placeholders) ? $message : strtr($message, $message_placeholders);
Chris@0 64
Chris@0 65 $entry = [
Chris@0 66 'message' => strip_tags($message),
Chris@0 67 'date' => $this->dateFormatter->format($context['timestamp']),
Chris@0 68 'type' => $context['channel'],
Chris@0 69 'ip' => $context['ip'],
Chris@0 70 'request_uri' => $context['request_uri'],
Chris@0 71 'referer' => $context['referer'],
Chris@0 72 'severity' => (string) RfcLogLevel::getLevels()[$level],
Chris@0 73 'uid' => $context['uid'],
Chris@0 74 ];
Chris@0 75
Chris@0 76 file_put_contents(
Chris@0 77 $this->config->get('path.temporary') . '/drupal.log',
Chris@0 78 print_r($entry, TRUE),
Chris@0 79 FILE_APPEND
Chris@0 80 );
Chris@0 81 }
Chris@0 82
Chris@0 83 }