0

I created a custom command. I want to log with monolog the command tapped by user with arguments and options

For exemple: php bin/console app:my_command argument1 --option=1234

Now I have that :

        $this->log->error('error on command', array(
            'command_tapped_by_user' => '????',
            'command' => $this->getName(),
            'arguments' => $input->getArguments(),
            'options' => $input->getOptions(),
            'exception' => $e->getMessage()
        ));

Thank you

Boris Guéry
  • 47,316
  • 8
  • 52
  • 87
Xero
  • 3,951
  • 4
  • 41
  • 73

1 Answers1

2

All arguments passed to the PHP script are available in the $argv array, where first element is a script name. It is not a "superglobal" array and in the Symfony command (or in any other function or method) it available as element of $GLOBALS or through the global keyword. Original command is splitted by whitespace characters. To restore it you should join them back:

echo join(' ', $GLOBALS['argv']);

Also if you are not worry about OS dependencies (and if you are a nonconformist), you can use system facilities. For example in the Linux environment current command line is stored in the /proc/$PID/cmdline:

$pid = getmypid();
$cmd = file_get_contents("/proc/$pid/cmdline");
// Arguments are splitted by NULL bytes that we replace by space character:
echo str_replace("\0", " ", $cmd);
Timurib
  • 2,735
  • 16
  • 29
  • 1
    The actual purpose of using the `Symfony/Console` is to hide such technical details. While using Symfony, I advise you to use their utilities such as [`ArgvInput`](https://github.com/symfony/console/blob/master/Input/ArgvInput.php) – Boris Guéry Dec 23 '16 at 14:13
  • You right, i am forgot about that class. Method `__toString()` seems appropriate for the task. – Timurib Dec 23 '16 at 14:24