简体   繁体   English

Laravel 日志文件权限问题

[英]Laravel log file permission problems

If there is an error when running php artisan command the log file will be created like this:如果在运行php artisan命令时出现错误,日志文件将像这样创建:

 -rw-rw-r-- 1 user www-data 2,2K Jul 28 18:08 laravel-2019-07-28.log

If there is an error when using app through web browser the log file will be created like this:如果通过网络浏览器使用应用程序时出现错误,日志文件将如下创建:

-rw-r--r-- 1 www-data www-data 2,2K Jul 28 16:10 laravel-2019-07-28.log

After www-data has created the original file and if there is an error with php artisan command, it will throw an error Permission denied because it can't write to the log www-data 创建原始文件后,如果php artisan命令出现错误,则会抛出错误Permission denied因为无法写入日志

Is there a way to set default chmod for NEW created files, so that they always have rw for group Or you guys have some other solution for this有没有办法为新创建的文件设置默认 chmod,以便它们始终具有组的rw或者你们有其他一些解决方案

To reproduce this problem:要重现此问题:

  1. delete all storage/logs/*.log files删除所有 storage/logs/*.log 文件
  2. call some non existing php artisan command for example: php artisan make:xy -> this will make an error and create a .log file调用一些不存在的 php artisan 命令,例如: php artisan make:xy -> 这将产生错误并创建一个 .log 文件
  3. call route in browser /logout -> this will try to write in that same log file and will throw an error that it can't write into log 'Permission denied'在浏览器中调用路由 /logout -> 这将尝试写入同一个日志文件,并会抛出一个错误,表明它无法写入日志“权限被拒绝”

Was struggling with this for a long time, and wondered the same thing as the OP, who asked:为此苦苦挣扎了很长时间,并想知道与 OP 相同的事情,后者问:

Is there a way to set default chmod for NEW created files, so that they always have rw for group有没有办法为新创建的文件设置默认 chmod,以便它们始终具有组的 rw

The answer is YES .答案是肯定的

In config/logging.php I added permission => 0666 , which I found in the Laravel docs , so the config for my daily log now looks like this:config/logging.php我添加了permission => 0666 ,这是我在Laravel 文档中找到的,所以我的日常日志的配置现在看起来像这样:

'daily' => [
   'driver' => 'daily',
   'path' => storage_path('logs/laravel.log'),
   'level' => 'debug',
   'days' => 14,
   'permission' => 0666,
],

Just to be clear, 0664 would already be enough to set group permission to rw , but that still resulted in permission errors.需要明确的是, 0664已经足以将组权限设置为rw ,但这仍然会导致权限错误。

0666 has resolved the permission conflicts for me. 0666为我解决了权限冲突。 Hope this helps someone!希望这可以帮助某人!

In my projects I solve this problem defining a custom logger that create a log file fo reach username.在我的项目中,我定义了一个自定义记录器来解决这个问题,该记录器创建一个到达用户名的日志文件。

  1. Create the custom logger on app/Logging/UserNamedLogger.php :app/Logging/UserNamedLogger.php上创建自定义记录器:

     <?php namespace App\\Logging; use Monolog\\Handler\\RotatingFileHandler; use Monolog\\Handler\\SyslogHandler; use Monolog\\Logger; class UserNamedLogger { /** * Create a custom Monolog instance. * * @param array $config * @return \\Monolog\\Logger */ public function __invoke(array $config) { $logger = new Logger('UserNamedLogger'); // Configure Monolog to log on user named log files $filename = storage_path('logs/laravel-'. posix_getpwuid(posix_geteuid())['name'] .'.log'); $rotatingHandler = new RotatingFileHandler($filename); $logger->pushHandler($rotatingHandler); return $logger; } }
  2. Edit the config/logging.php config file:编辑config/logging.php配置文件:

Inside 'channels' key, add your custom logger:'channels'键中,添加您的自定义记录器:

    'named' => [
        'driver' => 'custom',
        'via' => App\Logging\UserNamedLogger::class,
    ],

And change the default logger to the named channel:并将默认记录器更改为named通道:

    'default' => env('LOG_CHANNEL', 'named'),

Now everytime that your artisan (or a scheduled job) runs, it will log on a different file that your www server do.现在,每次您的工匠(或计划的作业)运行时,它都会登录到您的 www 服务器所做的不同文件。 This solves the permission issues.这解决了权限问题。

Imo, this should be the default behavior for Laravel logging. Imo,这应该是 Laravel 日志记录的默认行为。

Change owner of the /var/www folder:更改 /var/www 文件夹的所有者:

sudo chown www-data:www-data /var/www ( If your project is under /var/www folder ) sudo chown www-data:www-data /var/www (如果您的项目在 /var/www 文件夹下)

You may replace www-data with the respective user level if you need to do for a different users like root .如果您需要为不同的用户(如root执行此操作,您可以将 www-data 替换为相应的用户级别。

For those who don't want any additional code just go into laravel storage folder through command line/terminal and run these 3 commands:对于那些不想要任何额外代码的人,只需通过命令行/终端进入 laravel 存储文件夹并运行以下 3 个命令:

set default group www-data设置默认组 www-data

find logs -type d -exec chgrp www-data {} +

set all new files and subfolders created within the current directory inherit the group of the directory设置当前目录中创建的所有新文件和子文件夹继承目录的组

find logs -type d -exec chmod g+s {} +

if user creates a file it will be with rw-rw-r permissions如果用户创建一个文件,它将具有 rw-rw-r 权限

sudo setfacl -R -d -m u::rw logs

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

相关问题 登录Laravel 5.1的文件权限 - File permission of Log in Laravel 5.1 laravel.log 文件 laravel 9 使用 Docker 的权限被拒绝 - permission denied on laravel.log file laravel 9 using Docker 即使在.ebextensions 配置文件中设置了权限,ElasticBeanstalk 上的 Laravel 的“日志文件权限被拒绝”错误也会不断出现 - Laravel on ElasticBeanstalk 'log file permission denied' error keeps coming up even the permission is set in the .ebextensions config file Laravel日志写入权限被拒绝 - Laravel log write permission denied Laravel:laravel.log权限被拒绝 - Laravel: laravel.log permission denied Laravel 7 XAMPP Mac OS:无法打开 stream 或文件“../storage/logs/laravel.log”。 没有权限 - Laravel 7 XAMPP Mac OS: The stream or file “../storage/logs/laravel.log” could not be opened. Permission denied xampp ubuntu laravel无法打开流或文件“ / storage / logs / laravel log”:打开流失败:权限被拒绝 - xampp ubuntu laravel The stream or file “/storage/logs/laravel log” could not be opened: failed to open stream: Permission denied Laravel文件权限错误 - Laravel file permission error 设置Laravel,获取PDO和权限问题 - Setting up Laravel, getting PDO and permission problems Laravel-Docker Permission denied 尝试记录时发生异常:流或文件 - Laravel-Docker Permission denied The exception occurred while attempting to log: The stream or file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM