所以我有一个Laravel控制器:
class YeahMyController extends BaseController { public function getSomething() { Console::info('mymessage'); // <-- what do I put here? return 'yeahoutputthistotheresponse'; } }
目前,我正在使用artisan运行应用程序(它运行PHP的内置开发Web服务器):
php artisan serve
我想将控制台消息记录到STDOUT
工匠流程的管道中.
啊哈!
这可以通过以下PHP函数完成:
error_log('Some message here.');
在这里找到答案:在PHP内置Web服务器中打印一些东西
为了更好地解释Dave Morrissey的答案,我已经在laravel外观中使用Console Output类进行了这些步骤.
1)在您喜欢的文件夹中创建一个Facade(在我的案例中为app\Facades):
class ConsoleOutput extends Facade { protected static function getFacadeAccessor() { return 'consoleOutput'; } }
2)在app\Providers中注册新的服务提供商,如下所示:
class ConsoleOutputServiceProvider extends ServiceProvider { public function register(){ App::bind('consoleOutput', function(){ return new \Symfony\Component\Console\Output\ConsoleOutput(); }); }
}
3)在config\app.php文件中添加所有这些东西,注册提供者和别名.
'providers' => [ //other providers App\Providers\ConsoleOutputServiceProvider::class ], 'aliases' => [ //other aliases 'ConsoleOutput' => App\Facades\ConsoleOutput::class, ],
就是这样,现在在Laravel应用程序的任何地方,只需以这种方式调用您的方法:
ConsoleOutput::writeln('hello');
希望这对你有所帮助.
我自己没有试过这个,但是通过图书馆的快速挖掘表明你可以这样做:
$output = new Symfony\Component\Console\Output\ConsoleOutput(); $output->writeln("<info>my message</info>");
我找不到这个的快捷方式,所以你可能想要创建一个外观来避免重复.
如果您想登录STDOUT,则可以使用Laravel提供的任何一种方式。例如(根据wired00的回答):
Log::info('This is some useful information.');
可以使用以下方法来实现STDOUT的魔力(您正在将文件设置为info
消息所在的位置):
Log::useFiles('php://stdout', 'info');
请注意:这仅用于调试。在生产中不要使用任何您不完全了解的东西。
这个问题与工匠的服务有关,所以Jrop的答案在这种情况下是理想的.即,error_log
登录到apache日志.
但是,如果通过标准Web服务器提供服务,则只需使用Laravel特定的日志记录功能:
\Log::info('This is some useful information.'); \Log::warning('Something could be going wrong.'); \Log::error('Something is really going wrong.');
对于信息的当前版本的laravel如下:
info('This is some useful information.');
这会记录到位于/laravel/storage/logs/laravel-<date>.log
(laravel 5.0)的Laravel的日志文件.监控日志 - 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
非常简单
您可以从APP中的任何位置调用它。
$out = new \Symfony\Component\Console\Output\ConsoleOutput(); $out->writeln("Hello from Terminal");