【问题标题】:Laravel 5 logs - Apache vs CLI conflictLaravel 5 日志 - Apache 与 CLI 冲突
【发布时间】:2015-06-17 14:02:32
【问题描述】:

默认情况下,Laravel 将每日日志文件保存在 storage/logs 中,权限为 644。当第一个日志条目由 Apache 编写,然后从 CLI 运行的 PHP 尝试编写某些内容(或相反)时,这会产生问题。如果我运行ls -lh storage/logs/,结果如下所示:

-rw-r--r-- 1 test.example.org test.example.org 36K Jun 15 10:07 laravel-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 掩码创建文件。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    好的,我终于可以解决它了。相当多的扩展是必要的,但我不需要编辑任何供应商文件,这很好。所以,就这样吧:

    新文件app/Bootstrap/ConfigureLogging.php

    命名空间应用\引导程序; 使用 Illuminate\Contracts\Foundation\Application; 使用 App\Support\Log\Writer; 使用 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) ); } 受保护的函数 registerLogger(Application $app) { $app->instance('log', $log = new Writer( 新独白($app->environment()), $app['events']) ); 返回$日志; } }

    app/Console/Kernel.php我已经添加到顶部:

    使用 Illuminate\Contracts\Events\Dispatcher; 使用 Illuminate\Contracts\Foundation\Application;

    然后我添加了这个构造函数:

    公共函数 __construct(Application $app, Dispatcher $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 中进行类似的更改:

    使用照明\路由\路由器; 使用 Illuminate\Contracts\Foundation\Application; ... 公共函数__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 扩展 \Monolog\Handler\RotatingFileHandler{ 受保护的函数写入(数组 $record) { 父::写($记录); if ($this->mustRotate) { chmod($this->url, 0666); } } }

    还有app/Support/Log/Writer.php:

    命名空间应用\支持\日志; 使用 App\Support\Handler\RotatingFileHandler; 类 Writer 扩展 \Illuminate\Log\Writer{ 公共函数 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'); 添加到你的内核应该会有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-02-26
      • 2015-06-02
      • 2012-03-16
      • 2020-07-17
      • 2015-04-17
      • 1970-01-01
      • 2019-07-11
      • 1970-01-01
      • 2015-04-15
      相关资源
      最近更新 更多