How Do I Clear Laravel Log

Laravel is an open-source PHP framework used for web application development.

It provides a built-in logging system to track errors and debug information.

Over time, logs can grow significantly, taking up disk space and potentially slowing down your application.

In this tutorial, we’ll go over the steps to clear Laravel logs in a safe and effective manner.


Why Clear Laravel Logs

As your Laravel application grows and runs for a long time, the log files can grow very large and take up a lot of disk space.

This can cause your application to slow down and make it more difficult to identify recent errors and debugging information.

Clearing the logs regularly will help to keep your application running smoothly.

How to Clear Laravel Logs

Using Artisan Command Line:

Laravel provides an Artisan command-line interface that makes it easy to perform various tasks, including clearing the logs.

To clear the logs, you can run the following command in your terminal or command prompt:

php artisan log:clear

This will clear all logs in the storage/logs directory and create a new empty log file.

Manually Deleting Log Files:

If you prefer to delete the logs manually, you can go to the storage/logs directory and delete the log files.

However, be sure to only delete the log files and not the directory itself, as the directory is required for Laravel’s logging system.

Configuring Log Management:

If you want to keep the logs for a specific period of time, you can configure the log management in Laravel.

You can use the log configuration file located in the config directory to set the log file size, number of rotated files, and more.

For example, the following code will rotate the logs every day and keep a maximum of 7 days’ worth of logs:

'log' => [
    'default' => env('LOG_CHANNEL', 'stack'),
    'channels' => [
        'stack' => [
            'driver' => 'daily',
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
            'days' => 7,
        ],
    ],
],

Conclusion

In this tutorial, we’ve covered the various methods of clearing Laravel logs.

Whether you prefer to use the Artisan command-line interface, delete the logs manually, or configure log management, there is a method for everyone.

Clearing the logs regularly will help keep your Laravel application running smoothly and free up disk space.