-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuceap_logging.module
More file actions
221 lines (195 loc) · 6.51 KB
/
uceap_logging.module
File metadata and controls
221 lines (195 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* @file
* Provides logging for HTTP requests and entity CRUD operations.
*/
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_entity_insert().
*
* Log all content entity insertions to watchdog.
*/
function uceap_logging_entity_insert(EntityInterface $entity) {
if (!$entity instanceof ContentEntityInterface) {
return;
}
$context = _uceap_logging_build_entity_log_context($entity, 'create');
\Drupal::logger('uceap_entity_crud')->info('Created @type (@bundle): @label (ID: @id)', $context);
}
/**
* Implements hook_entity_update().
*
* Log all content entity updates to watchdog.
*/
function uceap_logging_entity_update(EntityInterface $entity) {
if (!$entity instanceof ContentEntityInterface) {
return;
}
$context = _uceap_logging_build_entity_log_context($entity, 'update');
// Track changed fields if original entity is available.
if (isset($entity->original) && $entity->original instanceof ContentEntityInterface) {
$changes = _uceap_logging_get_entity_field_changes($entity, $entity->original);
if (!empty($changes)) {
$context['field_changes'] = $changes;
$context['@change_count'] = count($changes);
\Drupal::logger('uceap_entity_crud')->info('Updated @type (@bundle): @label (ID: @id) | @change_count field(s) changed', $context);
return;
}
}
\Drupal::logger('uceap_entity_crud')->info('Updated @type (@bundle): @label (ID: @id) | No field changes detected', $context);
}
/**
* Implements hook_entity_delete().
*
* Log all content entity deletions to watchdog.
*/
function uceap_logging_entity_delete(EntityInterface $entity) {
if (!$entity instanceof ContentEntityInterface) {
return;
}
$context = _uceap_logging_build_entity_log_context($entity, 'delete');
\Drupal::logger('uceap_entity_crud')->warning('Deleted @type (@bundle): @label (ID: @id)', $context);
}
/**
* Build structured logging context for entity operations.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity being logged.
* @param string $operation
* The operation type: 'create', 'update', or 'delete'.
*
* @return array
* Context array for logger with both message placeholders and structured data.
*/
function _uceap_logging_build_entity_log_context(ContentEntityInterface $entity, string $operation) {
$entity_type = $entity->getEntityTypeId();
$bundle = $entity->bundle();
$label = $entity->label() ?: 'N/A';
$id = $entity->id();
return [
// Message placeholders (prefixed with @).
'@type' => $entity_type,
'@bundle' => $bundle,
'@label' => $label,
'@id' => $id,
// Structured data for CloudWatch (no prefix).
'entity_type' => $entity_type,
'bundle' => $bundle,
'entity_id' => $id,
'operation' => $operation,
];
}
/**
* Get changed fields between two entity states.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The updated entity.
* @param \Drupal\Core\Entity\ContentEntityInterface $original
* The original entity.
*
* @return array
* Array of changes keyed by field name, with 'old' and 'new' values.
*/
function _uceap_logging_get_entity_field_changes(ContentEntityInterface $entity, ContentEntityInterface $original) {
// Fields to completely exclude from change tracking.
$excluded_fields = [
'changed',
'revision_timestamp',
'revision_translation_affected',
'revision_uid',
'revision_log',
];
// Sensitive fields that should have values masked in logs.
$config = \Drupal::config('uceap_logging.settings');
$sensitive_fields = $config->get('sensitive_fields') ?? [];
$changes = [];
$field_definitions = $entity->getFieldDefinitions();
foreach ($field_definitions as $field_name => $field_definition) {
// Skip computed and internal fields (they are derived values).
if ($field_definition->isComputed() || $field_definition->isInternal()) {
continue;
}
// Skip excluded fields entirely.
if (in_array($field_name, $excluded_fields)) {
continue;
}
// Check if field exists on both entities.
if (!$entity->hasField($field_name) || !$original->hasField($field_name)) {
continue;
}
$new_value = $entity->get($field_name)->getValue();
$old_value = $original->get($field_name)->getValue();
// Compare values.
if ($new_value !== $old_value) {
// Mask sensitive field values instead of skipping them.
$is_sensitive = in_array($field_name, $sensitive_fields);
$changes[$field_name] = [
'old' => $is_sensitive ? '****MASKED****' : _uceap_logging_format_field_value($old_value),
'new' => $is_sensitive ? '****MASKED****' : _uceap_logging_format_field_value($new_value),
];
}
}
return $changes;
}
/**
* Helper function to format field values for logging.
*
* @param mixed $value
* The field value (typically an array from getValue()).
*
* @return string
* A formatted string representation of the value.
*/
function _uceap_logging_format_field_value($value) {
if (empty($value)) {
return 'empty';
}
// If it's an array, format it appropriately.
if (is_array($value)) {
// For single-value fields, just show the value.
if (count($value) === 1 && isset($value[0])) {
$item = $value[0];
// Handle common field types.
if (isset($item['value'])) {
// Truncate long text values.
$val = (string) $item['value'];
return strlen($val) > 100 ? substr($val, 0, 100) . '...' : $val;
}
elseif (isset($item['target_id'])) {
return "ID:{$item['target_id']}";
}
elseif (isset($item['uri'])) {
return $item['uri'];
}
else {
// For other complex items, show key fields.
$parts = [];
foreach ($item as $key => $val) {
if (!is_array($val) && !is_object($val)) {
$parts[] = "$key:$val";
}
}
return implode(',', $parts);
}
}
// For multi-value fields, show count and summary.
else {
$count = count($value);
$first_item = reset($value);
if (isset($first_item['target_id'])) {
$ids = array_column($value, 'target_id');
return "$count items [IDs:" . implode(',', array_slice($ids, 0, 5)) . (count($ids) > 5 ? '...' : '') . ']';
}
elseif (isset($first_item['value'])) {
return "$count values";
}
else {
return "$count items";
}
}
}
// For scalar values.
$str = (string) $value;
return strlen($str) > 100 ? substr($str, 0, 100) . '...' : $str;
}