Home > Software engineering >  Laravel - Save new log in new file and erase file
Laravel - Save new log in new file and erase file

Time:02-17

In laravel (v9) project i try to launch some bash command, like this

   <?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
use Carbon\Carbon;

class SaveLog extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'eraselog';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Save current log and erase old log';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

        $copy = new Process(['cp storage/logs/laravel.log storage/logs/laravel_new.log']);

        $erase = new Process([' > storage/logs/laravel.log']);

        $copy->run();
        $erase->run();
    }
}

The problem: Commands doesn't work. Maybe this ins´t the proper way to launch this commands. So, anybody shows what's the right way?

CodePudding user response:

You can use the Laravel File Storage commands:

Storage::disk('logs')->copy('laravel.log', 'laravel_new.log');

Storage::disk('logs')->delete('laravel.log');

You need add a new "disk" into the "config/filesystems.php":

    'disks' => [
       'logs' => [
          'driver' => 'logs',
          'root' => storage_path('logs'),
       ],
    ],

Laravel Docs: https://laravel.com/docs/9.x/filesystem#copying-moving-files

  • Related