I’m trying to write a Laravel 8.0 task that renames the default log file every week and makes another empty file. I’m using a Debian 10 virtual machine as a testing environment.
Here’s my handle()
code:
$now = Carbon::now();
$baseLog = "/laravel.log";
$oldLog = storage_path("logs".$baseLog);
$renamedLog = storage_path("logs"."/log_".$now->day."_"
.$now->month."_".$now->year.".log");
Storage::move($oldLog, $renamedLog);
Storage::delete($oldLog);
Storage::put(storage_path("logs".$baseLog), "");
The problem is that Laravel
says…
File not found at path: var/www/html/storage/logs/laravel.log
2
Answers
As you correctly pointed out, I called delete on a moved file (my bad) but the error happened before, while trying to locate the laravel.log.
After adding an exists check, I noticed the storage path returned has an "/app" so I've just created another disk instance in
config/filesystems
and then I could finally resolve my issue like that:
Thank you
Add check for file exists and not use delete – because you move file:
Or, if this is not for studying, use package for log rotate, e.g. this