Delete File quá cũ và file dung lượng lớn và đọc log 100 dòng cuối

Delete File quá cũ và file dung lượng lớn và đọc log 100 dòng cuối

Trong khi dev bạn đôi khi sẽ gặp những vấn đề này 1 là phải đọc file log , xóa file cũ ,2 là file dung lượng lớn 

Có 1 vài hệ thống mà chúng ta gặp vấn đề này đó là hệ thống log hệ thống và hệ thống để người dùng tạo báo cáo và download file về 

1 : Hệ thống log 

Hệ thống log thì thường sẽ gặp 1 vấn đề đó là đọc log 

Dưới đây là đoạn code nhằm đọc 100 dòng cuối cùng hệ thống :

  $so_line_cuoi_can_lay = 200;
        $file = file(storage_path('logs\deletefilelarger.log'));
        for ($i = max(0, count($file)-$so_line_cuoi_can_lay); $i < count($file); $i++) {
            $file_return[] = $file[$i];
        }

2 : Hệ thống xuất báo cáo và đọc báo cáo

Thường thì người dùng xử dụng tính năng kết xuất báo cáo và import file lên hệ thống 1 đến 2 lần 1 file  .Theo thời gian thì các file này sẽ làm đầy thư mục mà chúng không còn tác dụng gì nữa .Vậy việc bây giờ chúng ta là cần tìm ra các file đó và xóa đi 

a : file quá cũ 

<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

class DeleteFileOld extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:delete-file-old';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Xóa file sau 10 ngày';

    /**
     * Execute the console command.
     */
    public function handle(): void
    {
        $files = Storage::disk('storage')->allFiles('logs');
        foreach ($files as $file) {
            $file_create_time = Storage::disk('storage')->lastModified($file);
            $datetime_create_file = Carbon::createFromTimestamp($file_create_time)->toDateTime();
            $time_moc = Carbon::now()->subDays(10);
            if($time_moc > $datetime_create_file)
            {
                $status = Storage::disk('storage')->delete($file);
                if ($status){
                    $this->info(Carbon::now().'-delete-'.$file);
                }
                else{
                    $this->error(Carbon::now().'-error-'.$file);
                }
            }
        }
    }
}

File dung lượng lớn 

<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

class DeteleFileLarger extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:detele-file-larger';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Xóa file quá lớn';

    /**
     * Execute the console command.
     */
    public function handle(): void
    {
        // mặc định 20 mb
        $max_lager = 100000 *20 ;
        $files = Storage::disk('storage')->allFiles('logs');
        foreach ($files as $file) {
            $file_size = Storage::disk('storage')->size($file);
            if ($file_size > $max_lager) {
                $status = Storage::disk('storage')->delete($file);
                if ($status){
                    $this->info(Carbon::now().':delete-larger:1:'.$file);
                }
                else{
                    $this->error(Carbon::now().':delete-larger:0:'.$file);
                }
            }

        }
    }
}

Trong Kennel của Command:

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\Log;

class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     */
    protected function schedule(Schedule $schedule): void
    {
        // $schedule->command('inspire')->hourly();
        $schedule->command('app:cache-d-m-s')->dailyAt('1:50')->appendOutputTo(storage_path('logs\schedule.log'));
        $schedule->command('app:delete-file-old')->dailyAt('2:30')->appendOutputTo(storage_path('logs\deletefile.log'));
        $schedule->command('app:detele-file-larger')->everyMinute()->appendOutputTo(storage_path('logs\deletefilelarger.log'));
    }

    /**
     * Register the commands for the application.
     */
    protected function commands(): void
    {
        $this->load(__DIR__.'/Commands');
        require base_path('routes/console.php');
    }
}