简体   繁体   English

无法从记录器对象调用方法“错误”。

[英]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.

相关问题 无法将消息从 Json 字符串转换为对象。 类转换异常 - Not able to convert Message from Json String to Object. ClassCastException logger() slf4j 错误:来自 CoreLogger 类型的方法 logger() 引用了缺少的类型 Logger - logger() slf4j error: The method logger() from the type CoreLogger refers to the missing type Logger 从类创建对象。 保存对象。 从第三类调用对象 - Create object from a class. Save the object. Call the object from a third class 编译错误:Logger类型的方法错误(字符串,对象,对象)不适用于参数 - Compile ERROR: The method error(String, Object, Object) in the type Logger is not applicable for the arguments 多个Servlet请求无法调用JMS对象的方法 - Multiple servlet request not able to call JMS object's method 如何投放清单 <T> 能够在各个对象上调用特定方法? - How to cast List<T> to be able to call specific method on respective Object? 我怎么能在null对象上调用方法? - How am I able to call a method on a null object? 从对象内的对象调用方法?(Java) - Call method from Object within Object?(Java) 使用对象的float属性的compareTo()方法。 如何使用,为什么? - compareTo() method using float attribute of object. How is it used, and why? 方法 java/lang/Object 的定义在哪里。”<init> “:()V?</init> - Where is definition of Method java/lang/Object."<init>":()V?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM