Symfony2 yaml: overwrite configuration

I cannot get the symfony2 configuration to overwrite values ​​from other configuration files correctly. Here is the problem:

I have a new "staging" environment where I want to use most of the material from config_prod.yml, but have a different level of logging (I want it to be what it is in development, just writing everything to a file). Here is the contents of the configuration I am using:

config_prod.yml:

imports: - { resource: config.yml } monolog: handlers: main: type: fingers_crossed action_level: error handler: nested nested: type: stream path: %kernel.logs_dir%/%kernel.environment%.log level: debug 

config_staging.yml:

 imports: - { resource: config_prod.yml } monolog: handlers: main: type: stream path: %kernel.logs_dir%/%kernel.environment%.log level: debug nested: ~ 

From my point of view, the nested logger is now null and the main logs for this file. What really happens is that he records every message twice! The same thing happens when I use this for config_staging.yml:

 imports: - { resource: config_prod.yml } monolog: handlers: main: type: stream path: %kernel.logs_dir%/%kernel.environment%.log level: debug handler: ~ nested: ~ 

I found a workaround by setting the action_level of the main handler to debug and leave everything else as it is, but I don't like this solution. There must be a way to overwrite the configuration files, so I only have the main monologue handler.

+6
source share
3 answers

Many years later, I now have an understanding of what is happening and how to prevent it:

The nested handler is populated with the configuration from config.yml , and when parsing config_staging.yml component does not overwrite the entire hash file and does not set it to null, but it tries to merge both, which leads to the same array as before.

There is a type called null that can be used to overwrite any registrar. It does nothing and therefore is suitable for this use case:

 monolog: handlers: main: type: stream path: %kernel.logs_dir%/%kernel.environment%.log level: debug handler: ~ nested: ~ type: null 

Another solution would be to not configure logging in config.yml, but only in specific environment configurations such as config_prod.yml , etc.

+8
source

Make sure that you do not have duplicate keys in the _staging configuration file - the second will override the first, while the net result will be ignored by the first.

+1
source

If you want to change the collection by deleting the element, you will need to create an intermediate YAML file (import the base), set the collection to "null" and re-add all the necessary elements of the collection to the file, which, in turn, imports the intermediate YAML file.

You cannot just overwrite a collection. New items will be added, but you cannot delete existing ones except the described workaround.

0
source

Source: https://habr.com/ru/post/904386/


All Articles