How do I write to the console from a Laravel Controller?

217,176

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

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!');
Share:
217,176
Jonathan Apodaca
Author by

Jonathan Apodaca

Updated on July 08, 2022

Comments

  • Jonathan Apodaca
    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
    wired00 about 9 years
    more ideal is using laravel's built in logging ie, Log::info('This is some useful information.'); just see my answer
  • melvinmt
    melvinmt almost 9 years
    There's no apache if you're using the artisan web server. And using Log::info will also not output to STDOUT.
  • Stuart Wagner
    Stuart Wagner over 8 years
    This also works great for inline debugging of migrations and other artisan commands.
  • Tobia
    Tobia almost 8 years
    @wired00 The question specifically asks how to write to the artisan serve console. error_log() does so, while Log::info() does not.
  • dotnetCarpenter
    dotnetCarpenter over 7 years
    just what I needed. quick 'n dirty - perfect for dev work
  • Jonathan Apodaca
    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
    wired00 over 6 years
    @Jrop yep right you are, I amended my answer to clarify that
  • Mark Odey
    Mark Odey over 6 years
    Despite the fact that I like Jrop answers, I feel this should be the accepted answer too.
  • Mark Odey
    Mark Odey over 6 years
  • Tomeg
    Tomeg over 6 years
    If you want the fancy command IO from Laravel (like styling, asking and table) then check the ConsoleCommand class in my answer here
  • Tomeg
    Tomeg over 6 years
    If you want the fancy command IO from Laravel (like styling, asking and table) then check the ConsoleCommand class in my answer here
  • Tomeg
    Tomeg over 6 years
    If you want the fancy command IO from Laravel (like styling, asking and table) then check the ConsoleCommand class in my answer here
  • Tomeg
    Tomeg over 6 years
    If you want the fancy command IO from Laravel (like styling, asking and table) then check the ConsoleCommand class in my answer here
  • Tarek
    Tarek almost 6 years
    But it's only printing strings i want to print an object :(
  • Googlian
    Googlian over 4 years
    Did you check it in terminal
  • dude2511
    dude2511 almost 4 years
    Thanks for this nice idea. For current Laravel versions (using 7.x) I think it should be public function __construct($argInput = "") though.
  • boryn
    boryn about 3 years
    I love this solution with CliDumper()! Thank you :)
  • Black
    Black over 2 years
    @wired00, I had to use \Log::info('This is some useful information.');
  • luisenricke
    luisenricke almost 2 years
    Simple and easy, thanks