如何检查Logback中是否存在特定的记录器

时间:2015-07-17 14:52:42

标签: logging logback

基本上我需要检查是否存在特定的记录器,以便我可以更改其级别。像下面的伪代码:

Logger myLogger =  (Logger) LoggerFactory.getLogger( "com.logger.to.Change");

if ( validateThatLoggerExist (myLOgger))  // check logger effectively exist
   myLogger.setLeve(Level.NEW_LEVEL)     // Change logger log level
else
   throw new LoggerNotFoundExeption()   // do somethig

1 个答案:

答案 0 :(得分:2)

    String loggerName = "com.logger.to.Change";
    LoggerContext loggerContext = (LoggerContext) LoggerFactory
                    .getILoggerFactory();
    Logger logger = loggerContext.exists(loggerName); //returns Logger if exists else returns null

    if(logger != null){
        logger.setLeveL(Level.NEW_LEVEL)
    }else{
        throw new LoggerNotFoundExeption()   // do something
    }