简体   繁体   中英

how to log exception while loading log4j.xml

We have a Java program that gets packaged as a runnable jar. The logging is via slf4j with log4j. We have placed log4j.xml outside the runnable jar - onto the file system. As soon as the user clicks on the runnable jar, the main method is invoked which calls the function to load the log4j.xml file. My question is what if the loading of log4j.xml itself fails. How can that exception be logged? Logger is not yet initialized and sys.out to my knowledge will not be useful in that case. Here is the piece of code which gets invoked right after main method is invoked:

/* This method loads the log4j.xml file from local directory */
public static void loadLog4jXml(){
        DOMConfigurator.configure( "C:\MyFirstJavaExecJar\log4j.xml" );
    }

I tried googling it, but could not find any pointers whatsoever. Also tried going through the FAQ list on apache website: http://logging.apache.org/log4j/2.x/faq.html

Any help is appreciated!

Why it is not not working just capsulate the code with try catch blog and log it to Console or a file, because it is not a thing to be happened frequently.

/* This method loads the log4j.xml file from local directory */
public static void loadLog4jXml(){
        try{
             DOMConfigurator.configure( "C:\MyFirstJavaExecJar\log4j.xml");
        }
        catch(Exception e){
          System.out.println("There is a problem about log4J file loading");
        }
    }

Ok first you encapsulate your code when you loading the log4j file with a try catch and if you run it in windows console, console is the dos screen at here, so if there is a problem at windows when you run it via java -jar yourJar.jar in that case it writes messages to your dos screen. Or you can give a file path to this jar and write your log to this file. Or you can use event logging for more sophisticated solution, Please check this discussion for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM