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

Set datasource spring bean properties dynamically if server is not available

Hi Readers today i want to share with you guys something that i faced recently.

Problem :

Recently i was developing a spring application in which container need to initialized by public static void main method rather then any server .
We want user to enter details with swing ui and do not want to save them in properties file at run time. Risk in this is, that anybody can look into properties file which is a security threat.
So now the question is that how we will tell spring container to which DataBase it needs to connect with.

Solution :

So to solve this problem, I created a class which extends BasicDatasource as code given below.


public class CustomDatasource extends BasicDatasource {

private static String hostName;
private static String dbName;
private static String port;
private static String userName;
private static String dbPassword;


private CustomDatasource(){
setUrl("jdbc:oracle:thin:@"+hostName+":"+port+":"+dbName);
setDriverClassName("oracle.jdbc.OracleDriver");
setUsername(userName);
setPassword(dbPassword);
}


public static String getHostName() {
return hostName;
}

public static void setHostName(String hostName) {
CustomDatasource.hostName = hostName;
}

public static String getDbName() {
return dbName;
}

public static void setDbName(String dbName) {
CustomDatasource.dbName = dbName;
}

public static String getPort() {
return port;
}

public static void setPort(String port) {
CustomDatasource.port = port;
}

public static String getUserName() {
return userName;
}

public static void setUserName(String userName) {
CustomDatasource.userName = userName;
}

public static String getDbPassword() {
return dbPassword;
}

public static void setDbPassword(String dbPassword) {
CustomDatasource.dbPassword = dbPassword;
}

Now make this class as datasource bean in Beans.xml and inject this bean into persistanceUnitManager .
Now initialize spring container as given below.


public class Test {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("Enter a string");
String dbName = in.nextLine();
CustomDatasource.setDbName(dbName);
String hostName = in.nextLine();
CustomDatasource.setHostName(hostName);
String port = in.nextLine();
CustomDatasource.setPort(port);
String userName = in.nextLine();
CustomDatasource.setUserName(userName);
String dbPassword = in.nextLine();
CustomDatasource.setDbPassword(dbPassword);
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/Beans.xml");
}
}

Now without saving database configuration in any properties file your application will be able to connect with database.

Other related posts are :
HTML to PDF conversion in JAVA | HTML to PDF example in JAVA
Top 10 spring interview questions and their answers. Spring basic concepts.
MQ connection not closed and giving MQ Error-2009
JPA Hibernate Sequence generator generating odd ID value and IntegrityConstraintVolation exception is coming

Please share this posts on facebook,Linkeedin,Google+,Twitter by clicking on below icons.

MQ connection not closed and giving MQ Error-2009

Problem :

Sometimes MQ connection does not close even after calling close method. It gives us MQ Error code-2009. I found out solution for this problem after a long struggle and hit & try, so i thought to share it with everyone.

Solution :

What most people do the mistake is that they close queue connection before queue manager connection. When you close queue connection first then it made queue manager connection unavailable and open inspite of firing disconnect method on queue manager connection after queue close. IBM MQ close these open connections self but when you create connection very frequently before MQ close them and eventually it gives you 2009 error. So always close queue manager before queue connection.

Other related posts are :
Top 10 spring interview questions and their answers. Spring basic concepts.
JPA Hibernate Sequence generator generating odd ID value and IntegrityConstraintVolation exception is coming
Set datasource spring bean properties dynamically if server is not available

Please share these posts on facebook,Linkeedin,Google+,Twitter by clicking on below icons.