Skip to content

Commit e1dfe3b

Browse files
committed
DOCSP-43518: query logging
1 parent 4fd1b81 commit e1dfe3b

File tree

3 files changed

+139
-1
lines changed

3 files changed

+139
-1
lines changed

docs/fundamentals/read-operations.txt

+2-1
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:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
.. tabs::
46+
47+
.. tab:: Query Syntax
48+
:tabid: query-syntax
49+
50+
Use the following syntax to specify the query:
51+
52+
.. literalinclude:: /includes/fundamentals/read-operations/ReadOperationsTest.php
53+
:language: php
54+
:dedent:
55+
:start-after: start-query-log
56+
:end-before: end-query-log
57+
:emphasize-lines: 1, 7
58+
59+
.. tab:: Controller Method
60+
:tabid: controller
61+
62+
To see the logs in the ``browse_movies`` view, edit the ``show()`` function
63+
in the ``MovieController.php`` file to resemble the following code:
64+
65+
.. io-code-block::
66+
:copyable: true
67+
68+
.. input::
69+
:language: php
70+
71+
class MovieController
72+
{
73+
public function show()
74+
{
75+
DB::connection('mongodb')->enableQueryLog();
76+
77+
Movie::where('title', 'Carrie')->get();
78+
Movie::where('year', '<', 2005)->get();
79+
Movie::where('imdb.rating', '>', 8.5)->get();
80+
81+
$logs = DB::connection('mongodb')->getQueryLog();
82+
foreach ($logs as $log) {
83+
echo json_encode($log, JSON_PRETTY_PRINT);
84+
}
85+
}
86+
}
87+
88+
.. output::
89+
:language: none
90+
:visible: false
91+
92+
{
93+
"query": "{ \"find\" : \"movies\", \"filter\" : { \"title\" : \"Carrie\" } }",
94+
"bindings": [],
95+
"time": 29476
96+
}
97+
{
98+
"query": "{ \"find\" : \"movies\", \"filter\" : { \"year\" : { \"$lt\" : { \"$numberInt\" : \"2005\" } } } }",
99+
"bindings": [],
100+
"time": 29861
101+
}
102+
{
103+
"query": "{ \"find\" : \"movies\", \"filter\" : { \"imdb.rating\" : { \"$gt\" : { \"$numberDouble\" : \"8.5\" } } } }",
104+
"bindings": [],
105+
"time": 27251
106+
}
107+
108+
Additional Information
109+
----------------------
110+
111+
To learn more about connecting to MongoDB, see the
112+
:ref:`laravel-connect-to-mongodb`.
113+
114+
To learn how to retrieve data based on filter criteria, see the
115+
:ref:`laravel-fundamentals-read-retrieve` guide.

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

+22
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,26 @@ public function testReadPreference(): void
183183
$this->assertNotNull($movies);
184184
$this->assertCount(2, $movies);
185185
}
186+
187+
/**
188+
* @runInSeparateProcess
189+
* @preserveGlobalState disabled
190+
*/
191+
public function testQueryLog(): void
192+
{
193+
// start-query-log
194+
DB::connection('mongodb')->enableQueryLog();
195+
196+
Movie::where('title', 'Carrie')->get();
197+
Movie::where('year', '<', 2005)->get();
198+
Movie::where('imdb.rating', '>', 8.5)->get();
199+
200+
$logs = DB::connection('mongodb')->getQueryLog();
201+
foreach ($logs as $log) {
202+
echo json_encode($log, JSON_PRETTY_PRINT);
203+
}
204+
// end-query-log
205+
206+
$this->assertNotNull($logs);
207+
}
186208
}

0 commit comments

Comments
 (0)