File tree 4 files changed +66
-2
lines changed
Tests/Consumption/Extension
4 files changed +66
-2
lines changed 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 51
51
* [ Development] ( #development )
52
52
- [ Contribution] ( contribution.md )
53
53
54
+ ## Cookbook
55
+
56
+ * [ Symfony] ( #symfony-cookbook )
57
+ - [ How to change consume command logger] ( cookbook/symfony/how-to-change-consume-command-logger.md )
58
+
54
59
# Blogs
55
60
56
61
* [ 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)
31
31
*/
32
32
public function onStart (Context $ context )
33
33
{
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
+ }
36
44
}
37
45
38
46
/**
Original file line number Diff line number Diff line change @@ -151,6 +151,29 @@ public function testShouldNotLogAckMessageStatusIfReasonIsEmpty()
151
151
$ extension ->onPostReceived ($ context );
152
152
}
153
153
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
+
154
177
/**
155
178
* @return \PHPUnit_Framework_MockObject_MockObject|PsrContext
156
179
*/
You can’t perform that action at this time.
0 commit comments