Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7653289

Browse files
rustagirGromNaN
andauthored
DOCSP-43518: logging (#3316)
* DOCSP-43518: query logging * formatting * remove controller * tests * tests * tests * tests * test * test * test * test * test * formatting * Fix test on output --------- Co-authored-by: Jérôme Tamarelle <jerome.tamarelle@mongodb.com>
1 parent 522b2ff commit 7653289

File tree

3 files changed

+124
-1
lines changed

3 files changed

+124
-1
lines changed

‎docs/fundamentals/read-operations.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ Read Operations
1717
Retrieve Data </fundamentals/read-operations/retrieve>
1818
Search Text </fundamentals/read-operations/search-text>
1919
Modify Query Results </fundamentals/read-operations/modify-results>
20-
Set Read Preference </fundamentals/read-operations/read-pref>
20+
Read Preference </fundamentals/read-operations/read-pref>
21+
Query Logging </fundamentals/read-operations/query-logging>
2122

2223
.. contents:: On this page
2324
:local:
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
.. _laravel-query-logging:
2+
3+
====================
4+
Enable Query Logging
5+
====================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: monitoring, CRUD, code example
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 2
18+
:class: singlecol
19+
20+
Overview
21+
--------
22+
23+
In this guide, you can learn how to enable query logging in
24+
{+odm-long+}. Query logging can help you debug your queries and monitor
25+
database interactions.
26+
27+
.. include:: /includes/fundamentals/read-operations/before-you-get-started.rst
28+
29+
Enable Logs On a Connection
30+
---------------------------
31+
32+
To enable logs on a connection, you can use the ``enableQueryLog()``
33+
method on the ``DB`` facade. This method enables MongoDB command logging
34+
on any queries that you perform on the database connection.
35+
36+
After you enable query logging, any queries you perform are stored in
37+
memory. To retrieve the logs, use one of the following methods:
38+
39+
- ``getQueryLog()``: Returns a log of MongoDB queries
40+
- ``getRawQueryLog()``: Returns a log of raw MongoDB queries
41+
42+
The following example enables query logging, performs some queries, then
43+
prints the query log:
44+
45+
.. io-code-block::
46+
:copyable: true
47+
48+
.. input:: /includes/fundamentals/read-operations/ReadOperationsTest.php
49+
:language: php
50+
:dedent:
51+
:start-after: start-query-log
52+
:end-before: end-query-log
53+
:emphasize-lines: 1, 7
54+
55+
.. output::
56+
:language: json
57+
:visible: false
58+
59+
{
60+
"query": "{ \"find\" : \"movies\", \"filter\" : { \"title\" : \"Carrie\" } }",
61+
"bindings": [],
62+
"time": 29476
63+
}
64+
{
65+
"query": "{ \"find\" : \"movies\", \"filter\" : { \"year\" : { \"$lt\" : { \"$numberInt\" : \"2005\" } } } }",
66+
"bindings": [],
67+
"time": 29861
68+
}
69+
{
70+
"query": "{ \"find\" : \"movies\", \"filter\" : { \"imdb.rating\" : { \"$gt\" : { \"$numberDouble\" : \"8.5\" } } } }",
71+
"bindings": [],
72+
"time": 27251
73+
}
74+
75+
Additional Information
76+
----------------------
77+
78+
To learn more about connecting to MongoDB, see the
79+
:ref:`laravel-connect-to-mongodb`.
80+
81+
To learn how to retrieve data based on filter criteria, see the
82+
:ref:`laravel-fundamentals-read-retrieve` guide.

‎docs/includes/fundamentals/read-operations/ReadOperationsTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
use MongoDB\Driver\ReadPreference;
1010
use MongoDB\Laravel\Tests\TestCase;
1111

12+
use function json_encode;
13+
use function ob_get_flush;
14+
use function ob_start;
15+
16+
use const JSON_PRETTY_PRINT;
17+
use const PHP_EOL;
18+
1219
class ReadOperationsTest extends TestCase
1320
{
1421
protected function setUp(): void
@@ -183,4 +190,37 @@ public function testReadPreference(): void
183190
$this->assertNotNull($movies);
184191
$this->assertCount(2, $movies);
185192
}
193+
194+
/**
195+
* @runInSeparateProcess
196+
* @preserveGlobalState disabled
197+
*/
198+
public function testQueryLog(): void
199+
{
200+
$output = '';
201+
ob_start(function (string $buffer) use (&$output) {
202+
$output .= $buffer;
203+
});
204+
// start-query-log
205+
DB::connection('mongodb')->enableQueryLog();
206+
207+
Movie::where('title', 'Carrie')->get();
208+
Movie::where('year', '<', 2005)->get();
209+
Movie::where('imdb.rating', '>', 8.5)->get();
210+
211+
$logs = DB::connection('mongodb')->getQueryLog();
212+
foreach ($logs as $log) {
213+
echo json_encode($log, JSON_PRETTY_PRINT) . PHP_EOL;
214+
}
215+
216+
// end-query-log
217+
$output = ob_get_flush();
218+
$this->assertNotNull($logs);
219+
$this->assertNotEmpty($output);
220+
221+
$this->assertStringContainsString('"query": "{ \"find\" : \"movies\", \"filter\" : { \"title\" : \"Carrie\" } }"', $output);
222+
$this->assertStringContainsString('"query": "{ \"find\" : \"movies\", \"filter\" : { \"imdb.rating\" : { \"$gt\" : { \"$numberDouble\" : \"8.5\" } } } }"', $output);
223+
$this->assertStringContainsString('"query": "{ \"find\" : \"movies\", \"filter\" : { \"imdb.rating\" : { \"$gt\" : { \"$numberDouble\" : \"8.5\" } } } }"', $output);
224+
$this->assertMatchesRegularExpression('/"time": \d+/', $output);
225+
}
186226
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /