views:

1019

answers:

2

Hi : I have used log4j for looging error log using FileAppender. The problem is its logging the same error two times in a log file when the below situation

Case1:

Class1 :

public void func(){
    try{
        new Class2.prop()
    }catch(IOException ioe){
        logger.log(2,ioe);
    }
}

Class2 :

public void prop(){
    try{
        //error oocurs here
    }catch(FileNotFoundException fe){
        logger.log(2,fe);
    }
}

Error :
    Class2 .FileNotFoundException 
    at Class2.prop(Class2.java:3)
    at Class1.func(Class1.java:4)

Log File :

    FileNotFound exception
    FileNotFound exception

But its logging the error one time for the below case.

Case2 :

Class1 :

public void func(){
    try{
        new Class2.prop()
        //error oocurs here
    }catch(IOException ioe){
        logger.log(2,ioe);
    }
}

Class2 :
    public void prop(){
        try{
        }catch(FileNotFoundException fe){
            logger.log(2,fe);
        }
    }

Error :
    Class2 .IOException 
    at Class1.func(Class1.java:4)

Log File :
    IOException exception

Help me what should i do to log the error only one time in a log file whereever it is.

+3  A: 

But its logging the error one time for the below case.

That's because you're handling the exception:

Class1:

public void func() {
    try{
        new Class2.prop()
    }catch(IOException ioe){
        logger.log(2,ioe);            
    }
}

Class2 :

public void prop() throws IOException {
    try{
        //error oocurs here
    }catch(FileNotFoundException fe){
        logger.log(2,fe);
        throw fe;
    }
    // Here!!!!!
}

In your catch block of the class2 ( after your //error oocurs here ) you log the exception, that's what you've got on your logs.

But since you are just logging the exception, you're telling the program that the exception has been controlled somehow, or handled ( which is more appropriate) and the program continues it's flow to the line where I added the comment // Here!!!!!

Later in class1, since the exception was handled, nothing happens in your try/catch block and your second exception is never logged ( as you expected ) because it never happened.

If you want to see two logs ( which I think it's unnecessary ) you should re-throw the exception as I did in your class2 and plus, modify the method signature to mark indicate it throws an IOException.

That way you will have two logs.

Better will be like this:

Class1:

public void func() {
    try{
        new Class2.prop()
    }catch(IOException ioe){
        logger.log(2,ioe);            
    }
}

Class2 :

public void prop() throws IOException {
        //error oocurs here
}

In class 2 you don't handle the exception, you just let it go through the caller. In the stacktrace you'll have the information anyway.

I hope this helps.

OscarRyz
See the case1 . First it logs error when error occurs in class2. After whecontrol goes to class1 its again logging the same log again. So totally two times log has been logged. I don want to log same error twice. Tell me howto resolve the case1 n dont have problem in case2.
raja
Oscar has given you exactly the right answer: don't log in prop. Only do it in func(). Let the exception just propagate from prop() to func().
Jon Skeet
A: 

In your log4j configuration, do you have the logger used in Class 2 being sent to your appender twice?

Stephen Denne