Completed
Push — 17-watchdog-capped_tracker ( ded5c8 )
by Frédéric G.
02:40
created

Logger::ensureCappedCollection()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 19
c 1
b 0
f 1
nc 5
nop 2
dl 0
loc 29
rs 8.5806
1
<?php
2
3
namespace Drupal\mongodb_watchdog;
4
5
use Drupal\Component\Render\MarkupInterface;
6
use Drupal\Component\Utility\Unicode;
7
use Drupal\Component\Utility\Xss;
8
use Drupal\Core\Config\ConfigFactoryInterface;
9
use Drupal\Core\Logger\LogMessageParserInterface;
10
use Drupal\Core\Logger\RfcLogLevel;
11
use MongoDB\Database;
12
use MongoDB\Driver\Exception\InvalidArgumentException;
13
use MongoDB\Driver\Exception\RuntimeException;
14
use Psr\Log\AbstractLogger;
15
use Symfony\Component\HttpFoundation\RequestStack;
16
17
/**
18
 * Class Logger is a PSR/3 Logger using a MongoDB data store.
19
 *
20
 * @package Drupal\mongodb_watchdog
21
 */
22
class Logger extends AbstractLogger {
23
  const CONFIG_NAME = 'mongodb_watchdog.settings';
24
25
  const TRACKER_COLLECTION = 'watchdog_tracker';
26
  const TEMPLATE_COLLECTION = 'watchdog';
27
  const EVENT_COLLECTION_PREFIX = 'watchdog_event_';
28
  const EVENT_COLLECTIONS_PATTERN = '^watchdog_event_[[:xdigit:]]{32}$';
29
30
  const LEGACY_TYPE_MAP = [
31
    'typeMap' => [
32
      'array' => 'array',
33
      'document' => 'array',
34
      'root' => 'array',
35
    ],
36
  ];
37
38
  /**
39
   * The logger storage.
40
   *
41
   * @var \MongoDB\Database
42
   */
43
  protected $database;
44
45
  /**
46
   * The limit for the capped event collections.
47
   *
48
   * @var int
49
   */
50
  protected $items;
51
52
  /**
53
   * The minimum logging level.
54
   *
55
   * @var int
56
   */
57
  protected $limit;
58
59
  /**
60
   * The message's placeholders parser.
61
   *
62
   * @var \Drupal\Core\Logger\LogMessageParserInterface
63
   */
64
  protected $parser;
65
66
  /**
67
   * The "requests" setting.
68
   *
69
   * @var int
70
   */
71
  protected $requests;
72
73
  /**
74
   * An array of templates already used in this request.
75
   *
76
   * Used only with request tracking enabled.
77
   *
78
   * @var string[]
79
   */
80
  protected $templates = [];
81
82
  /**
83
   * A sequence number for log events during a request.
84
   *
85
   * @var int
86
   */
87
  protected $sequence = 0;
88
89
  /**
90
   * Logger constructor.
91
   *
92
   * @param \MongoDB\Database $database
93
   *   The database object.
94
   * @param \Drupal\Core\Logger\LogMessageParserInterface $parser
95
   *   The parser to use when extracting message variables.
96
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
97
   *   The core config_factory service.
98
   * @param \Symfony\Component\HttpFoundation\RequestStack $stack
99
   *   The core request_stack service.
100
   */
101
  public function __construct(Database $database, LogMessageParserInterface $parser, ConfigFactoryInterface $config_factory, RequestStack $stack) {
102
    $this->database = $database;
103
    $this->parser = $parser;
104
    $this->requestStack = $stack;
105
106
    $config = $config_factory->get(static::CONFIG_NAME);
107
    $this->limit = $config->get('limit');
108
    $this->items = $config->get('items');
109
    $this->requests = $config->get('requests');
110
    $this->requestTracking = $config->get('request_tracking');
111
  }
112
113
  /**
114
   * Fill in the log_entry function, file, and line.
115
   *
116
   * @param array $log_entry
117
   *   An event information to be logger.
118
   * @param array $backtrace
119
   *   A call stack.
120
   */
121
  protected function enhanceLogEntry(array &$log_entry, array $backtrace) {
122
    // Create list of functions to ignore in backtrace.
123
    static $ignored = array(
124
      'call_user_func_array' => 1,
125
      '_drupal_log_error' => 1,
126
      '_drupal_error_handler' => 1,
127
      '_drupal_error_handler_real' => 1,
128
      'Drupal\mongodb_watchdog\Logger::log' => 1,
129
      'Drupal\Core\Logger\LoggerChannel::log' => 1,
130
      'Drupal\Core\Logger\LoggerChannel::alert' => 1,
131
      'Drupal\Core\Logger\LoggerChannel::critical' => 1,
132
      'Drupal\Core\Logger\LoggerChannel::debug' => 1,
133
      'Drupal\Core\Logger\LoggerChannel::emergency' => 1,
134
      'Drupal\Core\Logger\LoggerChannel::error' => 1,
135
      'Drupal\Core\Logger\LoggerChannel::info' => 1,
136
      'Drupal\Core\Logger\LoggerChannel::notice' => 1,
137
      'Drupal\Core\Logger\LoggerChannel::warning' => 1,
138
    );
139
140
    foreach ($backtrace as $bt) {
141
      if (isset($bt['function'])) {
142
        $function = empty($bt['class']) ? $bt['function'] : $bt['class'] . '::' . $bt['function'];
143
        if (empty($ignored[$function])) {
144
          $log_entry['%function'] = $function;
145
          /* Some part of the stack, like the line or file info, may be missing.
146
           *
147
           * @see http://goo.gl/8s75df
148
           *
149
           * No need to fetch the line using reflection: it would be redundant
150
           * with the name of the function.
151
           */
152
          $log_entry['%line'] = isset($bt['line']) ? $bt['line'] : NULL;
153
          if (empty($bt['file'])) {
154
            $reflected_method = new \ReflectionMethod($function);
155
            $bt['file'] = $reflected_method->getFileName();
156
          }
157
158
          $log_entry['%file'] = $bt['file'];
159
          break;
160
        }
161
        elseif ($bt['function'] == '_drupal_exception_handler') {
162
          $e = $bt['args'][0];
163
          $this->enhanceLogEntry($log_entry, $e->getTrace());
164
        }
165
      }
166
    }
167
  }
168
169
  /**
170
   * {@inheritdoc}
171
   */
172
  public function log($level, $template, array $context = []) {
173
    if ($level > $this->limit) {
174
      return;
175
    }
176
177
    // Convert PSR3-style messages to SafeMarkup::format() style, so they can be
178
    // translated too in runtime.
179
    $message_placeholders = $this->parser->parseMessagePlaceholders($template, $context);
180
181
    // If code location information is all present, as for errors/exceptions,
182
    // then use it to build the message template id.
183
    $type = $context['channel'];
184
    $location_info = [
185
      '%type' => 1,
186
      '@message' => 1,
187
      '%function' => 1,
188
      '%file' => 1,
189
      '%line' => 1,
190
    ];
191
    if (!empty(array_diff_key($location_info, $message_placeholders))) {
192
      $this->enhanceLogEntry($message_placeholders, debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 10));
193
    }
194
    $file = $message_placeholders['%file'];
195
    $line = $message_placeholders['%line'];
196
    $function = $message_placeholders['%function'];
197
    $key = "${type}:${level}:${file}:${line}:${function}";
198
    $template_id = md5($key);
199
200
    $selector = ['_id' => $template_id];
201
    $update = [
202
      '$inc' => ['count' => 1],
203
      '$set' => [
204
        '_id' => $template_id,
205
        'message' => $template,
206
        'severity' => $level,
207
        'changed' => time(),
208
        'type' => Unicode::substr($context['channel'], 0, 64),
209
      ],
210
    ];
211
    $options = ['upsert' => TRUE];
212
    $template_result = $this->database
213
      ->selectCollection(static::TEMPLATE_COLLECTION)
214
      ->updateOne($selector, $update, $options);
215
    // Only add the template if if has not already been added.
216
    if ($this->requestTracking) {
217
      $request_id = $this->requestStack
218
        ->getCurrentRequest()
219
        ->server
220
        ->get('UNIQUE_ID');
221
222
      if (isset($this->templates[$template_id])) {
223
        $this->templates[$template_id]++;
224
      }
225
      else {
226
        $this->templates[$template_id] = 1;
227
        $selector = ['_id' => $request_id];
228
        $update = ['$addToSet' => ['templates' => $template_id]];
229
        $this->trackerCollection()->updateOne($selector, $update, $options);
230
      }
231
    }
232
233
    $event_collection = $this->eventCollection($template_id);
234
    if ($template_result->getUpsertedCount()) {
235
      // Capped collections are actually size-based, not count-based, so "items"
236
      // is only a maximum, assuming event documents weigh 1kB, but the actual
237
      // number of items stored may be lower if items are heavier.
238
      // We do not use 'autoindexid' for greater speed, because:
239
      // - it does not work on replica sets,
240
      // - it is deprecated in MongoDB 3.2 and going away in 3.4.
241
      $options = [
242
        'capped' => TRUE,
243
        'size' => $this->items * 1024,
244
        'max' => $this->items,
245
      ];
246
      $this->database->createCollection($event_collection->getCollectionName(), $options);
247
248
      // Do not create this index by default, as its cost is useless if request
249
      // tracking is not enabled.
250
      if ($this->requestTracking) {
251
        $key = ['requestTracking_id' => 1];
252
        $options = ['name' => 'admin-by-request'];
253
        $event_collection->createIndex($key, $options);
254
      }
255
    }
256
257
    foreach ($message_placeholders as &$placeholder) {
258
      if ($placeholder instanceof MarkupInterface) {
0 ignored issues
show
Bug introduced by
The class Drupal\Component\Render\MarkupInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
259
        $placeholder = Xss::filterAdmin($placeholder);
260
      }
261
    }
262
    $event = [
263
      'hostname' => Unicode::substr($context['ip'], 0, 128),
264
      'link' => $context['link'],
265
      'location' => $context['request_uri'],
266
      'referer' => $context['referer'],
267
      'timestamp' => $context['timestamp'],
268
      'user' => ['uid' => $context['uid']],
269
      'variables' => $message_placeholders,
270
    ];
271
    if ($this->requestTracking) {
272
      // Fetch the current request on each event to support subrequest nesting.
273
      $event['requestTracking_id'] = $request_id;
0 ignored issues
show
Bug introduced by
The variable $request_id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
274
      $event['requestTracking_sequence'] = $this->sequence;
275
      $this->sequence++;
276
    }
277
    $event_collection->insertOne($event);
278
  }
