Symfony Command tapped in monolog

79 views Asked by At

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

1

There are 1 answers

2
Timurib On BEST ANSWER

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);