You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by WebServices Development <We...@csx.com> on 2013/09/11 13:21:49 UTC

Defining different log4j2.xml configurations based on environment

I had posted this on SO and was advised that it may be better to post this question on this list.

I want to try and use logj2 in my web application. With log4j 1.x, we set-up different configurations files based on environment and use ServletContextListener to load the appropriate configuration using a call like

DOMConfigurator.configureAndWatch(logConfigFile, delay);

by passing the config file location.

I was planning on doing the same for log4j2 config file but according to this<http://stackoverflow.com/questions/16716556/how-to-specify-log4j-2-x-config-location> (http://stackoverflow.com/questions/16716556/how-to-specify-log4j-2-x-config-location )  and this<http://stackoverflow.com/questions/16778986/log4j-only-can-view-error-level-on-my-java-enterprise-application-module-ejb-wi> (http://stackoverflow.com/questions/16778986/log4j-only-can-view-error-level-on-my-java-enterprise-application-module-ejb-wi ), it is not possible or advisable to use DOMConfigurator with log4j2.

So, my question is: How can I use different configuration files based on environment with log4j2?

I am not trying to have separate logfiles for each web application within an EAR, but a separate config file for each of the environments, so that, for example, I can have DEBUG in dev and UAT, but not in PROD or can have large file sizes in dev and uat, but not in prod. Like I mentioned in the question, what we do with version 1 is that we have, say, log4j-dev.xml, log4j-prod.xml etc. and have a servlet context listener and an env variable such that without making code changes, when I deploy the app to an env, it picks up correct config.

Thank you






-----------------------------------------
This email transmission and any accompanying attachments may
contain CSX privileged and confidential information intended only
for the use of the intended addressee.  Any dissemination,
distribution, copying or action taken in reliance on the contents
of this email by anyone other than the intended recipient is
strictly prohibited.  If you have received this email in error
please immediately delete it and  notify sender at the above CSX
email address.  Sender and CSX accept no liability for any damage
caused directly or indirectly by receipt of this email.

Re: Defining different log4j2.xml configurations based on environment

Posted by Ralph Goers <ra...@dslextreme.com>.
Yes, Log4j2 has an environment Lookup to access OS environment variables.

Ralph

On Sep 11, 2013, at 11:21 AM, WebServices Development wrote:

> Thank you, I shall check it out.
> 
> If instead of System variable ( using a -Dxxx on a VM ), if I want to use the Environment variable, can I use env in a similar manner. I.e. is it valid to say   <param-value>file://etc/myApp/mylogging-${env:region}.xml</param-value>?
> 
> 
> -----Original Message-----
> From: Ralph Goers [mailto:ralph.goers@dslextreme.com]
> Sent: Wednesday, September 11, 2013 11:26 AM
> To: Log4J Users List
> Subject: Re: Defining different log4j2.xml configurations based on environment
> 
> Have you looked at http://logging.apache.org/log4j/2.x/manual/webapp.html?  The documentation there is missing one thing that will probably help you.  You can specify
> 
> <context-param>
>  <param-name>log4jConfiguration</param-name>
>  <param-value>file://etc/myApp/mylogging-${sys:region}.xml</param-value>
> </context-param>
> 
> If you then set the region system property to "prod" you would use mylogging-prod.xml.
> 
> Ralph
> 
> 
> On Sep 11, 2013, at 4:21 AM, WebServices Development wrote:
> 
>> I had posted this on SO and was advised that it may be better to post this question on this list.
>> 
>> I want to try and use logj2 in my web application. With log4j 1.x, we
>> set-up different configurations files based on environment and use
>> ServletContextListener to load the appropriate configuration using a
>> call like
>> 
>> DOMConfigurator.configureAndWatch(logConfigFile, delay);
>> 
>> by passing the config file location.
>> 
>> I was planning on doing the same for log4j2 config file but according to this<http://stackoverflow.com/questions/16716556/how-to-specify-log4j-2-x-config-location> (http://stackoverflow.com/questions/16716556/how-to-specify-log4j-2-x-config-location )  and this<http://stackoverflow.com/questions/16778986/log4j-only-can-view-error-level-on-my-java-enterprise-application-module-ejb-wi> (http://stackoverflow.com/questions/16778986/log4j-only-can-view-error-level-on-my-java-enterprise-application-module-ejb-wi ), it is not possible or advisable to use DOMConfigurator with log4j2.
>> 
>> So, my question is: How can I use different configuration files based on environment with log4j2?
>> 
>> I am not trying to have separate logfiles for each web application within an EAR, but a separate config file for each of the environments, so that, for example, I can have DEBUG in dev and UAT, but not in PROD or can have large file sizes in dev and uat, but not in prod. Like I mentioned in the question, what we do with version 1 is that we have, say, log4j-dev.xml, log4j-prod.xml etc. and have a servlet context listener and an env variable such that without making code changes, when I deploy the app to an env, it picks up correct config.
>> 
>> Thank you
> 
> 
> 
> 
> 
> 
> -----------------------------------------
> This email transmission and any accompanying attachments may
> contain CSX privileged and confidential information intended only
> for the use of the intended addressee.  Any dissemination,
> distribution, copying or action taken in reliance on the contents
> of this email by anyone other than the intended recipient is
> strictly prohibited.  If you have received this email in error
> please immediately delete it and  notify sender at the above CSX
> email address.  Sender and CSX accept no liability for any damage
> caused directly or indirectly by receipt of this email.


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


RE: Defining different log4j2.xml configurations based on environment