279
280
  /**
281
   * List the event collections.
282
   *
283
   * @return \MongoDB\Collection[]
284
   *   The collections with a name matching the event pattern.
285
   */
286
  public function eventCollections() {
287
    echo static::EVENT_COLLECTIONS_PATTERN;
288
    $options = [
289
      'filter' => [
290
        'name' => ['$regex' => static::EVENT_COLLECTIONS_PATTERN],
291
      ],
292
    ];
293
    $result = iterator_to_array($this->database->listCollections($options));
294
    return $result;
295
  }
296
297
  /**
298
   * Return a collection, given its template id.
299
   *
300
   * @param string $template_id
301
   *   The string representation of a template \MongoId.
302
   *
303
   * @return \MongoDB\Collection
304
   *   A collection object for the specified template id.
305
   */
306
  public function eventCollection($template_id) {
307
    $collection_name = static::EVENT_COLLECTION_PREFIX . $template_id;
308
    if (!preg_match('/' . static::EVENT_COLLECTIONS_PATTERN . '/', $collection_name)) {
309
      throw new InvalidArgumentException(t('Invalid watchdog template id `@id`.', [
310
        '@id' => $collection_name,
311
      ]));
312
    }
313
    $collection = $this->database->selectCollection($collection_name);
314
    return $collection;
315
  }
