Skip to content

Commit aaf2176

Browse files
committed
Fixed formatting, improved example of command exit code logging to include the actual exit code
1 parent 664c9b2 commit aaf2176

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

cookbook/console/logging.rst

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
single: Console; Enabling logging
33

44
How to enable logging in Console Commands
5-
===============================
5+
=========================================
66

77
The Console component doesn't provide any logging capabilities out of the box.
88
Normally, you run console commands manually and observe the output, that's
@@ -14,20 +14,19 @@ output and process it. This can be especially handful if you already have
1414
some existing setup for aggregating and analyzing Symfony logs.
1515

1616
There are basically two logging cases you would need:
17-
* Manually logging some information from your command
18-
* Logging not caught Exceptions
17+
* Manually logging some information from your command;
18+
* Logging not caught Exceptions.
1919

2020
Manually logging from console command
21-
----------------------------------
21+
-------------------------------------
2222

2323
This one is really simple. When you create console command within full framewok
24-
as described here (:doc:`/cookbook/console/console_command`), your command
25-
extends (:class:`Symfony\\Bundle\\FrameworkBundle\\Command\\ContainerAwareCommand`),
24+
as described :doc:`here</cookbook/console/console_command>`, your command
25+
extends :class:`Symfony\\Bundle\\FrameworkBundle\\Command\\ContainerAwareCommand`,
2626
so you can simply access standard logger service through the container and
2727
use it to do the logging::
2828

2929
// src/Acme/DemoBundle/Command/GreetCommand.php
30-
3130
namespace Acme\DemoBundle\Command;
3231

3332
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
@@ -39,7 +38,7 @@ use it to do the logging::
3938

4039
class GreetCommand extends ContainerAwareCommand
4140
{
42-
// ..
41+
// ...
4342

4443
protected function execute(InputInterface $input, OutputInterface $output)
4544
{
@@ -55,10 +54,10 @@ use it to do the logging::
5554

5655
if ($input->getOption('yell')) {
5756
$text = strtoupper($text);
58-
$logger->warn('Yelled: ' . $text);
57+
$logger->warn('Yelled: '.$text);
5958
}
6059
else {
61-
$logger->info('Greeted: ' . $text);
60+
$logger->info('Greeted: '.$text);
6261
}
6362

6463
$output->writeln($text);
@@ -69,16 +68,15 @@ Depending on the environment you run your command you will get the results
6968
in ``app/dev.log`` or ``app/prod.log``.
7069

7170
Enabling automatic Exceptions logging
72-
----------------
71+
-------------------------------------
7372

7473
In order to enable console application to automatically log uncaught exceptions
7574
for all commands you'd need to do something more.
7675

77-
First, you have to extend :class:`Symfony\Bundle\FrameworkBundle\Console\Application`
78-
class to override its ``run()`` method, where exception handling should happen::
76+
First, you have to extend :class:`Symfony\\Bundle\\FrameworkBundle\\Console\\Application`
77+
class to override its :method:`Symfony\\Bundle\\FrameworkBundle\\Console\\Application::run` method, where exception handling should happen::
7978

8079
// src/Acme/DemoBundle/Console/Application.php
81-
8280
namespace Acme\DemoBundle\Console;
8381

8482
use Symfony\Bundle\FrameworkBundle\Console\Application as BaseApplication;
@@ -199,7 +197,7 @@ one.
199197

200198

201199
Logging non-0 exit statuses
202-
-------------------------------------------
200+
---------------------------
203201

204202
The logging capabilities of the console can be further extended by logging
205203
non-0 exit statuses. This way you will know if a command had any errors, even
@@ -228,7 +226,7 @@ In order to do that, you'd have to modify ``run()`` method of your extended
228226
if ($statusCode !== 0) {
229227
/** @var $logger LoggerInterface */
230228
$logger = $this->getKernel()->getContainer()->get('logger');
231-
$logger->warn(sprintf('Command `%s` exited with non 0 status code', $this->getCommandName($input)));
229+
$logger->warn(sprintf('Command `%s` exited with status code %d', $this->getCommandName($input), $statusCode));
232230
}
233231

234232
// @codeCoverageIgnoreStart

0 commit comments

Comments
 (0)