@@ -5,10 +5,10 @@ How to enable logging in Console Commands
5
5
=========================================
6
6
7
7
The Console component doesn't provide any logging capabilities out of the box.
8
- Normally, you run console commands manually and observe the output, that's
8
+ Normally, you run console commands manually and observe the output, which is
9
9
why logging is not provided. However, there are cases when you might need
10
10
logging. For example, if you are running console commands unattended, such
11
- as from cron jobs or deployment scripts it may be easier to use Symfony's
11
+ as from cron jobs or deployment scripts, it may be easier to use Symfony's
12
12
logging capabilities instead of configuring other tools to gather console
13
13
output and process it. This can be especially handful if you already have
14
14
some existing setup for aggregating and analyzing Symfony logs.
@@ -17,14 +17,14 @@ There are basically two logging cases you would need:
17
17
* Manually logging some information from your command;
18
18
* Logging uncaught Exceptions.
19
19
20
- Manually logging from console command
21
- -------------------------------------
20
+ Manually logging from a console Command
21
+ ---------------------------------------
22
22
23
- This one is really simple. When you create console command within full framewok
24
- as described :doc: `here< /cookbook/console/console_command> ` , your command
25
- extends :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Command\\ ContainerAwareCommand `,
26
- so you can simply access standard logger service through the container and
27
- use it to do the logging::
23
+ This one is really simple. When you create a console command within the full
24
+ framework as described in " :doc: `/cookbook/console/console_command `" , your command
25
+ extends :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Command\\ ContainerAwareCommand `.
26
+ This means that you can simply access the standard logger service through the
27
+ container and use it to do the logging::
28
28
29
29
// src/Acme/DemoBundle/Command/GreetCommand.php
30
30
namespace Acme\DemoBundle\Command;
@@ -64,18 +64,30 @@ use it to do the logging::
64
64
}
65
65
}
66
66
67
- Depending on the environment you run your command you will get the results
68
- in ``app/logs/dev.log `` or ``app/logs/prod.log ``.
67
+ Depending on the environment in which you run your command (and your logging
68
+ setup), you should see the logged entries in ``app/logs/dev.log `` or ``app/logs/prod.log ``.
69
69
70
70
Enabling automatic Exceptions logging
71
71
-------------------------------------
72
72
73
- In order to enable console application to automatically log uncaught exceptions
74
- for all commands you'd need to do something more.
73
+ To get your console application to automatically log uncaught exceptions
74
+ for all of your commands, you'll need to do a little bit more work.
75
+
76
+ First, create a new sub-class of :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Console\\ Application `
77
+ and override its :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Console\\ Application::run `
78
+ method, where exception handling should happen:
79
+
80
+ .. warning ::
81
+
82
+ Due to the nature of the core :class: `Symfony\\ Component\\ Console\\ Application `
83
+ class, much of the :method: `run<Symfony\\ Bundle\\ FrameworkBundle\\ Console\\ Application::run> `
84
+ method has to be duplicated and even a private property ``originalAutoExit ``
85
+ re-implemented. This serves as an example of what you *could * do in your
86
+ code, though there is a high risk that something may break when upgrading
87
+ to future versions of Symfony.
88
+
75
89
76
- First, you have to extend :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Console\\ Application `
77
- class to override its :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Console\\ Application::run `
78
- method, where exception handling should happen::
90
+ .. code-block :: php
79
91
80
92
// src/Acme/DemoBundle/Console/Application.php
81
93
namespace Acme\DemoBundle\Console;
@@ -113,7 +125,7 @@ method, where exception handling should happen::
113
125
*/
114
126
public function run(InputInterface $input = null, OutputInterface $output = null)
115
127
{
116
- //make parent method throw exceptions, so we can log it
128
+ // make the parent method throw exceptions, so you can log it
117
129
$this->setCatchExceptions(false);
118
130
119
131
if (null === $input) {
@@ -165,27 +177,28 @@ method, where exception handling should happen::
165
177
166
178
public function setAutoExit($bool)
167
179
{
168
- // parent property is private, so we need to intercept it in setter
180
+ // parent property is private, so we need to intercept it in a setter
169
181
$this->originalAutoExit = (Boolean) $bool;
170
182
parent::setAutoExit($bool);
171
183
}
172
184
173
185
}
174
186
175
- What happens above is we disable exception catching, so that parent run method
176
- would throw the exceptions. When exception is caught, we simple log it by
187
+ In the code above, you disable exception catching so the parent `` run `` method
188
+ will throw all exceptions. When an exception is caught, you simple log it by
177
189
accessing the ``logger `` service from the service container and then handle
178
- the rest in the same way parent run method does that (Since parent :method: `run<Symfony\\ Bundle\\ FrameworkBundle\\ Console\\ Application::run> `
190
+ the rest of the logic in the same way that the parent ``run `` method does
191
+ (specifically, since the parent :method: `run<Symfony\\ Bundle\\ FrameworkBundle\\ Console\\ Application::run> `
179
192
method will not handle exceptions rendering and status code handling when
180
- `catchExceptions ` is set to false, it has to be done in the overridden
193
+ `` catchExceptions ` ` is set to false, it has to be done in the overridden
181
194
method).
182
195
183
- For our extended Application class to work properly with console shell mode
184
- we have to do a small trick to intercept ``autoExit `` setter, and store the
196
+ For the extended Application class to work properly with in console shell mode,
197
+ you have to do a small trick to intercept the ``autoExit `` setter and store the
185
198
setting in a different property, since the parent property is private.
186
199
187
- Now to be able to use our extended ``Application `` class we need to adjust
188
- ``app/console `` script to use our class instead of the default::
200
+ Now to be able to use your extended ``Application `` class you need to adjust
201
+ the ``app/console `` script to use the new class instead of the default::
189
202
190
203
// app/console
191
204
@@ -196,26 +209,25 @@ Now to be able to use our extended ``Application`` class we need to adjust
196
209
197
210
// ...
198
211
199
- That's it! Thanks to autoloader, our class will now be used instead of original
212
+ That's it! Thanks to autoloader, your class will now be used instead of original
200
213
one.
201
214
202
-
203
215
Logging non-0 exit statuses
204
216
---------------------------
205
217
206
218
The logging capabilities of the console can be further extended by logging
207
219
non-0 exit statuses. This way you will know if a command had any errors, even
208
220
if no exceptions were thrown.
209
221
210
- In order to do that, you'd have to modify ``run() `` method of your extended
211
- `Application ` class in the following way::
222
+ In order to do that, you'd have to modify the ``run() `` method of your extended
223
+ `` Application ` ` class in the following way::
212
224
213
225
public function run(InputInterface $input = null, OutputInterface $output = null)
214
226
{
215
- //make parent method throw exceptions, so we can log it
227
+ // make the parent method throw exceptions, so you can log it
216
228
$this->setCatchExceptions(false);
217
229
218
- // store autoExit value before resetting it - we'd need it later
230
+ // store the autoExit value before resetting it - you'll need it later
219
231
$autoExit = $this->originalAutoExit;
220
232
$this->setAutoExit(false);
221
233
@@ -240,6 +252,3 @@ In order to do that, you'd have to modify ``run()`` method of your extended
240
252
241
253
return $statusCode;
242
254
}
243
-
244
-
245
-
0 commit comments