[英]Not able to call method “error” from the logger object.
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 在上面的代码中,我在“logger.error(e)”行中收到错误。这是错误:方法错误()未定义类型Logger
Rest of the methods like logger.info are working. 其余的方法如logger.info正在运行。
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. SaveOrder是同一个包中的另一个类。
You are using java.util.logging.Logger and this logger has no error()
method. 您正在使用java.util.logging.Logger,并且此记录器没有error()
方法。
Either use 要么使用
Logger.log(Level, String);
Logger.log(Level, String, Throwable);
with one of the levels defined in java.util.logging.Level 使用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. 我建议您阅读Java Logging Overview,并在有关于Java日志记录系统的特定问题时回来。
Another alternative: 另一种选择:
Try this add below import: 请尝试以下添加导入:
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
and remove 并删除
import java.util.logging.Logger; 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.