Laravel每日日志创建权限错误 [英] Laravel daily log created with wrong permissions

查看:83
本文介绍了Laravel每日日志创建权限错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,该脚本是使用php artisan(具有 root 用户)运行的,有时它会导致在Apache www-data 之前创建每日日志文件.用户这样做-这意味着当真实用户使用我的Web应用程序时,我会收到文件夹权限错误:

I have a script that I run using php artisan (with root user), and sometimes it causes the daily log file to be created before the apache www-data user does - which means that when a real user uses my web application, I get the folder permission error:

无法打开流:权限被拒绝

Failed to open stream: Permission denied

我每次都将权限更改回 www-data ,但是我想通过始终使用正确的权限创建日志文件来解决此问题.

I change the permissions back to www-data everytime but I want to solve this by having the log file always created with the correct permissions.

我已经考虑过创建一个cron作业来创建文件或对其进行触摸以确保它每天都有正确的权限,但是我正在寻找一种不依赖于另一个脚本的更好的解决方案.

I've considered creating a cron job that creates the file or touches it to make sure it has the right permission everyday, but I'm looking for a better solution that doesn't rely on another script.

我们还考虑过将php artisan包装在另一个脚本中,以确保它始终以 www-data 凭据运行,但是我们要做的实际上是 root 不允许执行apache的程序.

We've also considered wrapping php artisan in another script to make sure that it is always run with the www-data credentials, but somethings that we want to do are actually root procedures that apache should not be allowed to do.

还有其他建议吗?

推荐答案

让我们从常量开始.

您有一个php artisan命令,由root运行.

You have a php artisan command, run by root.

可以安全地假定此命令是每天执行的.

It is safe to assume that this command is executed daily.

解决方案1:

鉴于创建文件的用户默认是有权写入该文件的用户,我们可以按以下方式按用户分隔日志:

Given that the user that creates the files is the one that has the permission to write to it by default, we can separate the logs by user as such:

App/start/global.php

/*
|--------------------------------------------------------------------------
| Application Error Logger
|--------------------------------------------------------------------------
|
| Here we will configure the error logger setup for the application which
| is built on top of the wonderful Monolog library. By default we will
| build a basic log file setup which creates a single file for logs.
|
*/

Log::useDailyFiles(storage_path().'/logs/laravel-'.get_current_user().'.log');

如果您的 www-data 用户要创建错误日志,则会导致:storage/logs/laravel-www-data-2015-4-27.log.

If your www-data user were to create an error log, it would result in: storage/logs/laravel-www-data-2015-4-27.log.

如果您的 root 用户要创建错误日志,则会导致:storage/logs/laravel-root-2015-4-27.log.

If your root user were to create an error log, it would result in: storage/logs/laravel-root-2015-4-27.log.

解决方案2:

在php脚本中更改您的artisan命令使用的日志.

Change the log used by your artisan command, in your php script.

在您的run()函数中,在开始处添加以下行:

In your run() function, add this line at the start:

Log::useFiles(storage_path().'/logs/laravel-'.__CLASS__.'-'.Carbon::now()->format('Y-m-d').'.log');

如果您的班级名称是ArtisanRunner,则您的日志文件将是:

If your class's name is ArtisanRunner, then your log file will be:

storage/logs/laravel-ArtisanRunner-2015-4-27.log.

结论:解决方案编号1更好,因为它按用户划分了您的日志,因此不会发生任何错误.

Conclusion: Solution number 1 is better, given that it delineates your logs by user, and hence no errors will occur.

正如jason指出的那样,get_current_user()返回脚本的所有者名称.因此,要应用第1号解决方案,请chown将您的工匠类文件添加到所需的用户名.

As pointed out by jason, get_current_user() returns the script's owner name. Hence, for solution no.1 to apply, chown your artisan class files to the required username.

这篇关于Laravel每日日志创建权限错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