How do I write to the console from a Laravel Controller?
Solution 1
Aha!
This can be done with the following PHP function:
error_log('Some message here.');
Found the answer here: Print something in PHP built-in web server
Solution 2
The question relates to serving via artisan and so Jrop's answer is ideal in that case. I.e, error_log
logging to the apache log.
However, if your serving via a standard web server then simply use the Laravel specific logging functions:
\Log::info('This is some useful information.');
\Log::warning('Something could be going wrong.');
\Log::error('Something is really going wrong.');
Or with current version of Laravel, like this:
info('This is some useful information.');
This logs to Laravel's log file located at /laravel/storage/logs/laravel-<date>.log
(laravel 5.0). Monitor the log - linux/osx: tail -f /laravel/storage/logs/laravel-<date>.log
- Laravel 5.0 http://laravel.com/docs/5.0/errors
- Laravel 4.2: http://laravel.com/docs/4.2/errors
Solution 3
I haven't tried this myself, but a quick dig through the library suggests you can do this:
$output = new Symfony\Component\Console\Output\ConsoleOutput();
$output->writeln("<info>my message</info>");
I couldn't find a shortcut for this, so you would probably want to create a facade to avoid duplication.
Solution 4
It's very simple.
You can call it from anywhere in APP.
$out = new \Symfony\Component\Console\Output\ConsoleOutput();
$out->writeln("Hello from Terminal");
Solution 5
In Laravel 6 there is a channel called 'stderr'. See config/logging.php
:
'stderr' => [
'driver' => 'monolog',
'handler' => StreamHandler::class,
'formatter' => env('LOG_STDERR_FORMATTER'),
'with' => [
'stream' => 'php://stderr',
],
],
In your controller:
use Illuminate\Support\Facades\Log;
Log::channel('stderr')->info('Something happened!');
Jonathan Apodaca
Updated on July 08, 2022Comments
-
Jonathan Apodaca almost 2 years
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. -
wired00 about 9 yearsmore ideal is using laravel's built in logging ie,
Log::info('This is some useful information.');
just see my answer -
melvinmt almost 9 yearsThere's no apache if you're using the artisan web server. And using Log::info will also not output to STDOUT.
-
Stuart Wagner over 8 yearsThis also works great for inline debugging of migrations and other
artisan
commands. -
Tobia almost 8 years@wired00 The question specifically asks how to write to the
artisan serve
console.error_log()
does so, whileLog::info()
does not. -
dotnetCarpenter over 7 yearsjust what I needed. quick 'n dirty - perfect for dev work
-
Jonathan Apodaca over 6 years@wired00 You are correct, but as stated in my original question, I was not using Apache, but was using the artisan web server. In that context, error_log is more useful.
-
wired00 over 6 years@Jrop yep right you are, I amended my answer to clarify that
-
Mark Odey over 6 yearsDespite the fact that I like Jrop answers, I feel this should be the accepted answer too.
-
Mark Odey over 6 years
-
Tomeg over 6 yearsIf you want the fancy command IO from Laravel (like styling, asking and table) then check the ConsoleCommand class in my answer here
-
Tomeg over 6 yearsIf you want the fancy command IO from Laravel (like styling, asking and table) then check the ConsoleCommand class in my answer here
-
Tomeg over 6 yearsIf you want the fancy command IO from Laravel (like styling, asking and table) then check the ConsoleCommand class in my answer here
-
Tomeg over 6 yearsIf you want the fancy command IO from Laravel (like styling, asking and table) then check the ConsoleCommand class in my answer here
-
Tarek almost 6 yearsBut it's only printing strings i want to print an object :(
-
Googlian over 4 yearsDid you check it in terminal
-
dude2511 almost 4 yearsThanks for this nice idea. For current Laravel versions (using 7.x) I think it should be
public function __construct($argInput = "")
though. -
boryn about 3 yearsI love this solution with CliDumper()! Thank you :)
-
Black over 2 years@wired00, I had to use
\Log::info('This is some useful information.');
-
luisenricke almost 2 yearsSimple and easy, thanks