Dynamically selecting log4j2 configuration in Mule - mule

I have gone through Mule Logging documentation but not clear on how to dynamically load different logging configuration files for each environments. Basically I want to control log verbosity and sync/async feature across environments so looking for similar feature of dynamically selecting property file based on server environment property variable.

2 ways you can load the log4j2 file dynamically in your application either from an external path or from your application classpath:-
setting the log4j2 file path in your application's mule-deploy.properties like :-
log.configFile=E:\common-log4j2.xml
Loading the log4j2.xml in your application programmatically by reconfiguring the log manager via Spring and load our own log4j2.xml file from your defined path:-
ref:- https://dzone.com/articles/getting-own-log4j2-file-for-mule-via-spring

I haven't tried it, but you should be able to set the log4j config file at the command line when launching Mule, using the log4j.configuration system property.
For example (in Windows) by adding -Dlog4j.configuration=c:\some-path\log4j-%MULE-ENV%.xml if your env variable is called MULE-ENV.
Note that there are several places this can be set - directly on the command line if using Mule standalone (in which case I believe you need -M-Dlog4j.configuration=...), in the wrapper.conf file if using standalone, or in the VM params section of the Arguments tab in Run Configurations when running in Studio.

You can have a bean in your application which can call the method to set the configuration. You can pass the environment name as an argument to this bean, it'll pick the configuration file associated with that environment. You can call the method using the invoke component and have this flow executed at the startup.
Till the flow is executed, default logging configuration can be used.

Related

What is it called when an app set some configurations at build time?

A common pattern used to build applications/software (web, mobile, desktop) is to have multiple build configs like Dev, Stage and Production. Each one of these configs may overwrite multiple variables like the base url of a web service that is consumed by the application. These configs may be stored in multiple ways (.env file, a JSON config file, etc).
I was looking for a name for this pattern/practice and how to implement it on a VB.NET application. Most close thing I was able to found is using the app.config file to store app settings but this is missing the multiple environments part. I'm having some trouble looking for solutions since I don't know the proper term for this practice.
There's a Solution Configurations drop-down on the main toolbar. You would have already used it to select Debug or Release. You can open the Configuration Manager from there to create new build configurations.
You can then control what happens during the build process of a project for a particular configuration on the Build page of the project properties. You can also perform actions using build events, which you configure on the Build Events page of the project properties. Here's an example of a pre-build event commandline that I use to automatically select the appropriate config file for NLog:
IF EXIST "$(ProjectDir)nlog.$(ConfigurationName).config" XCOPY "$(ProjectDir)nlog.$(ConfigurationName).config" "$(ProjectDir)nlog.config" /Y

Where are IDEA run/debug configuration *defaults* stored?

It's easy to share run configurations instances in IDEA - simply instantiate a configuration and check "Share":
I'm already version controlling the resulting files in .idea/runConfigurations (in the relevant project) and part of ~/.IntelliJIdea* (for puppetising desktops). However, I can't find where IDEA stores the configuration defaults - it doesn't seem to be in either of these places. They must obviously be persisting it somewhere, because it works across restarts. The official documentation is unusually unhelpful in this case:
This check box is not available when editing the run/debug configuration defaults.
The particular use case is that I'd like all future "Behave" configurations to have the environment variable DISPLAY set to :1 to run browser tests in VNC rather than in the foreground.
Defaults (the ones that you configure under Defaults node from your screenshot) are per-project .. and therefore stored together with other non-shared configs in .idea/workspace.xml (which is not supposed to be stored under VCS as it contains developer/computer specific settings).
You can find such entries in the aforementioned file under <component name="RunManager" node. Default entries will have default="true" attribute.
There is no defaults of defaults for run/debug configs that you can edit/provision (configs that would be applied to any new projects). They are not stored in separate config file(s) on IDE level but initiated directly from plugin code .

One properties configuration file for Multi Apps on Mule ESB runtime

Today I have the same properties configurated on mule-app.properties file for each Apps. Like this:
All Apps (+ that 5) are running on the same Mule ESB runtime (CE).
For example, when It is necessary to change a host or password, I need to change the properties file of the all apps.
I would like to have just one properties file on Mule ESB runtime shared by all apps.
Is there a form to make this?
Thanks!
You can create a new Mule project and call it "shared". Now, in this project, you can create a file src/main/resources/config.properties and there put all the settings you need.
To have these settings in your current project, just add the project "shared" in the "Projects" tab of the "Java Build Path". Then in the main file of your current project, write
< context: property-placeholder location="config.properties" />
Use a Spring Property Placeholder and read a single file from the filesystem:
<context:property-placeholder location="file:/path/to/myConfigFile.properties" />
https://docs.mulesoft.com/mule-user-guide/v/3.6/configuring-properties#properties-files

Accessing mule-app.properties in custom properties file located in src/main/resources on cloudhub

I have a mule application built using mule apikit. The mule-app.properties file contains below property:
orig.db.url=jdbc:db2://mmrs001.nmd.net:1004/dudu:user=abc43;password=xxxx;
The custom properties file src/main/resources/dev.properties contains below property:
db.url=${orig.db.url}
And, the mule configuration xml file contains below property-placeholder:
<context:property-placeholder location="dev.properties"/>
Now, when I deploy it locally everything works fine and I am able to run the flow. But, at the same time when I deploy on Cloudhub it gives me below exception:
Invalid bean definition with name 'get:/total_amount:total_amount-db-config' defined in null: Could not resolve placeholder 'orig.db.url' in string value "jdbc:db2://mmrs001.nmd.net:1004/dudu:user=abc43;password=xxxx;"
I am unable to understand what am I missing here?
How did you deploy to CloudHub? If you used the deployment tool from Anypoint Studio you have an option to set properties before deployment.
The mule-app.properties will get added to system properties instead of classpath properties. We can't refer to system properties from property files added to classpath. We need to move these properties to classpath to resolve these in runtime.
One way to override this behaviour by importing mule-app.properties using:
<context:property-placeholder location="mule-app.properties,dev.properties”/>
It tells spring to load properties in respective order. So, first mule-app.properties gets loaded and then dev.properties.
But, its not the best practice to include the mule-app.properties file in the classpath. Best way is to remove the dev properties from mule-app.properties and put it inside dev.properties.
By default when deploying to cloudhub all properties from the mule-app.properties should appear in the properties tab. Did you not see them when you deployed the first time?

Mule Deployment properties file

I have seen some strange behaviour in Mule Application. I created a sample mule project(created flows) and opened mule-deploy.properties file "config.resources" value is empty.
But the same above mentioned key value is getting updated with "configuration xml file name" when i run my mule project.
Is this the behaviour or any updates need to be done.
Can some clarify?
Regards
Vikram
There are some known issues/features around mule-deploy.properties and Mule Studio.
Currently it is expected that when running your project from Mule Studio that mule-deploy.properties-config.resources is automatically populated with all your configuration files.
There are some open issues on this here: https://www.mulesoft.org/jira/browse/STUDIO-3297 that you can vote on.
Once you run the mule application it will automatically updated with the configuration file by default if you have only one main configuration file.But If you are using more configuration files in your application you should manually add all the filenames to the config.properties key separated by comma before run the application.