Posted by WebServices Development <We...@csx.com>.
Thank you, I shall check it out.

If instead of System variable ( using a -Dxxx on a VM ), if I want to use the Environment variable, can I use env in a similar manner. I.e. is it valid to say   <param-value>file://etc/myApp/mylogging-${env:region}.xml</param-value>?


-----Original Message-----
From: Ralph Goers [mailto:ralph.goers@dslextreme.com]
Sent: Wednesday, September 11, 2013 11:26 AM
To: Log4J Users List
Subject: Re: Defining different log4j2.xml configurations based on environment

Have you looked at http://logging.apache.org/log4j/2.x/manual/webapp.html?  The documentation there is missing one thing that will probably help you.  You can specify

<context-param>
  <param-name>log4jConfiguration</param-name>
  <param-value>file://etc/myApp/mylogging-${sys:region}.xml</param-value>
</context-param>

If you then set the region system property to "prod" you would use mylogging-prod.xml.

Ralph


On Sep 11, 2013, at 4:21 AM, WebServices Development wrote:

> I had posted this on SO and was advised that it may be better to post this question on this list.
>
> I want to try and use logj2 in my web application. With log4j 1.x, we
> set-up different configurations files based on environment and use
> ServletContextListener to load the appropriate configuration using a
> call like
>
> DOMConfigurator.configureAndWatch(logConfigFile, delay);
>
> by passing the config file location.
>
> I was planning on doing the same for log4j2 config file but according to this<http://stackoverflow.com/questions/16716556/how-to-specify-log4j-2-x-config-location> (http://stackoverflow.com/questions/16716556/how-to-specify-log4j-2-x-config-location )  and this<http://stackoverflow.com/questions/16778986/log4j-only-can-view-error-level-on-my-java-enterprise-application-module-ejb-wi> (http://stackoverflow.com/questions/16778986/log4j-only-can-view-error-level-on-my-java-enterprise-application-module-ejb-wi ), it is not possible or advisable to use DOMConfigurator with log4j2.
>
> So, my question is: How can I use different configuration files based on environment with log4j2?
>
> I am not trying to have separate logfiles for each web application within an EAR, but a separate config file for each of the environments, so that, for example, I can have DEBUG in dev and UAT, but not in PROD or can have large file sizes in dev and uat, but not in prod. Like I mentioned in the question, what we do with version 1 is that we have, say, log4j-dev.xml, log4j-prod.xml etc. and have a servlet context listener and an env variable such that without making code changes, when I deploy the app to an env, it picks up correct config.
>
> Thank you






-----------------------------------------
This email transmission and any accompanying attachments may
contain CSX privileged and confidential information intended only
for the use of the intended addressee.  Any dissemination,
distribution, copying or action taken in reliance on the contents
of this email by anyone other than the intended recipient is
strictly prohibited.  If you have received this email in error
please immediately delete it and  notify sender at the above CSX
email address.  Sender and CSX accept no liability for any damage
caused directly or indirectly by receipt of this email.

Re: Defining different log4j2.xml configurations based on environment

Posted by Ralph Goers <ra...@dslextreme.com>.
Have you looked at http://logging.apache.org/log4j/2.x/manual/webapp.html?  The documentation there is missing one thing that will probably help you.  You can specify

<context-param>
  <param-name>log4jConfiguration</param-name>
  <param-value>file://etc/myApp/mylogging-${sys:region}.xml</param-value>
</context-param>

If you then set the region system property to "prod" you would use mylogging-prod.xml.

Ralph


On Sep 11, 2013, at 4:21 AM, WebServices Development wrote:

> I had posted this on SO and was advised that it may be better to post this question on this list.
> 
> I want to try and use logj2 in my web application. With log4j 1.x, we set-up different configurations files based on environment and use ServletContextListener to load the appropriate configuration using a call like
> 
> DOMConfigurator.configureAndWatch(logConfigFile, delay);
> 
> by passing the config file location.
> 
> I was planning on doing the same for log4j2 config file but according to this<http://stackoverflow.com/questions/16716556/how-to-specify-log4j-2-x-config-location> (http://stackoverflow.com/questions/16716556/how-to-specify-log4j-2-x-config-location )  and this<http://stackoverflow.com/questions/16778986/log4j-only-can-view-error-level-on-my-java-enterprise-application-module-ejb-wi> (http://stackoverflow.com/questions/16778986/log4j-only-can-view-error-level-on-my-java-enterprise-application-module-ejb-wi ), it is not possible or advisable to use DOMConfigurator with log4j2.
> 
> So, my question is: How can I use different configuration files based on environment with log4j2?
> 
> I am not trying to have separate logfiles for each web application within an EAR, but a separate config file for each of the environments, so that, for example, I can have DEBUG in dev and UAT, but not in PROD or can have large file sizes in dev and uat, but not in prod. Like I mentioned in the question, what we do with version 1 is that we have, say, log4j-dev.xml, log4j-prod.xml etc. and have a servlet context listener and an env variable such that without making code changes, when I deploy the app to an env, it picks up correct config.
> 
> Thank you
> 
> 
> 
> 
> 
> 
> -----------------------------------------
> This email transmission and any accompanying attachments may
> contain CSX privileged and confidential information intended only
> for the use of the intended addressee.  Any dissemination,
> distribution, copying or action taken in reliance on the contents
> of this email by anyone other than the intended recipient is
> strictly prohibited.  If you have received this email in error
> please immediately delete it and  notify sender at the above CSX
> email address.  Sender and CSX accept no liability for any damage
> caused directly or indirectly by receipt of this email.