@@ -179,15 +179,75 @@ to write logs using the :phpfunction:`syslog` function:
179179 ->level(LogLevel::ERROR);
180180 };
181181
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, where each handler can define
183+ a ``priority `` (default ``0 ``) to control its position in the stack.
184+
185+ Handlers with a higher priority are called earlier, while those with the same priority keep
186+ the order in which they are defined:
187+
188+ .. configuration-block ::
189+
190+ .. code-block :: yaml
191+
192+ # config/packages/prod/monolog.yaml
193+ monolog :
194+ handlers :
195+ file_log :
196+ type : stream
197+ path : " %kernel.logs_dir%/%kernel.environment%.log"
198+
199+ syslog_handler :
200+ type : syslog
201+ priority : 10 # called first
202+
203+ .. code-block :: xml
204+
205+ <!-- config/packages/prod/monolog.xml -->
206+ <?xml version =" 1.0" encoding =" UTF-8" ?>
207+ <container xmlns =" http://symfony.com/schema/dic/services"
208+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
209+ xmlns : monolog =" http://symfony.com/schema/dic/monolog"
210+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
211+ https://symfony.com/schema/dic/services/services-1.0.xsd
212+ http://symfony.com/schema/dic/monolog
213+ https://symfony.com/schema/dic/monolog/monolog-1.0.xsd" >
214+
215+ <monolog : config >
216+ <monolog : handler name =" file_log"
217+ type =" stream"
218+ path =" %kernel.logs_dir%/%kernel.environment%.log"
219+ />
220+
221+ <!-- called first -->
222+ <monolog : handler name =" syslog_handler"
223+ type =" syslog"
224+ priority =" 10"
225+ />
226+ </monolog : config >
227+ </container >
228+
229+ .. code-block :: php
230+
231+ // config/packages/prod/monolog.php
232+ use Psr\Log\LogLevel;
233+ use Symfony\Config\MonologConfig;
234+
235+ return static function (MonologConfig $monolog): void {
236+ $monolog->handler('file_log')
237+ ->type('stream')
238+ ->path('%kernel.logs_dir%/%kernel.environment%.log')
239+ ;
240+
241+ $monolog->handler('syslog_handler')
242+ ->type('syslog')
243+ ->priority(10) // called first
244+ ;
245+ };
184246
185247 .. note ::
186248
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.
249+ When adding handlers in additional configuration files, it's recommended to set
250+ an explicit priority to ensure the desired order.
191251
192252.. _logging-handler-fingers_crossed :
193253
0 commit comments