Search code examples
phpsymfonyloggingmonolog

Getting all monolog file paths during deployment in symfony 4


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

Solution

  • 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 
                    ...
                }
            });
        }
    }