-2

I use monolog in symfony 4.4 with apache http server. Logging is working fine. But I've got a problem.

There are created following log files in prod environment:

  • File prod.log with file rights 770, user permission "user1" and group permission "www-data"
  • File prod_deprecations.log with file rights 644, user permission "www-data" and group permission "www-data"
  • File prod_errors.log with file rights 666, user permission "www-data" and group permission "www-data"

As you can see, the file rights, user permissions and group permissions from log files are different.

And I want to achieve that all three log files have the same file permissions like prod.log.

I am using a self written deploy script in a symfony command. During the deployment the prod.log is created also. But not the other two files.

Now my idea is that i generate all log files during deployment. Therefore i need all monolog file paths during deployment.

Is there a symfony mechanism to get ALL monolog log file paths (for e.g. prod-environment)? How can i achieve this? Thanks for your help in advance.

Here is a excerpt from the monolog config file (monolog.yaml):

monolog:
    handlers:
        nested:
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
        deprecation:
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%_deprecations.log"

        error:
            type:  stream
            path:  "%kernel.logs_dir%/%kernel.environment%_errors.log"
            level: error
            max_files: 10
            action_level: error
Stef
  • 98
  • 1
  • 9
  • Now i know a little bit more. The prod.log file is already generated during symfony deployment on server. That's why the user permission is different to other two files (which are created only in case if errors happens when user are visiting the web page and causes an error). So i've got following question. Is there a possibility to create all log files as empty files, which are defined in monolog.yaml during server deployment in a symfony command? – Stef Apr 17 '20 at 14:54
  • Sorry, I use [deployer](https://deployer.org/) for my deployments and fixing permissions, I'm not familiar with symfony cli but I'd think it *can* be done. Since the info in your comment is quite relevant, you should [edit] the question to make it more accurate. – msg Apr 17 '20 at 15:11
  • Thanks for your comment. I've updated my problem description and my question. – Stef Apr 17 '20 at 18:04
  • Wait, you mean a command? I misunderstood, thought you were referring to the `symfony` binary. Although, to be able to write to the directory as another user it either has open permissions or the user doing the deployment is privileged. Can't you run `chmod`? I think we should focus on the script, and not the monolog config. However, symfony should create the log files automatically if they don't exist. Have you run into some problem there? – msg Apr 17 '20 at 18:19
  • I make a `chmod` during the deployment (which is made via a symfony command). That's why prod.log has file rights 770. But the other log files are only generated afterwards when a user is visiting the web page and an error occurs. But then the user permission is then `www-data`. And i don't want this. That's why i search a general way to get ALL monolog log files path during deployment process to generate ALL log files as empty files with user permission `user1`. – Stef Apr 17 '20 at 18:43
  • And yes i have problems in the moment due to the log file rights with e.G. `php bin/console cache:clear` afterwards with the described log file rights. And i can't make `chmod` because `user1` is not root on the server. So that's why i want to generate all log files during deployment. – Stef Apr 17 '20 at 18:54

1 Answers1

0

This is the way i solved it on my own.

namespace App\Service;

// ...

use Symfony\Component\Yaml\Yaml;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;

class LogFilesGenerator
{
    private $params;

    public function __construct(ContainerBagInterface $params)
    {
        $this->params = $params;
    }

    public function createEmptyLogFiles($output)
    {
        $appEnv = $this->params->get('kernel.environment');
        $kernelLogsDir = $this->params->get('kernel.logs_dir');
        $kernelProjectDir = $this->params->get('kernel.project_dir');

        $filePath = realpath($kernelProjectDir.'/config/packages/'.$appEnv.'/monolog.yaml');

        $monologFileContent = Yaml::parseFile($filePath);
        $monologFileHandlers = $monologFileContent['monolog']['handlers'];


        array_walk_recursive($monologFileHandlers, function($value, $key) use ($appEnv, $kernelLogsDir) {
            if($key === 'path') {
                $path = $value;
                $path = str_replace('%kernel.environment%', $appEnv, $path);
                $path = str_replace('%kernel.logs_dir%', $kernelLogsDir, $path);
                // next step create empty files with $path 
                ...
            }
        });
    }
}
Stef
  • 98
  • 1
  • 9