316
317
  /**
318
   * Ensure a collection is capped with the proper size.
319
   *
320
   * @param string $name
321
   *   The collection name.
322
   * @param int $size
323
   *   The collection size cap.
324
   *
325
   * @return \MongoDB\Collection
326
   *   The collection, usable for additional commands like index creation.
327
   *
328
   * @TODO support sharded clusters: convertToCapped does not support them.
329
   *
330
   * @see https://docs.mongodb.com/manual/reference/command/convertToCapped
331
   *
332
   * Note that MongoDB 3.2 still misses a propert exists() command, which is the
333
   * reason for the weird try/catch logic.
334
   *
335
   * @see https://jira.mongodb.org/browse/SERVER-1938
336
   */
337
  public function ensureCappedCollection($name, $size) {
338
    try {
339
      $stats = $this->database->command([
340
        'collStats' => $name,
341
      ], static::LEGACY_TYPE_MAP)->toArray()[0];
342
    }
343
    catch (RuntimeException $e) {
0 ignored issues
show
Bug introduced by
The class MongoDB\Driver\Exception\RuntimeException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
344
      // 59 is expected if the collection was not found. Other values are not.
345
      if ($e->getCode() !== 59) {
346
        throw $e;
347
      }
348
349
      $this->database->createCollection($name);
350
      $stats = $this->database->command([
351
        'collStats' => $name,
352
      ], static::LEGACY_TYPE_MAP)->toArray()[0];
353
    }
354
355
    $collection = $this->database->selectCollection($name);
356
    if (!empty($stats['capped'])) {
357
      return $collection;
358
    }
359
360
    $this->database->command([
361
      'convertToCapped' => $name,
362
      'size' => $size,
363
    ]);
364
    return $collection;
365
  }
