|
| 1 | +How to Configure Monolog to Email Errors |
| 2 | +======================================== |
| 3 | + |
| 4 | +Monolog_ can be configured to send an email when an error occurs with an |
| 5 | +application. The configuration for this requires a few nested handlers |
| 6 | +in order to avoid receiving too many emails. This configuration looks |
| 7 | +complicated at first but each handler is fairly straight forward when |
| 8 | +it is broken down. |
| 9 | + |
| 10 | +.. configuration-block:: |
| 11 | + |
| 12 | + .. code-block:: yaml |
| 13 | +
|
| 14 | + monolog: |
| 15 | + handlers: |
| 16 | + mail: |
| 17 | + type: fingers_crossed |
| 18 | + action_level: error |
| 19 | + handler: buffered |
| 20 | + buffered: |
| 21 | + type: buffer |
| 22 | + handler: swift |
| 23 | + swift: |
| 24 | + type: swift_mailer |
| 25 | + from_email: error@example.com |
| 26 | + to_email: error@example.com |
| 27 | + subject: An Error Occurred! |
| 28 | + level: debug |
| 29 | +
|
| 30 | + .. code-block:: xml |
| 31 | +
|
| 32 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 33 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 34 | + xmlns:monolog="http://symfony.com/schema/dic/monolog" |
| 35 | + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd |
| 36 | + http://symfony.com/schema/dic/monolog http://symfony.com/schema/dic/monolog/monolog-1.0.xsd"> |
| 37 | +
|
| 38 | + <monolog:config> |
| 39 | + <monolog:handler |
| 40 | + name="mail" |
| 41 | + type="fingerscrossed" |
| 42 | + action-level="error" |
| 43 | + handler="buffered" |
| 44 | + /> |
| 45 | + <monolog:handler |
| 46 | + name="buffered" |
| 47 | + type="buffer" |
| 48 | + handler="swift" |
| 49 | + /> |
| 50 | + <monolog:handler |
| 51 | + name="swift" |
| 52 | + from-email="error@example.com" |
| 53 | + to-email="error@example.com" |
| 54 | + subject="An Error Occurred!" |
| 55 | + level="debug" |
| 56 | + /> |
| 57 | + </monolog:config> |
| 58 | + </container> |
| 59 | +
|
| 60 | +The ``mail`` handler is a ``fingerscrossed`` handler which means that |
| 61 | +it is only triggered when the action level, in this case ``error`` is reached. |
| 62 | +It then logs everything including messages below the action level. The |
| 63 | +``handler`` setting means that the output is then passed onto the ``buffered`` |
| 64 | +handler. |
| 65 | + |
| 66 | +The ``buffered`` handler simply keeps all the messages for a request and |
| 67 | +then passes them onto the nested handler in one go. If you do not use this |
| 68 | +handler then each message will be emailed separately. This is then passed |
| 69 | +to the ``swift`` handler. This is the handler that actually deals with |
| 70 | +emailing you the error. The settings for this are straightforward, the |
| 71 | +to and from addresses and the subject. |
| 72 | + |
| 73 | +You can combine these handlers with other handlers so that the errors still |
| 74 | +get logged on the server as well as the emails being sent: |
| 75 | + |
| 76 | +.. configuration-block:: |
| 77 | + |
| 78 | + .. code-block:: yaml |
| 79 | +
|
| 80 | + monolog: |
| 81 | + handlers: |
| 82 | + main: |
| 83 | + type: fingers_crossed |
| 84 | + action_level: error |
| 85 | + handler: nested |
| 86 | + nested: |
| 87 | + type: stream |
| 88 | + path: %kernel.logs_dir%/%kernel.environment%.log |
| 89 | + level: debug |
| 90 | + mail: |
| 91 | + type: fingers_crossed |
| 92 | + action_level: error |
| 93 | + handler: buffered |
| 94 | + buffered: |
| 95 | + type: buffer |
| 96 | + handler: swift |
| 97 | + swift: |
| 98 | + type: swift_mailer |
| 99 | + from_email: error@example.com |
| 100 | + to_email: error@example.com |
| 101 | + subject: An Error Occurred! |
| 102 | + level: debug |
| 103 | +
|
| 104 | + .. code-block:: xml |
| 105 | +
|
| 106 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 107 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 108 | + xmlns:monolog="http://symfony.com/schema/dic/monolog" |
| 109 | + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd |
| 110 | + http://symfony.com/schema/dic/monolog http://symfony.com/schema/dic/monolog/monolog-1.0.xsd"> |
| 111 | +
|
| 112 | + <monolog:config> |
| 113 | + <monolog:handler |
| 114 | + name="main" |
| 115 | + type="fingers_crossed" |
| 116 | + action_level="error" |
| 117 | + handler="nested" |
| 118 | +
B41A
<monolog:handler |
| 119 | + name="nested" |
| 120 | + type="stream" |
| 121 | + path="%kernel.logs_dir%/%kernel.environment%.log" |
| 122 | + level="debug" |
| 123 | + <monolog:handler |
| 124 | + name="mail" |
| 125 | + type="fingerscrossed" |
| 126 | + action-level="error" |
| 127 | + handler="buffered" |
| 128 | + /> |
| 129 | + <monolog:handler |
| 130 | + name="buffered" |
| 131 | + type="buffer" |
| 132 | + handler="swift" |
| 133 | + /> |
| 134 | + <monolog:handler |
| 135 | + name="swift" |
| 136 | + from-email="error@example.com" |
| 137 | + to-email="error@example.com" |
| 138 | + subject="An Error Occurred!" |
| 139 | + level="debug" |
| 140 | + /> |
| 141 | + </monolog:config> |
| 142 | + </container> |
| 143 | +
|
| 144 | +.. _Monolog: https://github.com/Seldaek/monolog |
0 commit comments