如何在Laravel中获取日志文件?

时间:2015-10-19 06:25:31

标签: php laravel

我有一个充满日志的文件!我称他们为apache.logs 所以现在我需要让他们在Laravel,我不知道如何 我只想将所有日志保存在变量($logs)中并查看它们。

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

这不起作用,我也不知道需要改变什么。

1 个答案:

答案 0 :(得分:1)

如果您使用\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不存在或无法访问laravel trow异常,我们需要处理它

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
}