Php Laravel 5日志-Apache与CLI冲突
默认情况下,Laravel以644权限将每日日志文件保存在Php Laravel 5日志-Apache与CLI冲突,php,laravel,Php,Laravel,默认情况下,Laravel以644权限将每日日志文件保存在存储/logs中。当第一个日志条目由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
存储/logs
中。当第一个日志条目由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
-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掩码的文件。好的,我终于能够解决它了。相当多的扩展是必要的,但我不需要编辑任何供应商文件,这很好。那么,下面是: 新文件
app/Bootstrap/ConfigureLogging.php
:
namespace App\Bootstrap;
use Illuminate\Contracts\Foundation\Application;
use App\Support\Log\Writer;
use Monolog\Logger as Monolog;
class ConfigureLogging extends \Illuminate\Foundation\Bootstrap\ConfigureLogging {
protected function configureDailyWithChmodHandler(Application $app, Writer $log)
{
$log->useDailyFilesWithChmod(
$app->storagePath().'/logs/laravel.log',
$app->make('config')->get('app.log_max_files', 5)
);
}
protected function registerLogger(Application $app)
{
$app->instance('log', $log = new Writer(
new Monolog($app->environment()), $app['events'])
);
return $log;
}
}
use Illuminate\Routing\Router;
use Illuminate\Contracts\Foundation\Application;
...
public function __construct(Application $app, Router $router)
{
$idx = array_search('Illuminate\Foundation\Bootstrap\ConfigureLogging', $this->bootstrappers);
if ($idx) {
array_splice($this->bootstrappers, $idx, 1, 'App\Bootstrap\ConfigureLogging');
}
parent::__construct($app, $router);
}
namespace App\Support\Handler;
class RotatingFileHandler extends \Monolog\Handler\RotatingFileHandler{
protected function write(array $record)
{
parent::write($record);
if ($this->mustRotate) {
chmod($this->url, 0666);
}
}
}
namespace App\Support\Log;
use App\Support\Handler\RotatingFileHandler;
class Writer extends \Illuminate\Log\Writer{
public function useDailyFilesWithChmod($path, $days = 0, $level = 'debug')
{
$this->monolog->pushHandler(
$handler = new RotatingFileHandler($path, $days, $this->parseLevel($level))
);
$handler->setFormatter($this->getDefaultFormatter());
}
}
'log' => 'dailyWithChmod'
然后我添加了这个构造函数:
public function __construct(Application $app, Dispatcher $events)
{
$idx = array_search('Illuminate\Foundation\Bootstrap\ConfigureLogging', $this->bootstrappers);
if ($idx) {
array_splice($this->bootstrappers, $idx, 1, 'App\Bootstrap\ConfigureLogging');
}
parent::__construct($app, $events);
}
然后app/Support/Handler/RotatingFileHandler.php
:
namespace App\Bootstrap;
use Illuminate\Contracts\Foundation\Application;
use App\Support\Log\Writer;
use Monolog\Logger as Monolog;
class ConfigureLogging extends \Illuminate\Foundation\Bootstrap\ConfigureLogging {
protected function configureDailyWithChmodHandler(Application $app, Writer $log)
{
$log->useDailyFilesWithChmod(
$app->storagePath().'/logs/laravel.log',
$app->make('config')->get('app.log_max_files', 5)
);
}
protected function registerLogger(Application $app)
{
$app->instance('log', $log = new Writer(
new Monolog($app->environment()), $app['events'])
);
return $log;
}
}
use Illuminate\Routing\Router;
use Illuminate\Contracts\Foundation\Application;
...
public function __construct(Application $app, Router $router)
{
$idx = array_search('Illuminate\Foundation\Bootstrap\ConfigureLogging', $this->bootstrappers);
if ($idx) {
array_splice($this->bootstrappers, $idx, 1, 'App\Bootstrap\ConfigureLogging');
}
parent::__construct($app, $router);
}
namespace App\Support\Handler;
class RotatingFileHandler extends \Monolog\Handler\RotatingFileHandler{
protected function write(array $record)
{
parent::write($record);
if ($this->mustRotate) {
chmod($this->url, 0666);
}
}
}
namespace App\Support\Log;
use App\Support\Handler\RotatingFileHandler;
class Writer extends \Illuminate\Log\Writer{
public function useDailyFilesWithChmod($path, $days = 0, $level = 'debug')
{
$this->monolog->pushHandler(
$handler = new RotatingFileHandler($path, $days, $this->parseLevel($level))
);
$handler->setFormatter($this->getDefaultFormatter());
}
}
'log' => 'dailyWithChmod'
最后替换config/app.php
中的“log”选项:
namespace App\Bootstrap;
use Illuminate\Contracts\Foundation\Application;
use App\Support\Log\Writer;
use Monolog\Logger as Monolog;
class ConfigureLogging extends \Illuminate\Foundation\Bootstrap\ConfigureLogging {
protected function configureDailyWithChmodHandler(Application $app, Writer $log)
{
$log->useDailyFilesWithChmod(
$app->storagePath().'/logs/laravel.log',
$app->make('config')->get('app.log_max_files', 5)
);
}
protected function registerLogger(Application $app)
{
$app->instance('log', $log = new Writer(
new Monolog($app->environment()), $app['events'])
);
return $log;
}
}
use Illuminate\Routing\Router;
use Illuminate\Contracts\Foundation\Application;
...
public function __construct(Application $app, Router $router)
{
$idx = array_search('Illuminate\Foundation\Bootstrap\ConfigureLogging', $this->bootstrappers);
if ($idx) {
array_splice($this->bootstrappers, $idx, 1, 'App\Bootstrap\ConfigureLogging');
}
parent::__construct($app, $router);
}
namespace App\Support\Handler;
class RotatingFileHandler extends \Monolog\Handler\RotatingFileHandler{
protected function write(array $record)
{
parent::write($record);
if ($this->mustRotate) {
chmod($this->url, 0666);
}
}
}
namespace App\Support\Log;
use App\Support\Handler\RotatingFileHandler;
class Writer extends \Illuminate\Log\Writer{
public function useDailyFilesWithChmod($path, $days = 0, $level = 'debug')
{
$this->monolog->pushHandler(
$handler = new RotatingFileHandler($path, $days, $this->parseLevel($level))
);
$handler->setFormatter($this->getDefaultFormatter());
}
}
'log' => 'dailyWithChmod'
'log'=>'dailyWithChmod'
您可能需要运行composer dump autoload
,以便找到新类
如果您有任何问题,并且Laravel没有显示错误,请添加ini_集('display_errors','on')代码>到内核应该有帮助