How to specify Log4J 2.x config location?
Asked Answered
M

9

41

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?

Mallorymallow answered 23/5, 2013 at 14:26 Comment(1)
You can use log4j2.component.properties for this purpose. Short explanation hereInculpate
E
39

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
Erebus answered 24/5, 2013 at 16:11 Comment(2)
This is no simple way because Configurator (and others) can be extended (logging.apache.org/log4j/2.0/manual/extending.html)Wifeless
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.Nadaha
S
10

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.

Staphylococcus answered 23/10, 2020 at 13:32 Comment(0)
C
9

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

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

Cesarcesare answered 14/3, 2019 at 11:5 Comment(3)
Is it -Dlog4j2.conf.. or -Dlog4j.conf.. like the other comment says?Laudianism
If you are using log4j2 then my answer will help you.Cesarcesare
Seems both ways do the job for log4j2.Laudianism
E
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
Edgardoedge answered 1/3, 2017 at 21:51 Comment(4)
I believe the system property accepts a normal file path (relative or absolute) as well as a URI.Erebus
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.Asynchronism
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.Attract
File URIs can have two or three slashes; both are syntactically correct but semantically different. Three slashes (equivalent to omitting “localhost” between the second and third slash) is used to access local files. Two slashes (as long as the host is not “localhost”) is used to access files in a remote system. Source : wiki.eclipse.org/File_URI_Slashes_issueHeathcote
O
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);
Olga answered 27/8, 2013 at 8:50 Comment(0)
A
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);
Angulation answered 28/4, 2015 at 3:54 Comment(1)
org.apache.logging.log4j.Logger has no getContext() method.Coacervate
C
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");

 
Catchpenny answered 15/1, 2022 at 20:56 Comment(0)
P
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

Problematic answered 4/2, 2022 at 9:6 Comment(0)
B
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;
    }
Budd answered 8/9, 2022 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.