|
| 1 | +================================ |
| 2 | +MongoDB\\Client::addSubscriber() |
| 3 | +================================ |
| 4 | + |
| 5 | +.. versionadded:: 1.18 |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 1 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | +Definition |
| 16 | +---------- |
| 17 | + |
| 18 | +.. phpmethod:: MongoDB\\Client::addSubscriber() |
| 19 | + |
| 20 | + Registers a monitoring event subscriber with this Client. The subscriber |
| 21 | + will be notified of all events for this Client. |
| 22 | + |
| 23 | + .. code-block:: php |
| 24 | + |
| 25 | + function addSubscriber(MongoDB\Driver\Monitoring\Subscriber $subscriber): void |
| 26 | + |
| 27 | + This method has the following parameters: |
| 28 | + |
| 29 | + .. include:: /includes/apiargs/MongoDBClient-method-addSubscriber-param.rst |
| 30 | + |
| 31 | + .. note:: |
| 32 | + |
| 33 | + If ``$subscriber`` is already registered with this Client, this function |
| 34 | + is a no-op. If ``$subscriber`` is also registered globally, it will still |
| 35 | + only be notified once of each event for this Client. |
| 36 | + |
| 37 | +Errors/Exceptions |
| 38 | +----------------- |
| 39 | + |
| 40 | +.. include:: /includes/extracts/error-invalidargumentexception.rst |
| 41 | + |
| 42 | +:php:`MongoDB\\Driver\\Exception\\InvalidArgumentException <mongodb-driver-exception-invalidargumentexception>` |
| 43 | +if subscriber is a :php:`MongoDB\\Driver\\Monitoring\\LogSubscriber <class.mongodb-driver-monitoring-logsubscriber>`, |
| 44 | +since loggers can only be registered globally with |
| 45 | +:php:`MongoDB\\Driver\\Monitoring\\addSubscriber <function.mongodb.driver.monitoring.addsubscriber>`. |
| 46 | + |
| 47 | +Example |
| 48 | +------- |
| 49 | + |
| 50 | +Create a :phpclass:`MongoDB\\Driver\\Monitoring\\CommandSubscriber` that |
| 51 | +logs all events: |
| 52 | + |
| 53 | +.. code-block:: php |
| 54 | + |
| 55 | + <?php |
| 56 | + |
| 57 | + use MongoDB\Driver\Monitoring\CommandSubscriber; |
| 58 | + use MongoDB\Driver\Monitoring\CommandStartedEvent; |
| 59 | + use MongoDB\Driver\Monitoring\CommandSucceededEvent; |
| 60 | + use MongoDB\Driver\Monitoring\CommandFailedEvent; |
| 61 | + |
| 62 | + class LogCommandSubscriber implements CommandSubscriber |
| 63 | + { |
| 64 | + private $stream; |
| 65 | + public function __construct($stream) |
| 66 | + { |
| 67 | + $this->stream = $stream; |
| 68 | + } |
| 69 | + |
| 70 | + public function commandStarted(CommandStartedEvent $event): void |
| 71 | + { |
| 72 | + fwrite($this->stream, sprintf( |
| 73 | + 'Started command #%d "%s": %s%s', |
| 74 | + $event->getRequestId(), |
| 75 | + $event->getCommandName(), |
| 76 | + Document::fromPHP($event->getCommand())->toCanonicalExtendedJSON(), |
| 77 | + PHP_EOL, |
| 78 | + )); |
| 79 | + } |
| 80 | + |
| 81 | + public function commandSucceeded(CommandSucceededEvent $event): void |
| 82 | + { |
| 83 | + fwrite($this->stream, sprintf( |
| 84 | + 'Succeeded command #%d "%s" in %d microseconds: %s%s', |
| 85 | + $event->getRequestId(), |
| 86 | + $event->getCommandName(), |
| 87 | + $event->getDurationMicros(), |
| 88 | + json_encode($event->getReply()), |
| 89 | + PHP_EOL, |
| 90 | + )); |
| 91 | + } |
| 92 | + |
| 93 | + public function commandFailed(CommandFailedEvent $event): void |
| 94 | + { |
| 95 | + fwrite($this->stream, sprintf( |
| 96 | + 'Failed command #%d "%s" in %d microseconds: %s%s', |
| 97 | + $event->getRequestId(), |
| 98 | + $event->getCommandName(), |
| 99 | + $event->getDurationMicros(), |
| 100 | + $event->getError()->getMessage(), |
| 101 | + PHP_EOL, |
| 102 | + )); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | +The subscriber can then be registered with a Client: |
| 107 | + |
| 108 | +.. code-block:: php |
| 109 | + |
| 110 | + <?php |
| 111 | + |
| 112 | + $client = new MongoDB\Client(); |
| 113 | + $subscriber = new LogCommandSubscriber(STDERR); |
| 114 | + |
| 115 | + $client->addSubscriber($subscriber); |
| 116 | + |
| 117 | + $client->test->users->insertOne(['username' => 'alice']); |
| 118 | + |
| 119 | +The above code will write the following to stderr output: |
| 120 | + |
| 121 | +.. code-block:: text |
| 122 | + |
| 123 | + Started command #1 "insert": { "insert" : "users", "ordered" : true, "$db" : "test", "lsid" : { "id" : { "$binary" : { "base64" : "dKTBhZD7Qvi0vUhvR58mCA==", "subType" : "04" } } }, "documents" : [ { "username" : "alice", "_id" : { "$oid" : "655d1fca12e81018340a4fc2" } } ] } |
| 124 | + Succeeded command #1 "insert" in 876 microseconds: {"n":1,"ok":1} |
| 125 | + |
| 126 | +See Also |
| 127 | +-------- |
| 128 | + |
| 129 | +- :phpmethod:`MongoDB\\Client::removeSubscriber()` |
| 130 | +- :php:`Application Performance Monitoring (APM) <manual/en/mongodb.tutorial.apm>` |
| 131 | +- :php:`MongoDB\\Driver\\Manager::addSubscriber() <manual/en/mongodb-driver-manager.addsubscriber>` |
| 132 | +- :php:`MongoDB\Driver\Monitoring\CommandSubscriber <manual/en/class.mongodb-driver-monitoring-commandsubscriber>` |
0 commit comments