Skip to content

Commit 3a60c74

Browse files
committed
Added condition to check length for Request URI
1 parent cc022f7 commit 3a60c74

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Entity/AccessLog.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
*/
3232
class AccessLog extends ContentEntityBase implements AccessLogInterface {
3333

34+
/**
35+
* Max length for Request URI field.
36+
*/
37+
const REQUEST_URI_FIELD_MAX_LENGTH = 255;
38+
3439
/**
3540
* {@inheritdoc}
3641
*/
@@ -96,7 +101,10 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
96101
->setLabel(t('Request URI'))
97102
->setDescription(t('URI from which the request has been made.'))
98103
->setRequired(TRUE)
99-
->setReadOnly(TRUE);
104+
->setReadOnly(TRUE)
105+
->setSettings([
106+
'max_length' => self::REQUEST_URI_FIELD_MAX_LENGTH,
107+
]);
100108

101109
// Created field.
102110
$fields['created'] = BaseFieldDefinition::create('created')

src/Logger/AccessLogsDbLog.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Drupal\os2web_logging\Logger;
44

55
use Drupal\Core\Logger\RfcLoggerTrait;
6+
use Drupal\Core\StringTranslation\StringTranslationTrait;
67
use Drupal\os2web_logging\Entity\AccessLog;
78
use Psr\Log\LoggerInterface;
89

@@ -11,11 +12,18 @@
1112
*/
1213
class AccessLogsDbLog implements LoggerInterface {
1314
use RfcLoggerTrait;
15+
use StringTranslationTrait;
1416

1517
/**
1618
* {@inheritdoc}
1719
*/
1820
public function log($level, $message, array $context = []) {
21+
if (strlen($context['request_uri']) > AccessLog::REQUEST_URI_FIELD_MAX_LENGTH) {
22+
$request_uri = $context['request_uri'];
23+
$context['request_uri'] = substr($request_uri, 0, AccessLog::REQUEST_URI_FIELD_MAX_LENGTH);
24+
$message .= PHP_EOL;
25+
$message .= $this->t('Request URI has been truncated due to max_length restriction.') . PHP_EOL;
26+
}
1927
AccessLog::create([
2028
'sid' => $context['sid'],
2129
'uid' => $context['uid'],

0 commit comments

Comments
 (0)