简体   繁体   中英

Laravel log file created with root:root instead of webapp:webapp

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.

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM