File tree Expand file tree Collapse file tree
Tests/Consumption/Extension Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # How to change consume command logger
2+
3+ By default ` bin/console enqueue:consume ` (or ` bin/console enqueue:transport:consume ` ) command prints messages to output.
4+ The amount of info could be controlled by verbosity option (-v, -vv, -vvv).
5+
6+ In order to change the default logger used by a command you have to register a ` LoggerExtension ` just before the default one.
7+ The extension asks you for a logger service, so just pass the one you want to use.
8+ Here's how you can do it.
9+
10+ ``` yaml
11+ // config/services.yaml
12+
13+ services :
14+ app_logger_extension :
15+ class : ' Enqueue\Consumption\Extension\LoggerExtension'
16+ public : false
17+ arguments : ['@logger']
18+ tags :
19+ - { name: 'enqueue.consumption.extension', priority: 255 }
20+
21+ ```
22+
23+ The logger extension with the highest priority will set its logger.
24+
25+ [ back to index] ( ../index.md )
26+
27+
28+
Original file line number Diff line number Diff line change 5151* [ Development] ( #development )
5252 - [ Contribution] ( contribution.md )
5353
54+ ## Cookbook
55+
56+ * [ Symfony] ( #symfony-cookbook )
57+ - [ How to change consume command logger] ( cookbook/symfony/how-to-change-consume-command-logger.md )
58+
5459# Blogs
5560
5661* [ Getting Started with RabbitMQ in PHP] ( https://blog.forma-pro.com/getting-started-with-rabbitmq-in-php-84d331e20a66 )
Original file line number Diff line number Diff line change @@ -31,8 +31,16 @@ public function __construct(LoggerInterface $logger)
3131 */
3232 public function onStart (Context $ context )
3333 {
34- $ context ->setLogger ($ this ->logger );
35- $ this ->logger ->debug (sprintf ('Set context \'s logger %s ' , get_class ($ this ->logger )));
34+ if ($ context ->getLogger ()) {
35+ $ context ->getLogger ()->debug (sprintf (
36+ 'Skip setting context \'s logger "%s". Another one "%s" has already been set. ' ,
37+ get_class ($ context ->getLogger ()),
38+ get_class ($ this ->logger )
39+ ));
40+ } else {
41+ $ context ->setLogger ($ this ->logger );
42+ $ this ->logger ->debug (sprintf ('Set context \'s logger "%s" ' , get_class ($ this ->logger )));
43+ }
3644 }
3745
3846 /**
Original file line number Diff line number Diff line change @@ -151,6 +151,29 @@ public function testShouldNotLogAckMessageStatusIfReasonIsEmpty()
151151 $ extension ->onPostReceived ($ context );
152152 }
153153
154+ public function testShouldNotSetLoggerIfOneHasBeenSetOnStart ()
155+ {
156+ $ logger = $ this ->createLogger ();
157+
158+ $ alreadySetLogger = $ this ->createLogger ();
159+ $ alreadySetLogger
160+ ->expects ($ this ->once ())
161+ ->method ('debug ' )
162+ ->with (sprintf (
163+ 'Skip setting context \'s logger "%s". Another one "%s" has already been set. ' ,
164+ get_class ($ logger ),
165+ get_class ($ alreadySetLogger )
166+ ))
167+ ;
168+
169+ $ extension = new LoggerExtension ($ logger );
170+
171+ $ context = new Context ($ this ->createPsrContextMock ());
172+ $ context ->setLogger ($ alreadySetLogger );
173+
174+ $ extension ->onStart ($ context );
175+ }
176+
154177 /**
155178 * @return \PHPUnit_Framework_MockObject_MockObject|PsrContext
156179 */
You can’t perform that action at this time.
0 commit comments