4

In my applicaiton, I use laravel.log file that are created daily at 00:00 UTC. However, sometimes the file gets created with root:root user/group instead of usual webapp:webapp (apache user). This crashes my application because the application can no longer write to it. I noticed that the file ownership may be changing after the file has been written for a while (couple of minutes).

  1. What could have potentially caused the ownership of the file to change after it is created?
  2. Is there a way in Laravel to try/catch logging so that the application doesn't crash due to file write errors?

Permission on the storage directory and files is not an issue because it has been working fine for a long time. It is only the laravel.log file's ownership changing occasionally without intervention.

show-me-the-code
  • 1,553
  • 1
  • 12
  • 14
  • Are you running your crons as root? You should consider using ACLs on the storage directory to ensure the webserver always has access. – ceejayoz May 07 '15 at 03:15
  • It sounds like whatever process is running daily to create the log file is being executed by (or as) root. – Stuart Wagner May 07 '15 at 03:15

2 Answers2

2

You should use php_sapi_name() instead of get_current_user(). As explained in the comment by Jason on the same answer you tried.

Please note that get_current_user() returns the owner of the current PHP script (according to php.net) and not the user who is currently running the script. I use php_sapi_name() instead, which gives the name of the php handler (apache or cli, for example) which will tend to be run as different users.

This way you would get 2 separate logs for web server & commandline(including queue listener).

Vikas
  • 993
  • 1
  • 10
  • 16
  • Thanks @Vikas. It's been a while since the post, and I have made several architectural changes in the way my queue is implemented, and how my log files are generated. This is no longer an issue for me. I use cron to create log files with proper permissions ahead of time, so that it never gets created with wrong permission. But I appreciate the answer, it may come handy some time. – show-me-the-code Dec 03 '15 at 14:37
0

Found the solution here.

His solution made me realize that the problem is my queue listener. In rare occasions when my queue listener kicks in before my web application has had a chance to rotate the log file after midnight UTC (within a couple of minutes), the file is created with as root owner.

I modified the log file name to include the current user thus isolating the logs of each user, avoiding any permission issues.

Community
  • 1
  • 1
show-me-the-code
  • 1,553
  • 1
  • 12
  • 14
  • Ok, here's the deal. This didn't work! I modified the filename as "laravel" . get_current_user(). ".log" as suggested. My log file is now named "laravelwebapp.log". However, the file got created again with "root:root" ownership, and still had the filename "laravelwebapp.log" while I would have expected it to be "laravelroot.log" when it created with root owner. I have no idea what is going on here. Help, anyone? – show-me-the-code May 08 '15 at 00:03