默认情况下,Laravel将每日日志文件保存在具有644权限的storage/logs
中。当第一个日志条目由 Apache 编写,然后从 CLI 运行的 PHP 尝试写入某些内容(或相反)时,这会产生问题。如果我运行ls -lh storage/logs/
结果如下所示:
-rw-r--r-- 1 test.example.org test.example.org 36K 6月 15 10:07 拉拉维尔-2015-06-15.log-rw-r--r-- 1 www-data www-data 24K Jun 16 09:55 laravel-2015-06-16.log-rw-r--r-- 1 www-data www-data 254 Jun 17 12:11 laravel-2015-06-17.log
因此,在给定日期写入日志文件的第一个用户将是唯一能够在当天剩余时间内写入日志文件的用户。
是否可以让 Laravel 以 666 权限保存日志?我发现的这个问题的解决方案建议运行 sudo chmod -R 666 storage/logs
,但这并不能真正解决问题 - Laravel将来会继续创建带有644掩码的文件。
好的,我终于能够解决它了。需要相当多的扩展,但我不需要编辑任何供应商文件,这很好。所以,它在这里:
新文件app/Bootstrap/ConfigureLogging.php
:
命名空间应用''引导程序; 使用照明''合同''基础''应用程序; 使用应用程序''支持''日志''编写器; 使用 Monolog''Logger 作为 Monolog; 类 ConfigureLogging 扩展 ''Illuminate''Foundation''Bootstrap''ConfigureLogging { 受保护的函数 configureDailyWithChmodHandler(Application $app, Writer $log) { $log->useDailyFilesWithChmod( $app->storagePath()."/logs/laravel.log', $app->make('config')->get('app.log_max_files', 5) ); } 受保护功能寄存器记录器(应用程序$app) { $app->实例('log', $log = new Writer( 新单语($app->环境()), $app['事件']) ); 返回$log; } }
在app/Console/Kernel.php
中,我已添加到顶部:
使用照明''合同''事件''调度程序; 使用照明''合同''基础''应用程序;
然后我添加了这个构造函数:
公共功能__construct(应用程序$app,调度程序$events) { $idx = array_search('Illuminate''Foundation''Bootstrap''ConfigureLogging', $this->bootstrappers); 如果 ($idx) { array_splice($this->bootstrappers, $idx, 1, 'App''Bootstrap''ConfigureLogging'); } 家长::__construct($app,$events); }
必须在app/Http/Kernel.php
中进行类似的更改:
使用"亮起''路由''路由器"; 使用照明''合同''基础''应用程序; ... 公共功能__construct(应用程序$app,路由器$router) { $idx = array_search('Illuminate''Foundation''Bootstrap''ConfigureLogging', $this->bootstrappers); 如果 ($idx) { array_splice($this->bootstrappers, $idx, 1, 'App''Bootstrap''ConfigureLogging'); } 家长::__construct($app,$router); }
然后app/Support/Handler/RotatingFileHandler.php
:
命名空间应用''支持''处理程序; 类 RotatingFileHandler extensions ''Monolog''Handler''RotatingFileHandler{ 受保护的函数写入(数组 $record) { 父::写($record); if ($this->必须旋转) { chmod($this->url, 0666); } } }
还app/Support/Log/Writer.php
:
命名空间应用''支持''日志; 使用应用程序''支持''处理程序''旋转文件处理程序; 类编写器扩展 ''照明''日志''编写器{ public function useDailyFilesWithChmod($path, $days = 0, $level = 'debug') { $this->monolog->pushHandler( $handler = new RotatingFileHandler($path, $days, $this->parseLevel($level)) ); $handler->setFormatter($this->getDefaultFormatter()); } }
最后替换config/app.php
中的"日志"选项:
'log' => 'dailyWithChmod'
您可能需要运行composer dump-autoload
以便找到新类。
如果您有任何问题并且Laravel没有显示任何错误,那么将ini_set('display_errors', 'on');
添加到您的内核应该会有所帮助。