简体   繁体   English

创建记录器Java时未调用构造函数

[英]Constructor not being called when creating logger java

I was implementing my Logger class but for a strange reason the constructor method is never called. 我正在实现Logger类,但是出于一个奇怪的原因,从未调用过构造方法。 In other class when I use SRCLogger.getLogger().log(Level.INFO, "Message"); 在其他类中,当我使用SRCLogger.getLogger().log(Level.INFO, "Message"); there's no log-file created on the path. 路径上没有创建日志文件。

What am I missing? 我想念什么? What's wrong in this code? 这段代码有什么问题?

Here's my code: 这是我的代码:

public final class SRCLogger implements Serializable{

    private static final Logger l = Logger.getLogger("mySRCLogger");
    private FileHandler fh;

    private String ROOT_DIR = "C:\\Users\\Test\\Desktop\\";

    public SRCLogger(){
        System.out.println("Constructor.");
        try {
            fh = new FileHandler(ROOT_DIR + "SRCLog.log");
            fh.setFormatter(new SimpleFormatter());
            l.addHandler(fh);
            System.out.println("Try.");
        } catch (IOException ex) {
            Logger.getLogger(SRCLogger.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Catch IOException.");
        } catch (SecurityException ex) {
            Logger.getLogger(SRCLogger.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Catch SecurityException.");
        }
    }

    public static Logger getLogger(){
        return l;
    }   
}

Cheers and thanks in advance 预先加油和感谢

Quite simply - you're never calling the constructor. 很简单-您永远不会调用构造函数。 You're calling this: 您正在呼叫此:

public static Logger getLogger(){
    return l;
}

... which returns l , a static field initialized like this: 返回l的静态字段,其初始化如下:

private static final Logger l = Logger.getLogger("mySRCLogger");

Why would you expect that to trigger your class to be instantiated? 您为什么期望那样触发您的类被实例化? The fact that you don't have any instance methods is also a design smell - what are you expecting the class to accomplish, other than adding a handler to an existing logger? 您没有任何实例方法的事实也是一种设计味道-除了向现有记录器添加处理程序之外,您希望类完成什么工作? If that's all you want to do, I would change your class to just have a static method - you don't really need any instances as far as I can tell. 如果这就是您想要做的,我将更改您的类,使其仅具有静态方法-就我所知,您实际上不需要任何实例。

In order for the constructor to be called, the object has to be instantiated using the "new" keyword. 为了调用构造函数,必须使用“ new”关键字实例化该对象。 If you call only the static methods of the class (not the object), the constructor will not be called. 如果仅调用类的静态方法(而不是对象),则不会调用构造函数。

So 所以

   SRCLogger.getLogger() 

is a static method being called. 是被调用的静态方法。

If you do something like 如果你做类似的事情

   new SRCLogger().getLogger()

, then the constructor will be called, but it is not good practice to call a static method on an object instance. ,则将调用构造函数,但是在对象实例上调用静态方法不是一种好习惯。 Static methods ought to be called on the class itself. 静态方法应该在类本身上调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM