8

If there is an error when running php artisan command the log file will be created like this:

 -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

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

To reproduce this problem:

  1. delete all storage/logs/*.log files
  2. call some non existing php artisan command for example: php artisan make:xy -> this will make an error and create a .log file
  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'
5
  • 1
    Seems like you are running artisan and your webserver as different users
    – PtrTon
    Commented Jul 28, 2019 at 16:47
  • of course.... How do you run your artisan commands? as www-data user???
    – lewis4u
    Commented Jul 28, 2019 at 16:51
  • Have you tried sudo -u www-data php artisan? Commented Jul 28, 2019 at 16:53
  • 1
    No I haven't, that would maybe create a file as www-data user, but it seems kind of pita running sudo -u www-data php artisan don't you think so?
    – lewis4u
    Commented Jul 29, 2019 at 14:57
  • 1
    For me queues failed when executed on background, because background-worker couldn't Log into the log files due to permission issue, It worked after adding "permission" => 0755, in "config/logging.php" file, on Log Channels (daily-channel) Commented Jan 14, 2020 at 19:32

4 Answers 4

13

Was struggling with this for a long time, and wondered the same thing as the OP, who asked:

Is there a way to set default chmod for NEW created files, so that they always have rw for group

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:

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

0666 has resolved the permission conflicts for me. Hope this helps someone!

3
  • 2
    Thank you, This worked on laravel 5.8 (I added 'permission' => 0775 ) Commented Jan 14, 2020 at 19:28
  • 1
    I follow @ChardenDaxicen that's working on my CentOS
    – bravohex
    Commented Jan 24, 2020 at 15:26
  • Finally, this worked on laravel 8. Thank you Commented Aug 4, 2021 at 18:06
4

In my projects I solve this problem defining a custom logger that create a log file for each username.

  1. Create the custom logger on 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:

Inside 'channels' key, add your custom logger:

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

And change the default logger to the named channel:

    '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. This solves the permission issues.

Imo, this should be the default behavior for Laravel logging.

5
  • Very interesting solution, so every log file that is created is stored into separate file and there can't be collisions with user permissions?
    – lewis4u
    Commented Jul 28, 2019 at 21:43
  • Exactly @lewis4u. You can do some cool stuff with similar code. Consider accept this answer if you like it! Commented Jul 28, 2019 at 21:57
  • Also thanks for the editing. I wrote this on mobile, and unfortunately stackoverflow don't have a button for styling code on mobile. Commented Jul 28, 2019 at 22:00
  • Is this applicable for Laravel 5.3 also? Please, reply!
    – Mohal
    Commented Jan 22, 2021 at 17:51
  • Yes, it is. I use same solution in a Laravel 5.8 application Commented Jan 23, 2021 at 6:59
1

Change owner of the /var/www folder:

sudo chown www-data:www-data /var/www ( If your project is under /var/www folder )

You may replace www-data with the respective user level if you need to do for a different users like root.

4
  • This won't work. The problem is that the log file is daily, and if the first log message of the day is from www-data, the artisan won't be able to log anything. Same applies if artisan is the first to write a log message. Commented Jul 28, 2019 at 17:03
  • This is working for me as this command is to change the Owner of that project folder including all the files within the folder. You may try it out. And please correct me if I am still wrong. Commented Jul 28, 2019 at 17:08
  • The file is created as the user that's running php. When you run php artisan as someusername, the log file will belong to someusername, not as www-data. (php cant create files with another username, unless if it's root) Commented Jul 28, 2019 at 17:19
  • @EliasSoares has right. This will work ONLY if you already have files inside logs and not for new created ones!
    – lewis4u
    Commented Jul 28, 2019 at 22:11
1

For those who don't want any additional code just go into laravel storage folder through command line/terminal and run these 3 commands:

set default group 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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.