Vicky Dev
Vicky Dev

Reputation: 2173

Print/Save complete Laravel Artisan Command output(large array from debug line) to file

I am working in Laravel 6.x project, where I have added debug line inside an Artisan command to get the large array generated from that line into text file as output.

It looks almost like following code in the handle method of said command:

....
if(!$dataArray->isEmpty())
{
    foreach ($dataArr as $dataEle)
    {
        $dataArrToPrnt[] = $dataEle->toArray();
        ....
    }
}
die(var_dump($dataArrToPrnt));
....

But the problem is the dataArray is huge, so after few entries, the command output skips the rest of the data and prints (...more elements) instead.

And I am running the command like below for printing to file(Windows 10 x64 with GitBash, so I have use php.exe instead of php):

php.exe artisan command-namespace:command >> result.out

How can I override/avoid that from happening and print the complete dataArray to file regardless of it's size ?

Upvotes: 0

Views: 1347

Answers (1)

Bennett
Bennett

Reputation: 738

Try using the global Log. Add a logging channel to your config/logging.php file like so:

'dataLogger' => [
    'driver' => 'daily',
    'path' => storage_path('logs/data.log'),
    'level' => 'info',
],

And use in your code like:

use Log; 
foreach ($dataArr as $dataEle){
    Log::channel('dataLogger')->info($dataEle);
}

(Or however you'd like to format your data.)

Upvotes: 1

Related Questions