AI 에이전트의 경우: 문서 인덱스는 https://www.mongodb.com/ko-kr/docs/llms.txt에서 사용할 수 있으며, 모든 페이지의 마크다운 버전은 어떤 URL 경로에 .md를 추가하여 사용할 수 있습니다.

MongoDB\Client::addSubscriber()

버전 1.18에 추가 되었습니다.

MongoDB\Client::addSubscriber()

이 클라이언트에 모니터링 이벤트 구독자를 등록합니다. 구독자는 이 클라이언트에 대한 모든 이벤트에 대한 알림을 받게 됩니다.

function addSubscriber(MongoDB\Driver\Monitoring\Subscriber $subscriber): void
$subscriber : MongoDB\ub4dc라이버\ubaa8니터링\uad6c독자
이 클라이언트에 등록할 모니터링 이벤트 구독자입니다.

MongoDB\Exception\InvalidArgumentException 매개변수 또는 옵션의 구문 분석과 관련된 오류의 경우입니다.

구독자가 MongoDB\ 드라이버\Monitoring\LogSubscriber인 경우, MongoDB\ 드라이버\Exception\InvalidArgumentException, 로거는 MongoDB\ 드라이버\Monitoring\addSubscriber를통해서만 전역적으로 등록할 수 있기 때문입니다.

$subscriber 이(가) 이 클라이언트에 이미 등록된 경우 이 함수는 작동하지 않습니다. $subscriber 이(가) 전 세계적으로 등록된 경우에도 이 클라이언트에 대한 각 이벤트에 대해 한 번만 알림을 받습니다.

모든 이벤트를 기록하는 MongoDB\ 드라이버\Monitoring\CommandSubscriber 를 만듭니다.

<?php use MongoDB\Driver\Monitoring\CommandSubscriber;use MongoDB\Driver\Monitoring\CommandStartedEvent;use MongoDB\Driver\Monitoring\CommandSucceededEvent;use MongoDB\Driver\Monitoring\CommandFailedEvent; class LogCommandSubscriber implements CommandSubscriber{ private $stream; public function __construct($stream) { $this->stream = $stream; } public function commandStarted(CommandStartedEvent $event): void { fwrite($this->stream, sprintf( 'Started command #%d "%s": %s%s', $event->getRequestId(), $event->getCommandName(), Document::fromPHP($event->getCommand())->toCanonicalExtendedJSON(), PHP_EOL, )); } public function commandSucceeded(CommandSucceededEvent $event): void { fwrite($this->stream, sprintf( 'Succeeded command #%d "%s" in %d microseconds: %s%s', $event->getRequestId(), $event->getCommandName(), $event->getDurationMicros(), json_encode($event->getReply()), PHP_EOL, )); } public function commandFailed(CommandFailedEvent $event): void { fwrite($this->stream, sprintf( 'Failed command #%d "%s" in %d microseconds: %s%s', $event->getRequestId(), $event->getCommandName(), $event->getDurationMicros(), $event->getError()->getMessage(), PHP_EOL, )); }}

그런 다음 구독자는 클라이언트에 등록할 수 있습니다.

<?php $client = new MongoDB\Client();$subscriber = new LogCommandSubscriber(STDERR); $client->addSubscriber($subscriber); $client->test->users->insertOne(['username' => 'alice']);

위의 코드는 stderr 출력에 다음을 쓰기 (write) 합니다.

Started command #1 "insert": { "insert" : "users", "ordered" : true, "$db" : "test", "lsid" : { "id" : { "$binary" : { "base64" : "dKTBhZD7Qvi0vUhvR58mCA==", "subType" : "04" } } }, "documents" : [ { "username" : "alice", "_id" : { "$oid" : "655d1fca12e81018340a4fc2" } } ] }Succeeded command #1 "insert" in 876 microseconds: {"n":1,"ok":1}