简体   繁体   English

使用root:root而不是webapp:webapp创建的Laravel日志文件

[英]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. 在我的应用程序中,我使用laravel.log文件,该文件每天在世界标准时间00:00创建。 However, sometimes the file gets created with root:root user/group instead of usual webapp:webapp (apache user). 但是,有时该文件是使用root:root用户/组而不是通常的webapp:webapp(apache用户)创建的。 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? Laravel中有没有一种方法可以尝试/捕获日志记录,以使应用程序不会由于文件写入错误而崩溃?

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. 只是laravel.log文件的所有权偶尔更改而无需干预。

You should use php_sapi_name() instead of get_current_user(). 您应该使用php_sapi_name()而不是get_current_user()。 As explained in the comment by Jason on the same answer you tried. 正如杰森(Jason)对您尝试的相同答案的评论中所述。

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. 请注意,get_current_user()返回当前PHP脚本的所有者(根据php.net),而不是当前正在运行该脚本的用户。 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. 我改用php_sapi_name(),它给出了将以不同用户身份运行的php处理程序的名称(例如,apache或cli)。

This way you would get 2 separate logs for web server & commandline(including queue listener). 这样,您将为Web服务器和命令行获取2个单独的日志(包括队列侦听器)。

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. 在极少数情况下,当我的Web应用程序有机会在午夜UTC之后(几分钟内)旋转日志文件之前,我的队列侦听器启动时,该文件将以root所有者的身份创建。

I modified the log file name to include the current user thus isolating the logs of each user, avoiding any permission issues. 我修改了日志文件名以包括当前用户,从而隔离了每个用户的日志,避免了任何权限问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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