Retrieve List of Log4J Appenders at Run Time

前端 未结 4 1466
广开言路
广开言路 2021-01-17 11:05

Is it possible to retrieve a list of all appenders configured in log4j at run time?

I\'ll flesh out the scenario a little more. Given the following config how would

4条回答
  •  不知归路
    2021-01-17 11:30

    I want to add something that took me a while to understand. If you look at the figure below (which I copied from here), you can see, that even though the logger com.foo.bar will print to the root loggers FileAppender, its appender list is still null. So you cannot get all appenders the logger will write to with the method logger.getAllAppenders().

    For this you need to iterate through all the parents also and the root logger seperately. Because you cannot iterate logger.getParent() to root logger (root logger returns null - see documentation of getParent()). As far as I know, you have to access the rootLoggers' appenders seperately via Logger.getRootLogger().getAllAppenders().

提交回复
热议问题