Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

super_simon's avatar

How can i set a Log file size

Hello. Can i set log rotation by log-file size? (Laravel 5.0.xx)

0 likes
19 replies
iTypo's avatar

This is a good question, Laravel now fills up the logs when an error occurs and the daemon is running.

I had a 400GB+ logfile when it could not connect to the DB and it kept trying overnight.

iTypo's avatar

Anyone? A simple way to limit the log file size for example would do.

kfirba's avatar

@super_simon @iTypo @mani95lisa why not just set a cronjob on the server to empty the log file?

Consider running this script every x seconds/minutes/hours/days/etc..

!#/bin/bash

file = /home/user/myapp.com/storage/logs/laravel.log # log file path
maxSize = 2048 #2MB - size in kilobytes
fileSize = $(du -k "$file" | cut -f 1)

if [ $fileSize -ge $maxSize]; then
    echo "" > $file
fi

Note that the script above checks for the actual space being used by the file.

ohffs's avatar

It might be easier to use the 'daily' log setting in config/app.php :

    /*
    |--------------------------------------------------------------------------
    | Logging Configuration
    |--------------------------------------------------------------------------
    |
    | Here you may configure the log settings for your application. Out of
    | the box, Laravel uses the Monolog PHP logging library. This gives
    | you a variety of powerful log handlers / formatters to utilize.
    |
    | Available Settings: "single", "daily", "syslog", "errorlog"
    |
    */

    'log' => 'single',  // change this to daily
nfauchelle's avatar

Have you looked into logrotate? It will also do compression and limit the amount of logs as well.

kfirba's avatar

@ohffs @Devmaurice if I'm not mistaken, that would create many log files. One per day. It won't delete the old one.

@nfauchelle @bashy well, that's also a solution. If he wants a pain free single log file he can just use cronjob and not over complicate such an easy task.

bashy's avatar

@ohffs Laravel? Yeah with logrotate you can set it up to how you want. Change it per week or per 3 days or how big it gets.

ohffs's avatar

@bashy yeah - I think laravel's internal 'daily' option defaults to 5 days worth - possibly a bit easier than logrotate as you can config it within the app. Horses for courses though :-)

mani95lisa's avatar

@kfirba thanks, my logs filled by Redis errors which caused by queue. I set --tries=3, but the Redis errors been logged forever, like a endless loop. I have fixed below bug about redis, but have no idea about endless loop errors.

RedisError: MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled

So I'm thinking to limit the max size of logs, to keep the disk space low.

Cron is a good idea, thanks again! I'll try to use shell to limit logs folder's size.

mani95lisa's avatar

@ohffs the point is endless error logs fill up disk space, not the way log saved, I use daily log too, thanks.

ohffs's avatar

@mani95lisa it sounds like you should be fixing that bug rather than dealing with the logs ;-) Your redis install is probably trying to save it's backup to a directory it doesn't have permissions on :-) Have a look in your redis.conf file and see where it's pointing to (the path is under the 'working directory' section of the config file).

iTypo's avatar

Indeed it has nothing to do with daily, or limit the amount of log files, i'm already using that.

The problem is when for example mysqld goes does in the middle of the night, and you have many laravel queue workers they start generating thousands of errors filling up the disk real fast.

We need a setting to limit the log file SIZE. Or some way when its repeating the same error thousands of times, maybe just use a counter instead of the whole error msg?

iTypo's avatar

I know i can manage it myself, but laravel offers very nice features like daily logs, limit number of logs, wouldent it be nice if it also had an out of the box feature which prevents your server from going down because of laravel error looping? (creating 400GB+ logs overnight)

I think it would be a great addition to laravel instead of just saying go manage it yourself using X external tool.

Please or to participate in this conversation.