Skip to content

class \Drupal\rules\Plugin\DataType\LoggerEntry #315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 8.x-3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions config/schema/rules.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,38 @@ rules.settings:
log_level:
type: integer
label: 'Log level'

rules.data_type.logger_entry.definition:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need a config schema entry for the logger entry. It is not a config entity, it is just a typed data structure? Does the core Language data type for example have a config schema?

type: mapping
label: 'Logger Entry'
mapping:
uid:
type: user
label: 'User'
channel:
type: string
label: 'Channel'
message:
type: string
label: 'Message'
variables:
type: string
label: 'Message placeholders'
severity:
type: string
label: 'Level'
link:
type: link
label: 'Link'
location:
type: url
label: 'Request URI'
referer:
type: string
label: 'Referer'
hostname:
type: string
label: 'IP'
timestamp:
type: data
label: 'Timestamp'
2 changes: 1 addition & 1 deletion rules.rules.events.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ rules_system_logger_event:
context:
# @todo: create a TypedData logger-entry object: https://www.drupal.org/node/2625238
logger_entry:
type: 'any'
type: 'logger_entry'
label: 'Logger entry'
5 changes: 5 additions & 0 deletions src/Logger/RulesLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Drupal\Core\Logger\LogMessageParserInterface;
use Drupal\Core\Logger\RfcLoggerTrait;
use Drupal\rules\Event\SystemLoggerEvent;
use Drupal\rules\Plugin\DataType\LoggerEntry;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

Expand Down Expand Up @@ -72,6 +73,10 @@ public function log($level, $message, array $context = array()) {
'timestamp' => $context['timestamp'],
);

$logger_entry = new LoggerEntry();
$logger_entry->log($level, $message, $context, $this->parser);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logger entry should not have a log() method. It should have a setMessage() method, a setLevel() method etc.



// Dispatch logger_entry event.
$event = new SystemLoggerEvent($logger_entry, ['logger_entry' => $logger_entry]);
$this->dispatcher->dispatch(SystemLoggerEvent::EVENT_NAME, $event);
Expand Down
99 changes: 99 additions & 0 deletions src/Plugin/DataType/LoggerEntry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/**
* @file
* Contains \Drupal\rules\Plugin\DataType\LoggerEntry.
*/

namespace Drupal\rules\Plugin\DataType;

use Drupal\Core\Logger\LogMessageParserInterface;
use Drupal\Core\TypedData\Plugin\DataType\Map;
use Drupal\Core\TypedData\TypedData;
use Drupal\Component\Utility\Random;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\MapDataDefinition;
use Drupal\Core\Url;
use Drupal\link\LinkItemInterface;

/**
* The logger entry data type.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we describe what this is? Example "Represents a log entry that is created from system log messages."

*
* Represents a log entry that is created from system log messages.
*
* @DataType(
* id = "logger_entry",
* label = @Translation("Logger entry"),
* definition_class = "\Drupal\rules\TypedData\LoggerEntryDataDefinition"
* )
*/
class LoggerEntry extends Map {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can look at examples for typed data in core: Language.php

/**
* The message's placeholders parser.
*
* @var \Drupal\Core\Logger\LogMessageParserInterface
*/
protected $parser;

/**
* {@inheritdoc}
*
* @todo: create a TypedData logger-entry object: https://www.drupal.org/node/2625238
*/
public function log($level, $message, array $context = array(), LogMessageParserInterface $parser) {
$this->parser = $parser;

// Remove any backtraces since they may contain an unserializable variable.
unset($context['backtrace']);

// Convert PSR3-style messages to SafeMarkup::format() style, so they can be
// translated too in runtime.
$message_placeholders = $this->parser->parseMessagePlaceholders($message, $context);

$logger_entry = array(
'uid' => $context['uid'],
'type' => $context['channel'],
'message' => $message,
'variables' => $message_placeholders,
'severity' => $level,
'link' => $context['link'],
'location' => $context['request_uri'],
'referer' => $context['referer'],
'hostname' => $context['ip'],
'timestamp' => $context['timestamp'],
);

$this->setValue($logger_entry);
}

// // Plain copy from \Drupal\rules\Logger\RulesLog
// protected function createLoggerEntryFromContext($log) {
// list($level, $message, $context) = $log;
//
// // Remove any backtraces since they may contain an unserializable variable.
// unset($context['backtrace']);
//
// // Convert PSR3-style messages to SafeMarkup::format() style, so they can be
// // translated too in runtime.
// $message_placeholders = $this->parser->parseMessagePlaceholders($message, $context);
//
// $logger_entry = array(
// 'uid' => $context['uid'],
// 'type' => $context['channel'],
// 'message' => $message,
// 'variables' => $message_placeholders,
// 'severity' => $level,
// 'link' => $context['link'],
// 'location' => $context['request_uri'],
// 'referer' => $context['referer'],
// 'hostname' => $context['ip'],
// 'timestamp' => $context['timestamp'],
// );
//
// return $logger_entry;
// }
}
20 changes: 20 additions & 0 deletions src/Plugin/DataType/MapDataDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* @file
* Contains \Drupal\rules\Plugin\DataType\LoggerEntryDataDefinition.
*/

namespace Drupal\rules\Plugin\DataType;

use Drupal\Core\TypedData\MapDataDefinition;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\Plugin\DataType\Map;
use Drupal\Core\TypedData\TypedDataInterface;

class LoggerEntryDataDefinition extends MapDataDefinition {

}
53 changes: 53 additions & 0 deletions src/TypedData/LoggerEntryDataDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* @file
* Contains \Drupal\rules\TypedData\LoggerEntryDataDefinition.
*/

namespace Drupal\rules\TypedData;

use Drupal\Core\TypedData\Plugin\DataType\Map;
use Drupal\Core\TypedData\TypedData;
use Drupal\Component\Utility\Random;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\MapDataDefinition;
use Drupal\Core\Url;
use Drupal\link\LinkItemInterface;


/**
* A typed data definition class for defining logger entries.
*/
class LoggerEntryDataDefinition extends MapDataDefinition {

/**
* Definitions of the contained properties.
*
* @see self::getPropertyDefinitions()
*
* @var array
*/
static $propertyDefinitions;

/**
* Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions().
*/
public function getPropertyDefinitions() {

if (!isset(static::$propertyDefinitions)) {
static::$propertyDefinitions['value'] = array(
'type' => 'integer',
'label' => t('Integer value'),
'class' => '\Drupal\comment\CommentNewValue',
'computed' => TRUE,
);
}
return static::$propertyDefinitions;
}

}
2 changes: 1 addition & 1 deletion tests/src/Integration/Event/SystemLoggerEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testSystemLoggerEvent() {
$logger_entry = $event->getContextDefinition('logger_entry');

// @todo: create a TypedData logger-entry object: https://www.drupal.org/node/2625238
$this->assertSame('any', $logger_entry->getDataType());
$this->assertSame('logger_entry', $logger_entry->getDataType());
}

}