366
367
  /**
368
   * Ensure indexes are set on the collections and tracker collection is capped.
369
   *
370
   * First index is on <line, timestamp> instead of <function, line, timestamp>,
371
   * because we write to this collection a lot, and the smaller index on two
372
   * numbers should be much faster to create than one with a string included.
373
   */
374
  public function ensureSchema() {
375
    $this->ensureCappedCollection(static::TRACKER_COLLECTION, $this->requests * 1024);
376
377
    $indexes = [
378
      // Index for adding/updating increments.
379
      [
380
        'name' => 'for-increments',
381
        'key' => ['line' => 1, 'changed' => -1],
382
      ],
383
384
      // Index for overview page without filters.
385
      [
386
        'name' => 'overview-no-filters',
387
        'key' => ['changed' => -1],
388
      ],
389
390
      // Index for overview page filtering by type.
391
      [
392
        'name' => 'overview-by-type',
393
        'key' => ['type' => 1, 'changed' => -1],
394
      ],
395
396
      // Index for overview page filtering by severity.
397
      [
398
        'name' => 'overview-by-severity',
399
        'key' => ['severity' => 1, 'changed' => -1],
400
      ],
401
402
      // Index for overview page filtering by type and severity.
403
      [
404
        'name' => 'overview-by-both',
405
        'key' => ['type' => 1, 'severity' => 1, 'changed' => -1],
406
      ],
407
    ];
408
409
    $this->templateCollection()->createIndexes($indexes);
410
411
  }
412
413
  /**
414
   * Return the events having occurred during a given request.
415
   *
416
   * @param string $unsafe_request_id
417
   *   The raw request_id.
418
   *
419
   * @return array<\Drupal\mongodb_watchdog\EventTemplate\Drupal\mongodb_watchdog\Event[]>
420
   *   An array of [template, event] arrays, ordered by occurrence order.
421
   */
