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

amirkamizi's avatar

make Laravel log files by the hour instead of day

at the moment the logs are set at daily. but even in a daily log the files some times get too big. I want to make it to smaller chunks. is it possible to make it rotate hourly rather than daily?

I'm using laravel 7.*

here I found a similar question

https://stackoverflow.com/questions/54708321/how-can-i-make-laravel-log-files-by-the-hour-instead-of-day

but I couldn't find a documentation or tutorial on how to make a driver that would do that. I'd appreciate you help.

0 likes
7 replies
bugsysha's avatar

Just curious what would happen if you replace 'path' => storage_path('logs/laravel.log'), with following:

'path' => storage_path('logs/laravel-' . (new \DateTime)->format('Y-m-d-H-i') . '.log'),
amirkamizi's avatar

I was curious too so I tested it. config is getting cached. it didn't have any effect.

amirkamizi's avatar

in that section it doesn't have anything regarding naming the files. however in front of the daily channel driver it says "A RotatingFileHandler based Monolog driver which rotates daily"

I wonder if it's possible to modify the rotatingFileHandler. what do you think? do you have any idea how I could do that?

amirkamizi's avatar

I couldn't create what i wanted so I ended up what @bugsysha said with what I found at https://safeharbourtechnology.com/blog/monolog-laravel-custom-logs and the final result is

		$logPath = storage_path('logs/customTest-' . (new \DateTime)->format('Y-m-d-H-i') . '.log');
        $orderLog = new Logger('production');
        $orderLog->pushHandler(new StreamHandler($logPath), Logger::INFO);
        $orderLog->info('Search : ',['data'=>"2"],);

now my problem is with the way it logs the data

what it logs looks like this

[2021-07-19T16:11:45.611001+00:00] production.INFO: Search :  {"data":"2"} []

but the file name is exactly the way I wanted

customTest-2021-07-19-16.log
bugsysha's avatar

I would just append minutes and seconds because it looks weird without it.

$logPath = storage_path('logs/customTest-' . (new \DateTime)->format('Y-m-d-H-i') . '-00-00.log');
Snapey's avatar

i would review what you are logging, sounds like you have too much to monitor to be of any use

Please or to participate in this conversation.