laravel的日志记录类型设置为daily,日志的保存天数默认为5天(即最大日志文件个数为五个):

protected function configureDailyHandler(Application $app, Writer $log)
{
        $log->useDailyFiles(
            $app->storagePath().'/logs/laravel.log',
            $app->make('config')->get('app.log_max_files', 5)
        );
}

可修改默认的设置,假如要保存30天的日志,则配置如下:

在配置文件config/app.php中添加如下代码:

'log_max_files' => 30,

如果不限制日志文件数量,则配置如下:

'log_max_files' => 0,

修改配置后,需要重启php服务器,配置才会生效.

分析日志删除触发机制:

日志删除机制:

protected function rotate()
    {
        // update filename
        $this->url = $this->getTimedFilename();
        $this->nextRotation = new \DateTime('tomorrow');

        // skip GC of old logs if files are unlimited
        if (0 === $this->maxFiles) {//如果设置为0则不限制
            return;
        }

        $logFiles = glob($this->getGlobPattern());
        if ($this->maxFiles >= count($logFiles)) {
            // no files to remove
            return;
        }

        // Sorting the files by name to remove the older ones
        usort($logFiles, function ($a, $b) {
            return strcmp($b, $a);
        });

        foreach (array_slice($logFiles, $this->maxFiles) as $file) {
            if (is_writable($file)) {
                // suppress errors here as unlink() might fail if two processes
                // are cleaning up/rotating at the same time
                set_error_handler(function ($errno, $errstr, $errfile, $errline) {});
                unlink($file);//删除过期日志动作
                restore_error_handler();
            }
        }

        $this->mustRotate = false;
    }

在生成新的日志文件时,会去删除过期日志.