如何在静态上下文中获取类的记录器?

sto*_*ack 6 java logging

我正在尝试接收我班级的记录器:

public static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(this);
Run Code Online (Sandbox Code Playgroud)

但是使用"this"会导致"无法在静态上下文中使用它"错误.

有人知道怎么修这个东西吗?

编辑:我必须能够从我的程序中的所有类访问记录器,因此它必须是公共的.

Tom*_*icz 10

注意我将修改器public改为private:

public class FooBar {

    private static final Logger log = Logger.getLogger(FooBar.class);

    //or (if you are using java.util.logging):

    private static final Logger log = Logger.getLogger(FooBar.class.getName());


}
Run Code Online (Sandbox Code Playgroud)


Ale*_*ien 6

对于org.appache.log4j:

private static final Logger LOG = Logger.getLogger(MyClass.class);
Run Code Online (Sandbox Code Playgroud)

对于java.util.Logging

private static final Logger LOG = Logger.getLogger(MyClass.class.getName());
Run Code Online (Sandbox Code Playgroud)