mercredi 21 octobre 2015

Laravel: change log path to include user folder

I use Laravel 5.1 and I am trying to intercept the default logger configuration (Monolog) and save logs to a different path that includes user name.

The current logs are saved to storage/logs/laravel.log.

The wanted paths are as follows

Authenticated Users: storage/logs/[username]/[date]_[api_path].log

Other user logs can be saved under storage/logs/guest/[date]_[api_path].log

ServiceProvider Approach

Have a LogServiceProvider and where I can modify each request and set the path as wanted.

public function boot(Request $request)
{

    $log = new Logger('View Logs');
    $user = \Auth::User()->getName(); // ERROR - uninitialized
    $path = 'storage/logs/'.$user.'/mylogfile.log'; // doesn't matter API path
    $log ->pushHandler(new StreamHandler($path, Logger::INFO));
    ...
 }

The problem with this approach, the Auth::user() seems to be uninitialized.

Why does this happen and how do I solve this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire