How to set the log4j.configuration system variable in WebSphere 7?

I have a Java EE 5 web application that I am deploying to WebSphere 7 as an EAR file.

I want my log4j configuration to be external to the EAR file, so I can customize the log contents when necessary, without having to rebuild and redeploy the EAR file.

As far as I understand, I can specify the location of my log4j.properties file by setting a "system variable" called log4j.configuration. (e.g. log4j.configuration = c: /log4j.properties)

My question is: how to set this system variable in the WebSphere 7 admin console?

Looking through, I see that there is Environment > WebSphere Variables , but it does not look right, because it will be setting a variable for the entire server. I assume that I want to set the system variable only for my application EAR file.

Any help or suggestions are welcome!

Rob

+8
source share
3 answers

The log4j.configuration property is a system property of the Java virtual machine. You can load this property by adding it to the end of the list of application servers of the general JVM arguments. This is done in the WebSphere Console by navigating to the following:

Servers > Application servers > [app server name] > Process definition > Java Virtual Machine

In the JVM General Arguments section, add the following:

-Dlog4j.configuration=file:C:/log4j.properties

Click Apply at the bottom of this page and save the changes. This will require a restart of the application server.

+11
source

You can also use the shared library for the application and put your log4j.xml file there.

+1
source

I find a solution to assign an external log4j.xml configuration file to each EAR or WAR:

  1. I extracted log4j.xml from our EAR, packed this lonely XML into a JAR file. Place it on the server in a path that is accessible to WebSphere,
  2. Create a shared library (WebSphere> Environment> Shared Libraries) by mapping the class path to this archive. Make sure you select "Use an isolated classloader for this shared library", otherwise you will need to assign a link for each module of your application, not just the parent EAR.
  3. Link assignment in our EAR (Application → ApplicationType → WebSphere enterprise applications → EAR NAME → Shared library links → SELECT YOAR EAR → Shared library links → SELECT the LOG4J CONFIGURATION JAR) and ADD IT.
  4. Restart application

I used this method to assign a specific external log4j configuration file to each WAR in the EAR. Here you can find an original solution.

0
source

All Articles