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

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

Posted by: admin October 29, 2017 Leave a comment

Questions:

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:

php artisan serve

I would like to log console messages to the STDOUT pipe for the artisan process.

Answers:

The question specifically 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:

use Log;
...

Log::info('This is some useful information.');

Log::warning('Something could be going wrong.');

Log::error('Something is really going wrong.');

or without the use statemnet:

\Log::info('This is some useful information.');

\Log::warning('Something could be going wrong.');

\Log::error('Something is really going wrong.');

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

Hope that helps the next weary traveler 🙂

Questions:
Answers:

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

Questions:
Answers:

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.

Questions:
Answers:

For better explain Dave Morrissey’s answer I have made these steps for wrap with Console Output class in a laravel facade.

1) Create a Facade in your prefer folder (in my case app\Facades):

class ConsoleOutput extends Facade {

 protected static function getFacadeAccessor() { 
     return 'consoleOutput';
 }

}

2) Register a new Service Provider in app\Providers as follow:

class ConsoleOutputServiceProvider extends ServiceProvider
{

 public function register(){
    App::bind('consoleOutput', function(){
        return new \Symfony\Component\Console\Output\ConsoleOutput();
     });
 }

}

3) Add all this stuffs in config\app.php file, registering the provider and alias.

 'providers' => [
   //other providers
    App\Providers\ConsoleOutputServiceProvider::class
 ],
 'aliases' => [
  //other aliases
   'ConsoleOutput' => App\Facades\ConsoleOutput::class,
 ],

That’s it, now in any place of your Laravel application, just call your method in this way:

ConsoleOutput::writeln('hello');

Hope this help you.

Questions:
Answers:

If you want to log to STDOUT you can use any of the ways Laravel provides; for example (from wired00’s answer):

Log::info('This is some useful information.');

The STDOUT magic can be done with the following (you are setting the file where info messages go):

Log::useFiles('php://stdout', 'info');

Word of caution: this is strictly for debugging. Do no use anything in production you don’t fully understand.

Questions:
Answers:

I use this for Lumen, pretty sure it will work with Laravel too

shell_exec('echo "hello world" 1>&2');

Leave a Reply

Your email address will not be published. Required fields are marked *