我如何得到一个日志文件在Laravel


How do I get a log file in Laravel?

我有一个满是日志的文件!我把它们命名为apache.logs。
所以现在我需要在Laravel中获取它们,我不知道该怎么做。
我只是想保存所有的日志在一个变量($logs),并查看它们。

public function getlogs ()
    {
        $logs = 'Request::file('apache.log');
        return view('logs' , [
            'all_logs' => $logs
        ]);
    } 

这个不行,我不知道我需要修改什么

如果您使用'Request::file,那么该文件应该伴随着请求参数,但这里似乎您想使用laravel

访问文件系统中存储的文件。

ini_set('memory_limit','256M');
$logs = 'File::get('apache.log');
return view('logs' , [
    'all_logs' => $logs
]);

如果你的文件和composer.json一样在根目录下,那么你需要改变路径来匹配它,比如

ini_set('memory_limit','256M');
$path = base_path()."/apache.log"; //get the apache.log file in root
$logs = 'File::get($path);
return view('logs' , [
    'all_logs' => $logs
]);

将其包装在try catch块中以获得最佳实践,因为在某些情况下,如果apache.log不存在或无法访问,则需要处理它

try {
    ini_set('memory_limit','256M');
    $path = base_path()."/apache.log"; //get the apache.log file in root
    $logs = 'File::get($path);
    return view('logs' , [
        'all_logs' => $logs
    ]);
} catch (Illuminate'Filesystem'FileNotFoundException $exception) {
    // handle the exception
}