10

I can see if a script is running in the console with the App::runningInConsole() command, but I would like to know (for audit logging purposes) which command has been run from console.

To add some context - I need to log whenever a system or user accesses a certain type of data. For users, that's pretty simple - I can use Auth::user() and get their IP address from Request, but for console commands, it's a little more difficult.

I can figure out if a console command is running, which is good, but I need to be able to record which console command is running.

3 Answers 3

8

I do not see a way to get that information directly Laravel. I'm also not sure how that would be possible, as you can have one command executing other commands or even creating a new application instance like some test tools do.

There is however a way to achieve that using plain PHP. You can check the server/environment variables to identify how application was executed.

Have a look at

dd($_SERVER['argv']);

If you run a command this should give you the command name in $_SERVER['argv'][1]

You can find more information here: http://php.net/manual/en/reserved.variables.argv.php

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

3 Comments

Yeah, I was hoping there was just some poorly documented laravel feature that you could call (like App::currentCommand() or something). In the instance of a command calling a command, I would prefer to log the inner command.
When laravel executes a command it updates the name of the process that it is running in. If you set $processTitle for your command you should get back whatever you put there by calling "cli_get_process_title()", just make sure you are in CLI mode.
If you set "protected $processTitle = _ _ CLASS _ _;" you will get what you need, but I would call it a hack, not a feature.
6

To get console command together with arguments you should use something like that:

$command = request()->server('argv');
if (is_array($command)) {
   $command = implode(' ', $command);
}

In Laravel you can use request()->server() to get $_SERVER variables. (In versions of Laravel before 5.1 this was available via \Request::server().)

Comments

1

The problem with $SERVER['arg'] is that it doesnt work when you execute the command on the GUI.

I fixed the problem adding the next code on the command.

private function getAttributes() 
{
    $arguments = $this->arguments();
    unset($arguments[0]);
    $arguments = collect($arguments)->implode(' ');
    $options = collect($this->options())->filter(function($item) {
        return $item;
    })->map(function($item, $key) {
        $return = '--'.$key;
        if($item!==true) {
            $return .= '='.$item;
        }
        return $return;
    })->implode(' ');
    return $arguments.' '.$options;
}

and you get it calling $this->getAttributes() and you will get all the command with the attributes

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.