hsl's avatar
Level 2

Log rotate

Hi Guys,

Right now I'm using a single log file, but that gets quite big quite fast when I add a lot of logging in my project.

Is it possible to get the logs daily, but named like this?

laravel.log

While the older (up to a week) are named like this:

laravel-2016-01-08.log
laravel-2016-01-07.log
laravel-2016-01-06.log
laravel-2016-01-05.log
laravel-2016-01-04.log
laravel-2016-01-03.log

I tried to use logrotate with the single log file, but after logrotate ran all my jobs except one stopped adding lines in the log file. And yes I did add the right filepermissions in the logrotate config.

Anyone has a suggestion how I can best achieve this?

Thanks!

0 likes
12 replies
joedawson's avatar

In your config/app.php there's a logging configuration option - from what I see in my 5.1 project at least, there's a few options.

"single", "daily", "syslog", "errorlog"

daily might be what you're after :)

4 likes
michaeldyrynda's avatar

In addition, using the daily option will also handle purging old log files.

You can add the log_max_files key to your config/app.php file if you want to keep more than the default five days.

3 likes
hsl's avatar
Level 2

Thanks guys, I already found the daily option but with the daily option today's logfile name is not laravel.log but has today's date. Is there any way to override that so it will be just laravel.log for todays logs?

skliche's avatar

@hsl Looking at the source of Monolog's RotatingFileHandler it doesn't seem to be an option out of the box. It always creates file names with the date appended.

martinbean's avatar

@hsl Why would you want daily log files without the date in the file name? What happens if you’re trying to debug something close to midnight?

hsl's avatar
Level 2

@martinbean Sorry for the late reply, but I want that because I want to be able to leave a tail -f laravel.log open on my server.

I fixed it by just using logrotate logrotate and use that to keep the log files for a week, todays log file is always laravel.log, yesterdays log file is laravel.log.1 and so on. :)

orrd's avatar

Yeah, I agree that the option of using "daily" isn't ideal since you then no longer have a log file with a simple static name so you can always read from it in the same place. So for now, configuring logrotate appears to be the best option for most of us.

For most servers with logrotate, the way you do that is by creating a file in /etc/logrotate.d such as "/etc/logrotate.d/laravel" with something similar to this:

/home/path-to-your-laravel-project-here/storage/logs/* 
/home/path-to-another-laravel-project-here/storage/logs/* 
{
        weekly
        missingok
        rotate 3
        compress
        notifempty
        copytruncate
}

If you want to test it, you can run "logrotate --force laravel" to force it to do one rotation immediately.

1 like
kobear's avatar

What I have done is setup rsyslogd to forward laravel.log to a local syslog facility. Then I tail that file, since it doesnt care about log rotation.

alvakoldo10's avatar

Using logrotate or any other outta Laravel is just a work around. What if you use a multi-user environment with multiple dirs and so different storage/logs files ? Surely a slightly more complex config should fit, but Laravel should manage its own log rotation.

Modifying function rotate of RotatingFileHandler.php should be the fix. I'll tell if I get it

shahzad.atta's avatar

I have a project in laravel 4. And I don't see any 'log' 'log_max_files' option in app/config/app.php. How do I configure log rotation in laravel 4. It is 4.1.* if it helps.

jjudge's avatar

I'm late here, but there is no reason why a symbolic link could not be used to point laravel.log to the latest named log file when it gets created, i.e. as a part of the rotation. That should involve minimum effort for a handy feature.

1 like

Please or to participate in this conversation.