Evolution48
Evolution48

Reputation: 169

How do I get a log file in Laravel?

I have a file full of logs! I called them apache.logs.
So now I need to get them in Laravel and I don't know how.
I just want to save all logs in a variable ($logs) and view them.

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

This doesn't work and I don't know what I need to change.

Upvotes: 1

Views: 3232

Answers (1)

Kalhan.Toress
Kalhan.Toress

Reputation: 21901

If your using \Request::file then the file should come along with the request params, But here seems like you want to access a stored file in file system using laravel

to do that

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

UPDATE

if you file is in root same like composer.json then you need to change the path to match with it like

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
]);

wrap this in a try catch block for best practice, because in some case if apache.log not exists or not accessible laravel trow a exception, we need to handle it

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
} 

Upvotes: 2

Related Questions