Laravel Log storage in S3 bucket

|
| By Webner

How to Save Logs of Laravel in Amazon S3 Bucket?

Problem – We are working on a laravel project in which logs are saved in the storage folder of laravel and it is using a lot of space. We need to save logs of laravel in Amazon S3 bucket. For this we used laravel filesystem package and amazon s3 bucket.

Steps:
1. Install new laravel project using below command. Note that firstly you need to install composer to start with laravel project.

composer create-project --prefer-dist laravel/laravel project-name

2. Now to use S3 driver you need to install package for it using composer with the help of below command.

composer require “league/flysystem-aws-s3-v3 ~1.0”

Above command will install the package for S3 driver. Now you are all set with the installation.

3. Open your Amazon account and then open S3 bucket and get credentials of S3 bucket.

4. Open filesystem.php in the “config” folder in the project. There will be array of disks arrays. Find disk for s3. The array will be as below.

's3' => [
            'driver' => 's3',
            'key' => 'AWS_ACCESS_KEY_ID',
            'secret' => 'AWS_SECRET_ACCESS_KEY',
            'region' => 'AWS_DEFAULT_REGION',
            'bucket' => 'AWS_BUCKET',
 'url' => 'AWS_URL',
           ],

5. Enter the credentials of the S3 bucket in the above array.

6. Now add this code. This will create one more disk which will be used to get log files of laravel.

 
'log' => [
            'driver' => 'local',
           		 'root' => storage_path(),
            ],

7. Now open .env file of you project where you will find key “LOG_CHANNEL”. Edit its value to “daily”. Now laravel will create log files on the daily bases. If you want to save logs everyday in same file then do not change .env file.

8. Now run below command to clear the configurations of laravel project. You are all set with the configurations.

php artisan config:clear

9. Now we will create a command using laravel to move all log files to S3 bucket. Run below command to create a new command in laravel. This will create command “MoveLog”.

 php artisan make:command MoveLog

10. This will create MoveLog.php file in app/console/command folder.

11. Open MoveLog.php file. There will be two variables $signature and $description. Set value of $signature variable to “moveLogFiles”. Using this $signature value, we will be able to run the new created command.

12. You can set $description variable according to your functionality like “Move laravel log to S3 bucket.” This variable is for the description of command. This will be shown when we will list the all commands using below command.

 php artisan route:list

13. There will be a function with the name “handle”. This function will run whenever we will run this command. Now we will write code for moving files to S3 bucket.

14. Now insert the below code into the handle function.

$localDisk = Storage::disk('log'); //getting “log” disk instance
    		$localFiles = $localDisk->allFiles('logs'); // getting all log files in from location “storage/logs”
    		$cloudDisk = Storage::disk('s3'); //getting instance of “s3” disk
    		$pathPrefix = 'BackupLogs' . DIRECTORY_SEPARATOR . Carbon::now() . DIRECTORY_SEPARATOR;  // setting target path for log files.
    		foreach ($localFiles as $file) 
                {
    			$contents = $localDisk->get($file);
    			$cloudLocation = $pathPrefix . $file;
    			$cloudDisk->put($cloudLocation, $contents);
    			$localDisk->delete($file);
    		} // moving log files into s3 bucket.

15. Whenever we will run below command, above code will run and move the log files to S3 bucket.

php artisan moveLogFiles

Leave a Reply

Your email address will not be published. Required fields are marked *