Completed
Push — 2761013-watchdog_pager ( d635f7...2f2a35 )
by Frédéric G.
03:11
created

Logger::templatesCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
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 = RfcLogLevel::DEBUG;
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
216
    // Only insert each template once per request.
217
    if ($this->requestTracking && !isset($this->templates[$template_id])) {
218
      $request_id = $this->requestStack
219
        ->getCurrentRequest()
220
        ->server
221
        ->get('UNIQUE_ID');
222
223
      $this->templates[$template_id] = 1;
224
      $track = [
225
        'request_id' => $request_id,
226
        'template_id' => $template_id,
227
      ];
228
      $this->trackerCollection()->insertOne($track);
229
    }
230
231
    $event_collection = $this->eventCollection($template_id);
232
    if ($template_result->getUpsertedCount()) {
233
      // Capped collections are actually size-based, not count-based, so "items"
234
      // is only a maximum, assuming event documents weigh 1kB, but the actual
235
      // number of items stored may be lower if items are heavier.
236
      // We do not use 'autoindexid' for greater speed, because:
237
      // - it does not work on replica sets,
238
      // - it is deprecated in MongoDB 3.2 and going away in 3.4.
239
      $options = [
240
        'capped' => TRUE,
241
        'size' => $this->items * 1024,
242
        'max' => $this->items,
243
      ];
244
      $this->database->createCollection($event_collection->getCollectionName(), $options);
245
246
      // Do not create this index by default, as its cost is useless if request
247
      // tracking is not enabled.
248
      if ($this->requestTracking) {
249
        $key = ['requestTracking_id' => 1];
250
        $options = ['name' => 'admin-by-request'];
251
        $event_collection->createIndex($key, $options);
252
      }
253
    }
254
255
    foreach ($message_placeholders as &$placeholder) {
256
      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...
257
        $placeholder = Xss::filterAdmin($placeholder);
258
      }
259
    }
260
    $event = [
261
      'hostname' => Unicode::substr($context['ip'], 0, 128),
262
      'link' => $context['link'],
263
      'location' => $context['request_uri'],
264
      'referer' => $context['referer'],
265
      'timestamp' => $context['timestamp'],
266
      'user' => ['uid' => $context['uid']],
267
      'variables' => $message_placeholders,
268
    ];
269
    if ($this->requestTracking) {
270
      // Fetch the current request on each event to support subrequest nesting.
271
      $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...
272
      $event['requestTracking_sequence'] = $this->sequence;
273
      $this->sequence++;
274
    }
275
    $event_collection->insertOne($event);
276
  }
277
278
  /**
279
   * Ensure a collection is capped with the proper size.
280
   *
281
   * @param string $name
282
   *   The collection name.
283
   * @param int $size
284
   *   The collection size cap.
285
   *
286
   * @return \MongoDB\Collection
287
   *   The collection, usable for additional commands like index creation.
288
   *
289
   * @TODO support sharded clusters: convertToCapped does not support them.
290
   *
291
   * @see https://docs.mongodb.com/manual/reference/command/convertToCapped
292
   *
293
   * Note that MongoDB 3.2 still misses a propert exists() command, which is the
294
   * reason for the weird try/catch logic.
295
   *
296
   * @see https://jira.mongodb.org/browse/SERVER-1938
297
   */
298
  public function ensureCappedCollection($name, $size) {
299
    try {
300
      $stats = $this->database->command([
301
        'collStats' => $name,
302
      ], static::LEGACY_TYPE_MAP)->toArray()[0];
303
    }
304
    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...
305
      // 59 is expected if the collection was not found. Other values are not.
306
      if ($e->getCode() !== 59) {
307
        throw $e;
308
      }
309
310
      $this->database->createCollection($name);
311
      $stats = $this->database->command([
312
        'collStats' => $name,
313
      ], static::LEGACY_TYPE_MAP)->toArray()[0];
314
    }
315
316
    $collection = $this->database->selectCollection($name);
317
    if (!empty($stats['capped'])) {
318
      return $collection;
319
    }
320
321
    $this->database->command([
322
      'convertToCapped' => $name,
323
      'size' => $size,
324
    ]);
325
    return $collection;
326
  }
327
328
  /**
329
   * Ensure indexes are set on the collections and tracker collection is capped.
330
   *
331
   * First index is on <line, timestamp> instead of <function, line, timestamp>,
332
   * because we write to this collection a lot, and the smaller index on two
333
   * numbers should be much faster to create than one with a string included.
334
   */
