2

I am doing a web application using Tomcat in eclipse and I kept the log4j2.xml file in environment variable location.

using System environment variable

Variable name : sys_logroot
Variable value : D:\user\gouse

In this directory I have my log4j2.xml file.I am trying to use this log4j2.xml file in my application I am not able to load this xml file and no log files are created to my application.

How can I load an Log4j2.xml file into my application to get logs for my application?

Mohammad Gouse
  • 257
  • 2
  • 13

1 Answers1

1

Apache says (from here):

How do I specify the configuration file location?

By default, Log4j looks for a configuration file named log4j2.xml (not log4j.xml) in the classpath.

You can also specify the full path of the configuration file with this system property: -Dlog4j.configurationFile=path/to/log4j2.xml

i.e. you have to set the system property at runtime to let log4j use it. Something like this:

    String value = System.getenv("sys_logroot");
    value = "file:" + value;
    System.setProperty("log4j.configurationFile", value);

Important is to call this part before using classes logging. Here is my test code:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Main {

  private static final Logger logger = LogManager.getLogger(Main.class.getName());

  public void doSomething() {
    logger.debug("Do something");
  }
}

Tester:

public class Tester {
public static void main(String[] args) {

String value = System.getenv("sys_logroot");
value = "file:" + value;
    System.setProperty("log4j.configurationFile", value);

    new Main().doSomething();
 }
}
alex555
  • 1,586
  • 3
  • 24
  • 42
  • I dont have my log4j2.xml in my class path. it is in other location specified in system environment variable now where i had to specify the path? – Mohammad Gouse Feb 25 '15 at 10:21
  • You ask wrong questions. The correct question are "What is the difference between SysEnv and Java system property?" (see http://stackoverflow.com/a/7054981/1171811) and "How to set a java system property at runtime?" Try to use your own head instead of expecting that someone will do your job for you! – alex555 Feb 25 '15 at 11:33
  • I dont get what you are saying. I mentioned that I stored my log4j2.xml file in the directory which is pointed in the Environment variable 'sys_logroot'. I cant able to load that xml file in my web application. I am trying for about a week I wont able to get the solution. if you any thing about this please share with me. – Mohammad Gouse Feb 25 '15 at 11:57
  • Is there any thing i am missing in log4j2.xml file or any thing else i tried with properties tag. but its not working. so if you the solution please let me know. – Mohammad Gouse Feb 25 '15 at 12:01
  • I edited the question am I correct? if I am its not working for me. No logs created. If not please tell me what i am doing wrong? – Mohammad Gouse Feb 25 '15 at 13:21
  • It will set into System property how would it load into my application to get the logs? – Mohammad Gouse Feb 25 '15 at 13:46
  • Not your app is loading but Log4j uses this configuration file. – alex555 Feb 25 '15 at 13:54
  • its not working when we set system property at runtime no logs are created but when we set system property in eclipse using runas -> run configurations -> vm arguments it working. – Mohammad Gouse Feb 25 '15 at 14:10
  • Thanx Alex but its not working for me. do you have another solution? – Mohammad Gouse Feb 26 '15 at 06:11
  • do you have the `main()` in the same class which logs? Then you have to expand the main method to another class, and set the property before constructing appropriate obejct. It is important to set it before the classloader of your class has been running. I added test code in my answer – alex555 Feb 26 '15 at 13:11