3

Is it possible to log a console command`s output to a log file ? For example when a command has:

protected function execute(InputInterface $input, OutputInterface $output)
{
    ...
    $output->writeln('command run');
    ...
}

How to log the output without modifying the execute method ?

5
  • What OS are you using? You can dump command line output to file or change your execute function by adding logging. Commented Jan 18, 2017 at 21:14
  • It shouldn't depend on OS and the execute method must not be modified. Commented Jan 19, 2017 at 8:39
  • 1
    Where are you using the command ? What about redirection the standard output to a file >> mycommand.txt Commented Jan 19, 2017 at 8:50
  • @COil that's what I wanted to suggest, but it would be OS dependend this way Commented Jan 19, 2017 at 10:00
  • I need generally log command's output, whereby the console output should exist also. Commented Jan 19, 2017 at 10:28

2 Answers 2

1

According to Symfony2 documentation:

The Console component doesn't provide any logging capabilities out of the box. Normally, you run console commands manually and observe the output, which is why logging is not provided.

http://symfony.com/doc/2.8/console/logging.html

However you could use service container inside the command:

$logger = $this->getContainer()->get('logger');

And log some events:

$logger->info('Hello: '.$variable);

You should see the logged entries in app/logs/dev.log or app/logs/prod.log.

Sign up to request clarification or add additional context in comments.

Comments

0

This is my approach to use the logger service in console commands.

First I configure the channel and handler for my logger, it is configured on config.yml My custom channel is named as con and the handler as conlog, but you can put names you want:

# app/config/config.yml

monolog:
    channels: ['con']
    handlers:
        conlog:
            type:  rotating_file
            max_files: 31
            path:  %kernel.logs_dir%/command.log
            level: debug
            channels: ['con']

I use a rotating_file type because my console command is executed all days by a scheduled task and I need to do a daily tracking of the command but you can use the type you need. More info about handler types in docs

Now you can use the logger in your console command, the important thing here is to inject monolog service using the automatically registered logger service with the name of the channel, in my sample it is:

monolog.logger.con

More info about this in docs

// src/AcmeBundle/Command/YourCommand

class YourCommand extends ContainerAwareCommand
{
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $logger = $this->getContainer()->get('monolog.logger.con');
        $myMessage = 'The first message';
        $output->writeln($myMessage);
        $logger->info($myMessage);
    }
}

And this is all, in this sample the log is saved in log directory with file names as: command-Y-m-d.log.

Finally it is important remarks that the con channel logger messages are added to dev.log too. If you want prevent it, it will be needed to configure channels option in main handler, on config_dev.yml, and prefix with ! our custom channel name:

# app/config/config_dev.yml

monolog:
    handlers:
        main:
            type:  stream
            path:  %kernel.logs_dir%/%kernel.environment%.log
            level: debug
            channels: ['!con']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.