So I have a Laravel controller:
class YeahMyController extends BaseController {
public function getSomething() {
Console::info('mymessage'); // <-- what do I put here?
return 'yeahoutputthistotheresponse';
}
}
Currently, I'm running the application using artisan (which runs PHP's built-in development web server under the hood):
php artisan serve
I would like to log console messages to the STDOUT
pipe for the artisan process.
Aha!
This can be done with the following PHP function:
Found the answer here: Print something in PHP built-in web server
It's very simple.
You can call it from anywhere in APP.
If you want to log to STDOUT you can use any of the ways Laravel provides; for example (from wired00's answer):
The STDOUT magic can be done with the following (you are setting the file where
info
messages go):Word of caution: this is strictly for debugging. Do no use anything in production you don't fully understand.
I haven't tried this myself, but a quick dig through the library suggests you can do this:
I couldn't find a shortcut for this, so you would probably want to create a facade to avoid duplication.
In Laravel 6 there is a channel called 'stderr'. See
config/logging.php
:In your controller:
Bit late to this...I'm surprised that no one mentioned Symfony's
VarDumper
component that Laravel includes, in part, for itsdd()
(and lesser-known,dump()
) utility functions.There's a bit more code needed, but, in return, we get nice formatted, readable output in the console—especially useful for debugging complex objects or arrays:
To take this a step further, we can even colorize the output! Add this helper function to the project to save some typing:
If we're running the app behind a full webserver (like Apache or Nginx—not
artisan serve
), we can modify this function slightly to send the dumper's prettified output to the log (typically storage/logs/laravel.log):...and, of course, watch the log using:
PHP's
error_log()
works fine for quick, one-off inspection of simple values, but the functions shown above take the hard work out of debugging some of Laravel's more complicated classes.