-1

我有一个内部类,我想添加一个记录器,目前它正在通过构造函数添加它。

我的外部类(spring bean)具有以下内容:

private final Logger LOGGER = LoggerFactory.getLogger(getClass());

我的内部课程有以下内容:

private final static class car {

    final Logger LOGGER;

    public car(Logger LOGGER) {
     this.LOGGER = LOGGER;
    }

}

我不喜欢上面的还有其他方法可以解决这个问题吗?

4

3 回答 3

2

你可以这样做:

public class Foo
{
    private final Logger logger = LoggerFactory.getLogger (getClass ());

    private static class Bar
    {
        private final Logger logger = Foo.this.logger;
    }
}

但更好的方法可能是:

public class Foo
{
    private final static Logger logger = LoggerFactory.getLogger (Foo.class);

    private static class Bar
    {
        private final static Logger logger = LoggerFactory.getLogger (Bar.class);
    }
}
于 2013-03-05T10:36:56.240 回答
0
private final static Logger LOGGER = LoggerFactory.getLogger(getClass());

private final static class Car {
   void drive() {
       LOGGER.debug(msg);
   }
}

或者

private final Logger LOGGER = LoggerFactory.getLogger(getClass());

    private final static class Car {
        private final Logger LOGGER = LoggerFactory.getLogger(Car.class);

        void drive() {
            LOGGER.debug(msg);
        }
    }
于 2013-03-05T10:38:29.253 回答
0

如果共享是您的意图,您可以要求提供给外部类的同一个记录器:

private final Logger LOGGER = LoggerFactory.getLogger(OuterClass.class);
于 2013-03-05T10:38:51.320 回答