Keep Logs Tidy With the Log Cleaner Package for Laravel


April 20th, 2021


Laravel Log Cleaner is a package by Joost van Veen for keeping your log files small and tidy. While there are various ways to keep log file size tidy in production, this package uses the built-in Artisan console that you can run manually, via cron directly, or through the scheduler:

2 ->command('logcleaner:run', [
3 '--keeplines' => 5000,
4 '--keepfiles' => 14
5 ])
6 ->daily()
7 ->at('01:00');

Log cleaner provides configurations you can set in your environment or override in the command. The main configuration features and console options include:

  • Specify the number of files to keep
  • Specify the number of lines to keep
  • Exclude pattern to exclude file patterns from processing
  • Log file trimming (enabled by default, configurable)
  • Configurable file deletion enabled/disabled

Cleaning computer

Here are some console command examples from the readme that give you a nice visualization of how to use this console command:

1# Get info about the command and options
2php artisan logcleaner:run --help
4# Trim big log files and delete old log files
5php artisan logcleaner:run
7# Pass the number of lines to keep when trimming log files. Overrides the config setting.
8# This overrides the default set in config
9php artisan logcleaner:run --keeplines=10000
11# Pass the number of files to keep when deleting old log files. Overrides the config setting.
12# This overrides the default set in config
13php artisan logcleaner:run --keepfiles=7
15# Run without actually cleaning any logs
16php artisan logcleaner:run --dry-run

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

This package was submitted to our Laravel News Links section. Links is a place the community can post packages and tutorials around the Laravel ecosystem. Follow along on Twitter @LaravelLinks

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.

Laravel News Partners