422
  public function requestEvents($unsafe_request_id) {
423
    $templates = $this->requestTemplates($unsafe_request_id);
424
    $request_id = "$unsafe_request_id";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $unsafe_request_id instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
425
    $selector = ['requestTracking_id' => $request_id];
426
    $events = [];
427
    $options = [
428
      'typeMap' => [
429
        'array' => 'array',
430
        'document' => 'array',
431
        'root' => '\Drupal\mongodb_watchdog\Event',
432
      ],
433
    ];
434
435
    /**
436
     * @var string $template_id
437
     * @var \Drupal\mongodb_watchdog\EventTemplate $template
438
     */
439
    foreach ($templates as $template_id => $template) {
440
      $event_collection = $this->eventCollection($template_id);
441
      $cursor = $event_collection->find($selector, $options);
442
      /** @var \Drupal\mongodb_watchdog\Event $event */
443
      foreach ($cursor as $event) {
444
        $events[$event->requestTracking_sequence] = [
445
          $template,
446
          $event,
447
        ];
448
      }
449
    }
450
451
    return $events;
452
  }
453
454
  /**
455
   * Return an array of templates uses during a given request.
456
   *
457
   * @param string $unsafe_request_id
458
   *   A request "unique_id".
459
   *
460
   * @return array
461
   *   An array of EventTemplate instances.
462
   */
463
  public function requestTemplates($unsafe_request_id) {
464
    $request_id = "${unsafe_request_id}";
465
    $selector = ['_id' => $request_id];
466
467
    $doc = $this->trackerCollection()->findOne($selector, static::LEGACY_TYPE_MAP);
468
    if (empty($doc) || empty($doc['templates'])) {
469
      return [];
470
    }
471
472
    $selector = ['_id' => ['$in' => $doc['templates']]];
473
    $options = [
474
      'typeMap' => [
475
        'array' => 'array',
476
        'document' => 'array',
477
        'root' => '\Drupal\mongodb_watchdog\EventTemplate',
478
      ],
479
    ];
480
    $templates = [];
481
    $cursor = $this->templateCollection()->find($selector, $options);
482
    /** @var \Drupal\mongodb_watchdog\EventTemplate $template */
483
    foreach ($cursor as $template) {
484
      $templates[$template->_id] = $template;
485
    }
486
    return $templates;
487
  }
488
489
  /**
490
   * Return the request events tracker collection.
491
   *
492
   * @return \MongoDB\Collection
493
   *   The collection.
494
   */
495
  public function trackerCollection() {
496
    return $this->database->selectCollection(static::TRACKER_COLLECTION);
497
  }
498
499
  /**
500
   * Return the event templates collection.
501
   *
502
   * @return \MongoDB\Collection
503
   *   The collection.
504
   */
505
  public function templateCollection() {
506
    return $this->database->selectCollection(static::TEMPLATE_COLLECTION);
507
  }
508
509
  /**
510
   * Return templates matching type and level criteria.
511
   *
512
   * @param string[] $types
513
   *   An array of EventTemplate types. May be a hash.
514
   * @param string[]|int[] $levels
515
   *   An array of severity levels.
516
   *
517
   * @return \MongoDB\Driver\Cursor
518
   *   A query result for the templates.
519
   */
520
  public function templates(array $types = [], array $levels = []) {
521
    $selector = [];
522
    if (!empty($types)) {
523
      $selector['type'] = ['$in' => array_values($types)];
524
    }
525
    if (!empty($levels) && count($levels) !== count(RfcLogLevel::getLevels())) {
526
      // Severity levels come back from the session as strings, not integers.
527
      $selector['severity'] = ['$in' => array_values(array_map('intval', $levels))];
528
    }
529
    $options = [
530
      'sort' => [
531
        'count' => -1,
532
        'changed' => -1,
533
      ],
534
      'typeMap' => [
535
        'array' => 'array',
536
        'document' => 'array',
537
        'root' => '\Drupal\mongodb_watchdog\EventTemplate',
538
      ],
539
    ];
540
541
    $cursor = $this->templateCollection()->find($selector, $options);
542
    return $cursor;
543
  }
544
545
  /**
546
   * Return the template types actually present in storage.
547
   *
548
   * @return string[]
549
   *   An array of distinct EventTemplate types.
550
   */
551
  public function templateTypes() {
552
    $ret = $this->templateCollection()->distinct('type');
553
    return $ret;
554
  }
555
556
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
557