How to get the cli command that was run, from PHP?
I have this logging class
class Logger {
public function log($logMessage)
{
$this->writeLog($this->getCommand() . ' : ' . $logMessage);
}
private function getCommand()
{
//How to get the command?
//here var_dump($argv); returns NULL
}
}
This logger is being used by many classes that are run from several commands. Tow examples: process_users.php
$logger = new Logger();
$users = new Users();
foreach ($users->getUnprocessed() as $user) {
$logger->log('processing '.$user->getId());
if ($user->process()){
$logger->log('processed ' . $user->getId());
} else {
$logger->log('failed ' . $user->getId());
}
}
upload_consumer.php
$logger = new Logger();
$consumer = new UploadConsumer();
while ($message = $consumer->getNextMessage()){
$logger->log('Uploading ' . $message['name']);
$this->upload($message);
$logger->log('Finished uploading '. $message['name']);
}
So when I run php process_users.php corporate --limit 10 I want to have logs like:
php process_users.php corporate --limit 10 : processing 2554
php process_users.php corporate --limit 10 : processed 2554
php process_users.php corporate --limit 10 : processing 2555
php process_users.php corporate --limit 10 : failed 2555
And when I run php upload_consumer.php -m 100 -w I want to have logs like:
php upload_consumer.php -m 100 -w : Uploading 564sdf564sdf56sd4f.png
php upload_consumer.php -m 100 -w : Finished uploading 564sdf564sdf56sd4f.png
php upload_consumer.php -m 100 -w : Uploading sd56f4sd54f6sd6f54.png
php upload_consumer.php -m 100 -w : Finished uploading sd56f4sd54f6sd6f54.png
Is this possible?