Not able to call method "error" from the logger object.

10.2k views Asked by At
package firstAOP;

import java.util.logging.Logger;

public class OrderDAO
{
    private final static Logger logger = Logger.getLogger(OrderDAO.class.getName()); 
    public boolean saveOrder(Order order)
    {
        boolean flag =false;


        try
        {
           //functional code
            flag = true;
        }
        catch(Exception e)
        {
            logger.error(e);            
        }        
        return flag;
    }
}

In the above code I get an error in the line "logger.error(e)" This is the error: The method error() is undefined for the type Logger

Rest of the methods like logger.info are working.

And if it is not too much to ask can you please tell me have I declared logger correctly. And what will happen if I write:

private final static Logger logger = Logger.getLogger(SaveOrder.class.getName()); 

SaveOrder is another class in the same package.

2

There are 2 answers

0
René Link On BEST ANSWER

You are using java.util.logging.Logger and this logger has no error() method.

Either use

Logger.log(Level, String);
Logger.log(Level, String, Throwable);

with one of the levels defined in java.util.logging.Level

or

Logger.severe(String);

... to your second question:

The logger is declared well.

If you change the logger declaration to

 private final static Logger logger = Logger.getLogger(SaveOrder.class.getName());

then the logger will have another name. The name is used when you configure the logging system. I would suggest you to read the Java Logging Overview and come back when you have a particular question about the java logging system.

1
Raghu On

Another alternative:

Try this add below import:

import org.apache.log4j.Logger;

and remove

import java.util.logging.Logger;

LOG.error(e.getMessage(),e);

Another example :

LOG.error("Error in loading the product data for Mass Request : =  "  + requestId);

example with info level logging:

LOG.info("Loading the product data...for " + requestId);