Skip to content

Commit b874bce

Browse files
committed
[symfony#1965] Proofreading the new logging in console commands entry
The most important changes is to highlight that we're extending Symfony's code further than you *should*, which of course you're welcome to do, but we needs to be properly warned.
1 parent 3d1003b commit b874bce

File tree

1 file changed

+44
-35
lines changed

1 file changed

+44
-35
lines changed

cookbook/console/logging.rst

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ How to enable logging in Console Commands
55
=========================================
66

77
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
99
why logging is not provided. However, there are cases when you might need
1010
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
1212
logging capabilities instead of configuring other tools to gather console
1313
output and process it. This can be especially handful if you already have
1414
some existing setup for aggregating and analyzing Symfony logs.
@@ -17,14 +17,14 @@ There are basically two logging cases you would need:
1717
* Manually logging some information from your command;
1818
* Logging uncaught Exceptions.
1919

20-
Manually logging from console command
21-
-------------------------------------
20+
Manually logging from a console Command
21+
---------------------------------------
2222

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::
2828

2929
// src/Acme/DemoBundle/Command/GreetCommand.php
3030
namespace Acme\DemoBundle\Command;
@@ -64,18 +64,30 @@ use it to do the logging::
6464
}
6565
}
6666

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``.
6969

7070
Enabling automatic Exceptions logging
7171
-------------------------------------
7272

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+
7589

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
7991
8092
// src/Acme/DemoBundle/Console/Application.php
8193
namespace Acme\DemoBundle\Console;
@@ -113,7 +125,7 @@ method, where exception handling should happen::
113125
*/
114126
public function run(InputInterface $input = null, OutputInterface $output = null)
115127
{
116-
//make parent method throw exceptions, so we can log it
128+
// make the parent method throw exceptions, so you can log it
117129
$this->setCatchExceptions(false);
118130
119131
if (null === $input) {
@@ -165,27 +177,28 @@ method, where exception handling should happen::
165177
166178
public function setAutoExit($bool)
167179
{
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
169181
$this->originalAutoExit = (Boolean) $bool;
170182
parent::setAutoExit($bool);
171183
}
172184
173185
}
174186
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
177189
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>`
179192
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
181194
method).
182195

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
185198
setting in a different property, since the parent property is private.
186199

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::
189202

190203
// app/console
191204

@@ -196,26 +209,25 @@ Now to be able to use our extended ``Application`` class we need to adjust
196209

197210
// ...
198211

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
200213
one.
201214

202-
203215
Logging non-0 exit statuses
204216
---------------------------
205217

206218
The logging capabilities of the console can be further extended by logging
207219
non-0 exit statuses. This way you will know if a command had any errors, even
208220
if no exceptions were thrown.
209221

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::
212224

213225
public function run(InputInterface $input = null, OutputInterface $output = null)
214226
{
215-
//make parent method throw exceptions, so we can log it
227+
// make the parent method throw exceptions, so you can log it
216228
$this->setCatchExceptions(false);
217229

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
219231
$autoExit = $this->originalAutoExit;
220232
$this->setAutoExit(false);
221233

@@ -240,6 +252,3 @@ In order to do that, you'd have to modify ``run()`` method of your extended
240252

241253
return $statusCode;
242254
}
243-
244-
245-

0 commit comments

Comments
 (0)