@@ -179,15 +179,74 @@ to write logs using the :phpfunction:`syslog` function:
179
179
->level(LogLevel::ERROR);
180
180
};
181
181
182
- This defines a *stack * of handlers and each handler is called in the order that it's
183
- defined.
182
+ This defines a stack of handlers. Each handler can define a ``priority ``
183
+ (default ``0 ``) to control its position in the stack. Handlers with a higher
184
+ priority are called first, while those with the same priority keep the order in
185
+ which they are defined:
186
+
187
+ .. configuration-block ::
188
+
189
+ .. code-block :: yaml
190
+
191
+ # config/packages/prod/monolog.yaml
192
+ monolog :
193
+ handlers :
194
+ file_log :
195
+ type : stream
196
+ path : " %kernel.logs_dir%/%kernel.environment%.log"
197
+
198
+ syslog_handler :
199
+ type : syslog
200
+ priority : 10 # called first
201
+
202
+ .. code-block :: xml
203
+
204
+ <!-- config/packages/prod/monolog.xml -->
205
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
206
+ <container xmlns =" http://symfony.com/schema/dic/services"
207
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
208
+ xmlns : monolog =" http://symfony.com/schema/dic/monolog"
209
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
210
+ https://symfony.com/schema/dic/services/services-1.0.xsd
211
+ http://symfony.com/schema/dic/monolog
212
+ https://symfony.com/schema/dic/monolog/monolog-1.0.xsd" >
213
+
214
+ <monolog : config >
215
+ <monolog : handler name =" file_log"
216
+ type =" stream"
217
+ path =" %kernel.logs_dir%/%kernel.environment%.log"
218
+ />
219
+
220
+ <!-- called first -->
221
+ <monolog : handler name =" syslog_handler"
222
+ type =" syslog"
223
+ priority =" 10"
224
+ />
225
+ </monolog : config >
226
+ </container >
227
+
228
+ .. code-block :: php
229
+
230
+ // config/packages/prod/monolog.php
231
+ use Psr\Log\LogLevel;
232
+ use Symfony\Config\MonologConfig;
233
+
234
+ return static function (MonologConfig $monolog): void {
235
+ $monolog->handler('file_log')
236
+ ->type('stream')
237
+ ->path('%kernel.logs_dir%/%kernel.environment%.log')
238
+ ;
239
+
240
+ $monolog->handler('syslog_handler')
241
+ ->type('syslog')
242
+ ->priority(10) // called first
243
+ ;
244
+ };
184
245
185
246
.. note ::
186
247
187
- If you want to override the ``monolog `` configuration via another config
188
- file, you will need to redefine the entire ``handlers `` stack. The configuration
189
- from the two files cannot be merged because the order matters and a merge does
190
- not allow you to control the order.
248
+ When adding handlers in other configuration files, it's recommended to set
249
+ an explicit priority to ensure they are ordered as expected.
191
250
192
251
.. _logging-handler-fingers_crossed :
193
252
0 commit comments