|
| 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. |
0 commit comments