2
2
single: Console; Enabling logging
3
3
4
4
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
8
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
14
14
some existing setup for aggregating and analyzing Symfony logs.
15
15
16
16
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.
19
19
20
20
Manually logging from console command
21
- ----------------------------------
21
+ -------------------------------------
22
22
23
23
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 `,
26
26
so you can simply access standard logger service through the container and
27
27
use it to do the logging::
28
28
29
29
// src/Acme/DemoBundle/Command/GreetCommand.php
30
-
31
30
namespace Acme\DemoBundle\Command;
32
31
33
32
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
@@ -39,7 +38,7 @@ use it to do the logging::
39
38
40
39
class GreetCommand extends ContainerAwareCommand
41
40
{
42
- // ..
41
+ // ...
43
42
44
43
protected function execute(InputInterface $input, OutputInterface $output)
45
44
{
@@ -55,10 +54,10 @@ use it to do the logging::
55
54
56
55
if ($input->getOption('yell')) {
57
56
$text = strtoupper($text);
58
- $logger->warn('Yelled: ' . $text);
57
+ $logger->warn('Yelled: '. $text);
59
58
}
60
59
else {
61
- $logger->info('Greeted: ' . $text);
60
+ $logger->info('Greeted: '. $text);
62
61
}
63
62
64
63
$output->writeln($text);
@@ -69,16 +68,15 @@ Depending on the environment you run your command you will get the results
69
68
in ``app/dev.log `` or ``app/prod.log ``.
70
69
71
70
Enabling automatic Exceptions logging
72
- ----------------
71
+ -------------------------------------
73
72
74
73
In order to enable console application to automatically log uncaught exceptions
75
74
for all commands you'd need to do something more.
76
75
77
- First, you have to extend :class: `Symfony\B undle\F rameworkBundle\C onsole\A pplication `
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::
79
78
80
79
// src/Acme/DemoBundle/Console/Application.php
81
-
82
80
namespace Acme\DemoBundle\Console;
83
81
84
82
use Symfony\Bundle\FrameworkBundle\Console\Application as BaseApplication;
199
197
200
198
201
199
Logging non-0 exit statuses
202
- -------------------------------------------
200
+ ---------------------------
203
201
204
202
The logging capabilities of the console can be further extended by logging
205
203
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
228
226
if ($statusCode !== 0) {
229
227
/** @var $logger LoggerInterface */
230
228
$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 ));
232
230
}
233
231
234
232
// @codeCoverageIgnoreStart
0 commit comments