Logback logs are not working. Log4j logs are not working. Steps to make logging work.

Logs are not working in application even jar is present in application is the very common problem faced by mostly every java developer .Steps to tackle and solve this problem is what i am going to share today.

Mostly problems come as :
1. Logs were working with previous version of server but not working after upgrade.
2. Log4j logs are not working.
3. Logback logs are not working. etc.

Steps to tackle these problems.

Step 1 : First check if respected jars are present in your application.
Jars for log4j : log4j-api.jar,log4j-core.jar
Jars for logback : logback-core.jar,logback-classic.jar,sl4j.jar

Step 2 : Now if logs are not still working then there is possibilty that your server have these jars with some other implementation.
For eg : Weblogic server have sl4j.jar with jcl logging. So inspite of having jar in your application logback logs will not work.
To solve this problem you can easily find out on google that how to reference application jar in weblogic or how to reference application jar in websphere etc.

If the problem still persist then you need to go to step-3;

Step 3 : Now the problem could be that jar are getting picked up from application but some other jar is also having the same package or server is still picking jars from it’s own directory. To find out that which jar server is picking up use below code in one class and try to call that method somehow to execute below code.

try{
System.out.println(“printing class paths–>”+Logger.class.getProtectionDomain().getCodeSource().getLocation().toURI().toString());
System.out.println(“printing class paths–>”+LoggerFactory.class.getProtectionDomain().getCodeSource().getLocation().toURI().toString());
System.out.println(“printing class paths–>”+StaticLoggerBinder.class.getProtectionDomain().getCodeSource().getLocation().toURI().toString());
}
catch(Exception e){
System.out.println(“Error in printing class paths”);
e.printStackTrace();
}

Above code is for logback logs and the classes used in this code are classes used for logback logging so you can change according to your logging. What this code is doing that it is printing location of a particular class that from which jar and from which location this class is getting picked up at runtime.

After this you will get your answer that which jar you need to concentrate oor remove from your application to make your logging work.

Please add comments and share if you find it useful or point out mistakes if you find any.

Some other useful post are :

Set datasource spring bean properties dynamically if server is not available
MQ connection not closed and giving MQ Error-2009

Advertisements