I'm developing a Illuminate\Console\Command. To be run via cli using php artisan. This Command class is using other classes. I appreciate the Command->info(), Command->error(), methods... How can I use them in dependencies?
Until now I'm passing to other classes $this as parameter
e.g.
class MyClass extends Command {
....
$g = new MyOtherClass($this, $param...);
$g->find();
....
}
class MyOtherClass {
$command;
....
public function __construct($command){
$this->command=$command;
}
public function find(){
if($error)
$this->command->error($error);
}
....
}
I wished methods could be accessed statically like: Command::error("some error");
But maybe this is not the intended use?
I suggest you use "echo" to return instead "$this->command->error", because you can use in Kernel and save in a different log, like this:
echo '['.date('Y-m-d H:i:s').'] local.ERROR: '.$error.PHP_EOL; // This way it will be better visible in log viewer.
And in app\Console\Kernel.php
$schedule->command('mycommand')
->everyTenMinutes()
->sendOutputTo(storage_path('logs/mycommand.log'))
->name('mycommand')
->withoutOverlapping();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With