335
  public function ensureSchema() {
336
    $trackerCollection = $this->ensureCappedCollection(static::TRACKER_COLLECTION, $this->requests * 1024);
337
    $indexes = [
338
      [
339
        'name' => 'tracker-request',
340
        'key' => ['request_id' => 1],
341
      ],
342
    ];
343
    $trackerCollection->createIndexes($indexes);
344
345
    $indexes = [
346
      // Index for adding/updating increments.
347
      [
348
        'name' => 'for-increments',
349
        'key' => ['line' => 1, 'changed' => -1],
350
      ],
351
352
      // Index for overview page without filters.
353
      [
354
        'name' => 'overview-no-filters',
355
        'key' => ['changed' => -1],
356
      ],
357
358
      // Index for overview page filtering by type.
359
      [
360
        'name' => 'overview-by-type',
361
        'key' => ['type' => 1, 'changed' => -1],
362
      ],
363
364
      // Index for overview page filtering by severity.
365
      [
366
        'name' => 'overview-by-severity',
367
        'key' => ['severity' => 1, 'changed' => -1],
368
      ],
369
370
      // Index for overview page filtering by type and severity.
371
      [
372
        'name' => 'overview-by-both',
373
        'key' => ['type' => 1, 'severity' => 1, 'changed' => -1],
374
      ],
375
    ];
376
377
    $this->templateCollection()->createIndexes($indexes);
378
379
  }
380
381
  /**
382
   * Return a collection, given its template id.
383
   *
384
   * @param string $template_id
385
   *   The string representation of a template \MongoId.
386
   *
387
   * @return \MongoDB\Collection
388
   *   A collection object for the specified template id.
389
   */
390
  public function eventCollection($template_id) {
391
    $collection_name = static::EVENT_COLLECTION_PREFIX . $template_id;
392
    if (!preg_match('/' . static::EVENT_COLLECTIONS_PATTERN . '/', $collection_name)) {
393
      throw new InvalidArgumentException(t('Invalid watchdog template id `@id`.', [
394
        '@id' => $collection_name,
395
      ]));
396
    }
397
    $collection = $this->database->selectCollection($collection_name);
398
    return $collection;
399
  }
400
401
  /**
402
   * List the event collections.
403
   *
404
   * @return \MongoDB\Collection[]
405
   *   The collections with a name matching the event pattern.
406
   */
407
  public function eventCollections() {
408
    echo static::EVENT_COLLECTIONS_PATTERN;
409
    $options = [
410
      'filter' => [
411
        'name' => ['$regex' => static::EVENT_COLLECTIONS_PATTERN],
412
      ],
413
    ];
414
    $result = iterator_to_array($this->database->listCollections($options));
415
    return $result;
416
  }
417
418
  /**
419
   * Return the number of events for a template.
420
   *
421
   * @param \Drupal\mongodb_watchdog\EventTemplate $template
422
   *   A template for which to count events.
423
   *
424
   * @return int
425
   *   The number of matching events.
426
   */
427
  public function eventCount(EventTemplate $template) {
428
    return $this->eventCollection($template->_id)->count();
429
  }
430
431
  /**
432
   * Return the events having occurred during a given request.
433
   *
434
   * @param string $requestId
435
   *   The request unique_id.
436
   * @param int $skip
437
   *   The number of events to skip in the result.
438
   * @param int $limit
439
   *   The maximum number of events to return.
440
   *
441
   * @return array<\Drupal\mongodb_watchdog\EventTemplate\Drupal\mongodb_watchdog\Event[]>
442
   *   An array of [template, event] arrays, ordered by occurrence order.
443
   */
444
  public function requestEvents($requestId, $skip = 0, $limit = 0) {
445
    $templates = $this->requestTemplates($requestId);
446
    $selector = [
447
      'requestTracking_id' => $requestId,
448
      'requestTracking_sequence' => [
449
        '$gte' => $skip,
450
        '$lt' => $skip + $limit,
451
      ],
452
    ];
453
    $events = [];
454
    $options = [
455
      'typeMap' => [
456
        'array' => 'array',
457
        'document' => 'array',
458
        'root' => '\Drupal\mongodb_watchdog\Event',
459
      ],
460
    ];
461
462
    /**
463
     * @var string $template_id
464
     * @var \Drupal\mongodb_watchdog\EventTemplate $template
465
     */
466
    foreach ($templates as $template_id => $template) {
467
      $event_collection = $this->eventCollection($template_id);
468
      $cursor = $event_collection->find($selector, $options);
469
      /** @var \Drupal\mongodb_watchdog\Event $event */
470
      foreach ($cursor as $event) {
471
        $events[$event->requestTracking_sequence] = [
472
          $template,
473
          $event,
474
        ];
475
      }
476
    }
477
478
    ksort($events);
479
    return $events;
480
  }
481
482
  /**
483
   * Count events matching a request unique_id.
484
   *
485
   * XXX This implementation may be very inefficient in case of a request gone
486
   * bad generating non-templated varying messages: #requests is O(#templates).
487
   *
488
   * @param string $requestId
489
   *   The unique_id of the request.
490
   *
491
   * @return int
492
   *   The number of events matching the unique_id.
493
   */
