Skip to content
Advertisement

How can I display Exception trace in laravel console command?

I’m using Laravel 5.1 to create a console based application. During development I would like to display the exception trace when an error occurs. However, even if I use -v -vv or -vvv option in php artisan, I don’t get an exception trace for my custom commands. I set APP_DEBUG=true in my .env, still no exception trace.

Output of php artisan some:unknowncommand is:

JavaScript

Output of php artisan -v some:unknowncommand is:

JavaScript

Now, I created a very simple console command called dp:test, with following handle function:

JavaScript

Output of php artisan dp:test is:

JavaScript

Output of php artisan -v dp:test is the same. Output of php artisan -vvv dp:test is the same.

The log file DOES show the exception trace, so somehow it should be possible to display it in cli. I don’t even see the filename and linenumer where the error occurs… How can I take care of this?

Thanks in advance!

EDIT:

Is digged a bit further. In case I use this in my Command:

JavaScript

and I issue the command php artisan -v dp:test, the error trace IS printed. The trace is only not printed when the exception is thrown due to a PHP error. In Illuminate/Foundation/Bootstrap/HandleExceptions.php method bootstrap PHP errors are converted to Exceptions. When this occurs, an exception is thrown, but the -v is somehow ignored when printing. This is very inconvenient because it makes debugging CLI apps hard.

I think the solution can be found in vendor/symfony/console/Application.php, method renderException.

I’m going to dig further later, unless someone else can point the solution faster than me 🙂

Advertisement

Answer

I found reason why -v is ignored:

in Illuminate/Foundation/Bootstrap/HandleExceptions.php, the renderForConsole method instantiates a ConsoleOutput object, with default settings, not taking into account the verbosity settings the user asked for:

JavaScript

For this reason, whatever -v -vv or -vvv is set, the $output->getVerbosity() in vendor/symfony/console/Application.php is always lower than OutputInterface::VERBOSITY_VERBOSE, for that reason, the stack trace is not printed.

I probably start an issue on github for this, because I think it’s much more convenient if errors are shown in CLI if user sets -v.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement