|
| 1 | +.. _mongosh-custom-log-entries: |
| 2 | + |
| 3 | +======================== |
| 4 | +Write Custom Log Entries |
| 5 | +======================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +You can write custom :ref:`log entries <mdb-shell-logs>` from your |
| 14 | +:ref:`MongoDB Shell scripts <mdb-shell-write-scripts>`. Custom log |
| 15 | +entries help with debugging, error handling, and alert you when the |
| 16 | +script performs specific functions. |
| 17 | + |
| 18 | +About this Task |
| 19 | +--------------- |
| 20 | + |
| 21 | +MongoDB Shell supports the following methods for custom log entries: |
| 22 | + |
| 23 | +- ``log.debug()`` |
| 24 | +- ``log.error()`` |
| 25 | +- ``log.fatal()`` |
| 26 | +- ``log.info()`` |
| 27 | +- ``log.warn()`` |
| 28 | + |
| 29 | +Steps |
| 30 | +----- |
| 31 | + |
| 32 | +.. procedure:: |
| 33 | + |
| 34 | + .. step:: Create a script that writes a custom log entry |
| 35 | + |
| 36 | + The following script inserts documents into the ``movies`` |
| 37 | + collection and writes a custom ``info`` log entry. If the script |
| 38 | + errors, it writes a custom ``error`` log entry instead. |
| 39 | + |
| 40 | + .. code-block:: javascript |
| 41 | + :emphasize-lines: 22,24 |
| 42 | + |
| 43 | + // connect-and-insert-with-log-entry.js |
| 44 | + |
| 45 | + try { |
| 46 | + db = connect( 'mongodb://localhost/myDatabase' ); |
| 47 | + |
| 48 | + db.movies.insertMany( [ |
| 49 | + { |
| 50 | + title: 'Titanic', |
| 51 | + year: 1997, |
| 52 | + genres: [ 'Drama', 'Romance' ] |
| 53 | + }, |
| 54 | + { |
| 55 | + title: 'Spirited Away', |
| 56 | + year: 2001, |
| 57 | + genres: [ 'Animation', 'Adventure', 'Family' ] |
| 58 | + }, |
| 59 | + { |
| 60 | + title: 'Casablanca', |
| 61 | + genres: [ 'Drama', 'Romance', 'War' ] |
| 62 | + } |
| 63 | + ] ) |
| 64 | + log.info('InsertData: Inserted 3 movies'); |
| 65 | + } catch (error) { |
| 66 | + log.error('Insert failed', { error: error.message }); |
| 67 | + } |
| 68 | + |
| 69 | + Save the script as ``connect-and-insert-with-log-entry.js``. |
| 70 | + |
| 71 | + .. step:: Run the script |
| 72 | + |
| 73 | + To run the ``connect-and-insert-with-log-entry.js`` script, use |
| 74 | + ``mongosh`` to connect to your deployment and run the following |
| 75 | + command within MongoDB Shell: |
| 76 | + |
| 77 | + .. code-block:: javascript |
| 78 | + |
| 79 | + load("connect-and-insert-with-log-entry.js") |
| 80 | + |
| 81 | + Alternatively, you can run the script programmatically by using |
| 82 | + the :option:`--file <mongosh --file>` option when you start |
| 83 | + ``mongosh``: |
| 84 | + |
| 85 | + .. code-block:: sh |
| 86 | + |
| 87 | + mongosh --file connect-and-insert-with-log-entry.js |
| 88 | + |
| 89 | +Results |
| 90 | +------- |
| 91 | + |
| 92 | +The custom log entry appears in the logs for your session: |
| 93 | + |
| 94 | +.. code-block:: javascript |
| 95 | + :copyable: false |
| 96 | + |
| 97 | + {"t":{"$date":"2025-02-25T18:04:01.690Z"},"s":"I","c":"MONGOSH-SCRIPTS","id":1000000054,"ctx":"custom-log","msg":"InsertData: Inserted 3 movies"} |
| 98 | + |
| 99 | +For more information about log sessions and how to retrieve log |
| 100 | +messages, see :ref:`mdb-shell-view-logs`. |
| 101 | + |
| 102 | +To verify that the script inserted the documents, query the ``movies`` |
| 103 | +collection: |
| 104 | + |
| 105 | +.. code-block:: javascript |
| 106 | + |
| 107 | + use myDatabase |
| 108 | + db.movies.find() |
| 109 | + |
| 110 | +Output: |
| 111 | + |
| 112 | +.. code-block:: javascript |
| 113 | + :copyable: false |
| 114 | + |
| 115 | + [ |
| 116 | + { |
| 117 | + _id: ObjectId('67bde8c2a527c6b1341979f2'), |
| 118 | + title: 'Titanic', |
| 119 | + year: 1997, |
| 120 | + genres: [ 'Drama', 'Romance' ] |
| 121 | + }, |
| 122 | + { |
| 123 | + _id: ObjectId('67bde8c2a527c6b1341979f3'), |
| 124 | + title: 'Spirited Away', |
| 125 | + year: 2001, |
| 126 | + genres: [ 'Animation', 'Adventure', 'Family' ] |
| 127 | + }, |
| 128 | + { |
| 129 | + _id: ObjectId('67bde8c2a527c6b1341979f4'), |
| 130 | + title: 'Casablanca', |
| 131 | + genres: [ 'Drama', 'Romance', 'War' ] |
| 132 | + } |
| 133 | + ] |
0 commit comments