494
  public function requestEventsCount($requestId) {
495
    if (empty($requestId)) {
496
      return 0;
497
    }
498
499
    $templates = $this->requestTemplates($requestId);
500
    $count = 0;
501
    foreach ($templates as $template) {
502
      $eventCollection = $this->eventCollection($template->_id);
503
      $selector = [
504
        'requestTracking_id' => $requestId,
505
      ];
506
      $count += $eventCollection->count($selector);
507
    }
508
509
    return $count;
510
  }
511
512
  /**
513
   * Return the number of event templates.
514
   */
515
  public function templatesCount() {
516
    return $this->templateCollection()->count([]);
517
  }
518
519
  /**
520
   * Return an array of templates uses during a given request.
521
   *
522
   * @param string $unsafe_request_id
523
   *   A request "unique_id".
524
   *
525
   * @return \Drupal\mongodb_watchdog\EventTemplate[]
526
   *   An array of EventTemplate instances.
527
   */
528
  public function requestTemplates($unsafe_request_id) {
529
    $request_id = "${unsafe_request_id}";
530
    $selector = [
531
      'request_id' => $request_id,
532
    ];
533
534
    $cursor = $this
535
      ->trackerCollection()
536
      ->find($selector, static::LEGACY_TYPE_MAP + [
537
        'projection' => [
538
          '_id' => 0,
539
          'template_id' => 1,
540
        ],
541
      ]);
542
    $template_ids = [];
543
    foreach ($cursor as $request) {
544
      $template_ids[] = $request['template_id'];
545
    }
546
    if (empty($template_ids)) {
547
      return [];
548
    }
549
550
    $selector = ['_id' => ['$in' => $template_ids]];
551
    $options = [
552
      'typeMap' => [
553
        'array' => 'array',
554
        'document' => 'array',
555
        'root' => '\Drupal\mongodb_watchdog\EventTemplate',
556
      ],
557
    ];
558
    $templates = [];
559
    $cursor = $this->templateCollection()->find($selector, $options);
560
    /** @var \Drupal\mongodb_watchdog\EventTemplate $template */
561
    foreach ($cursor as $template) {
562
      $templates[$template->_id] = $template;
563
    }
564
    return $templates;
565
  }
566
567
  /**
568
   * Return the request events tracker collection.
569
   *
570
   * @return \MongoDB\Collection
571
   *   The collection.
572
   */
573
  public function trackerCollection() {
574
    return $this->database->selectCollection(static::TRACKER_COLLECTION);
575
  }
576
577
  /**
578
   * Return the event templates collection.
579
   *
580
   * @return \MongoDB\Collection
581
   *   The collection.
582
   */
583
  public function templateCollection() {
584
    return $this->database->selectCollection(static::TEMPLATE_COLLECTION);
585
  }
586
587
  /**
588
   * Return templates matching type and level criteria.
589
   *
590
   * @param string[] $types
591
   *   An array of EventTemplate types. May be a hash.
592
   * @param string[]|int[] $levels
593
   *   An array of severity levels.
594
   * @param int $skip
595
   *   The number of templates to skip before the first one displayed.
596
   * @param int $limit
597
   *   The maximum number of templates to return.
598
   *
599
   * @return \MongoDB\Driver\Cursor
600
   *   A query result for the templates.
601
   */
602
  public function templates(array $types = [], array $levels = [], $skip = 0, $limit = 0) {
603
    $selector = [];
604
    if (!empty($types)) {
605
      $selector['type'] = ['$in' => array_values($types)];
606
    }
607
    if (!empty($levels) && count($levels) !== count(RfcLogLevel::getLevels())) {
608
      // Severity levels come back from the session as strings, not integers.
609
      $selector['severity'] = ['$in' => array_values(array_map('intval', $levels))];
610
    }
611
    $options = [
612
      'sort' => [
613
        'count' => -1,
614
        'changed' => -1,
615
      ],
616
      'typeMap' => [
617
        'array' => 'array',
618
        'document' => 'array',
619
        'root' => '\Drupal\mongodb_watchdog\EventTemplate',
620
      ],
621
    ];
622
    if ($skip) {
623
      $options['skip'] = $skip;
624
    }
625
    if ($limit) {
626
      $options['limit'] = $limit;
627
    }
628
629
    $cursor = $this->templateCollection()->find($selector, $options);
630
    return $cursor;
631
  }
632
633
  /**
634
   * Return the template types actually present in storage.
635
   *
636
   * @return string[]
637
   *   An array of distinct EventTemplate types.
638
   */
639
  public function templateTypes() {
640
    $ret = $this->templateCollection()->distinct('type');
641
    return $ret;
642
  }
643
644
}
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...
645