40

Is there any way to specify Log4J 2.x log4j2.xml file location manually (like DOMConfigurator in Log4J 1.x), without messing with classpath and system properties?

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
Andrei Petrenko
  • 3,922
  • 3
  • 31
  • 53
  • 4
    You can use *log4j2.component.properties* for this purpose. Short explanation [here](https://stackoverflow.com/a/59190560/443259) – Daud Dec 05 '19 at 09:35

9 Answers9

37

You could use the static method #initialize(String contextName, ClassLoader loader, String configLocation) (see source here) in org.apache.logging.log4j.core.config.Configurator. (You can pass null for the class loader.)

Be aware that this class is not part of the public API so your code may break with any minor release.

For completeness, you can also specify the location of the configuration file with this system property:

-Dlog4j.configurationFile=path/to/log4j2.xml
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • This is no simple way because Configurator (and others) can be extended (http://logging.apache.org/log4j/2.0/manual/extending.html) – mcoolive Feb 18 '15 at 20:08
  • 3
    Where do I call the ```Configurator.initialize() ``` in my project? I am asking it because I am doing it in a static block and Log4j is logging that it does not found the file before my code run. – Daniel T. Sobrosa Oct 19 '16 at 12:17
9

For log4j version 2.12.1, you can find how to reconfigure log4j2 here.

Below is an example

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

File file = new File("C:\\Path for Windows OS\\yourConfig.xml");
    
LoggerContext context = (LoggerContext) LogManager.getContext(false);
context.setConfigLocation(file.toURI());
    
Logger log  = LogManager.getLogger(YourClass.class);

It seems to me that way of configuring log4j2 is changing with new releases, so you should be aware of that.

mgrubovic
  • 91
  • 1
  • 2
8

If you are using log4j2 and properties are in defined in log4j2.properties file then use this.

-Dlog4j2.configurationFile=file:/home/atul/log4j2.properties

Atul
  • 3,043
  • 27
  • 39
7

In Windows, be aware that you need to use a URI with the log4j.configurationFile property

-Dlog4j.configurationFile=file://C:\path\to\log4j2.xml
Glenn McElhoe
  • 177
  • 2
  • 4
  • I believe the system property accepts a normal file path (relative or absolute) as well as a URI. – Remko Popma Mar 02 '17 at 10:49
  • It does not work. I got ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'org.apache.logging.log4j.simplelog.StatusLogger.level' to TRACE to show Log4j2 internal initialization logging. – Juan Acosta Sep 24 '20 at 00:31
  • I used "C:\\Users\\P....\\H............\\workspace\\P...........\\log4j2.properties" as the value for the system property "log4j.configurationFile" and it worked correctly. The double backslashes were needed by Java (internally they were converted to single backslashes). The dots were actually the correct letters in the path. – Peter Schaeffer Jan 03 '22 at 00:09
4

Using the LoggerContext allows to setConfigLocation.

File f = new File(this.logConfigFile);
URI fc = f.toURI();         
System.out.println("Loading logging config file: " + fc);
Logger l = (Logger) LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
l.getContext().setConfigLocation(fc);

or alternatively

LoggerContext.getContext().setConfigLocation(java.net.URI);
Haphil
  • 1,180
  • 1
  • 14
  • 33
Luigi
  • 59
  • 1
1

You can initialize like below as well

ConfigurationSource source = new ConfigurationSource(new FileInputStream(log4j file Path));
XmlConfiguration xmlConfig = new XmlConfiguration(source);
Logger logger = (Logger) LogManager.getLogger(); 
logger.getContext().start(xmlConfig); 

In each class you can get logger instance as below

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

private final Logger logger = LogManager.getLogger(ABC.class);
KalyanM
  • 27
  • 1
  • 6
    [org.apache.logging.log4j.Logger](https://logging.apache.org/log4j/2.0/log4j-api/apidocs/org/apache/logging/log4j/Logger.html) has no getContext() method. – Boris Dec 22 '15 at 10:02
1

Step: 1 - Get ready with your log4J.xml file with the appender details (Mostly under the resource folder)

Step: 2 - Following code should be added to the configuration class (In the previous log4J we had PropertyConfigurator and now we need to go with LoggerContext)

String log4JFilePath = "file path of your log4J.xml file";
LoggerContext loggerContext = (LoggerContext)LoggerManager.getContext(false);
File file = new File(log4JFilePath);
loggerContext.setConfigLocation(file.toURI());

Step: 3 - Add the following line to utilise the logger in any classes

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

logger.info("log here");

 
Pushparaj
  • 92
  • 4
0
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;

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

     Configurator.initialize(null, "src/main/config/log4j2.xml"); //path specify

     Logger logger = LogManager.getLogger(APITestToolMain.class);
     logger.info("working");
    }
}

resource: https://www.baeldung.com/spring-boot-change-log4j2-location

Supun Sandaruwan
  • 1,814
  • 19
  • 19
0
void initializeLogger()
{      
    try
        {
            String basepath=checkpath();
            File f = new File(basepath+"\\config\\log4j2.properties");
            URI fc = f.toURI(); 
            LoggerContext context = (LoggerContext) LogManager.getContext(false);
            context.setConfigLocation(f.toURI());
        }
        catch (Exception e)
        {
            errorlog="Unable to load logging property:";
            System.out.println(errorlog+": "+e.getMessage());
        }   
}

This is the way I initialize my log4j2 properties file from a different location, so what I simply do is to call the initializeLogger() method in my main method.

And it works perfectly.

Perhaps you need to see what the checkpath() blocks looks like, I added the function below.

String checkpath()
    {
        String parentpath="";
        try
        {
           URL url=getClass().getProtectionDomain().getCodeSource().getLocation();
           File f=new File(url.toURI());
           parentpath=f.getParent();
        }
        catch(Exception ex)
        {
            //logger.error("unable to retrieve application parent path");
        }
        return parentpath;
    }