Naftulin Consulting

Other links:

  • New Job
  • Adjusting logging:

    package readinglist.logging;
    
    import static java.util.stream.Collectors.toList;
    
    import java.util.List;
    
    import org.slf4j.LoggerFactory;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
    import org.springframework.jmx.export.annotation.ManagedOperation;
    import org.springframework.jmx.export.annotation.ManagedResource;
    import org.springframework.stereotype.Component;
    
    import ch.qos.logback.classic.Level;
    import ch.qos.logback.classic.LoggerContext;
    
    @Component
    @ConditionalOnClass(LoggerContext.class)
    @ManagedResource(objectName = "MyApp:name=MyMbean")
    public class LoggerDtoManagerLog4JImpl implements LoggingDtoManager {
    
      @Override
      public List<LoggerDto> getAllLoggers() {
        LoggerContext lc = (LoggerContext)   LoggerFactory.getILoggerFactory();
        List<ch.qos.logback.classic.Logger> loggerList = lc.getLoggerList();
        List<LoggerDto> dtoList = loggerList.stream().map((l) -> new LoggerDto(l.getName(), l.getEffectiveLevel().toString())).collect(toList());
        return dtoList;
      }
    
      @Override
      public void updateLoggers(LoggerDto loggerDto) {
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        List<ch.qos.logback.classic.Logger> loggerList = lc.getLoggerList();
        Level level = Level.toLevel(loggerDto.getLevel());
        loggerList.stream()
    .filter((l) -> l.getName().contains(loggerDto.getName()))
    .forEach((l) -> l.setLevel(level));
    
      }
    
      @ManagedOperation(description = "get log level." )
      public Object getAllLoggersJmX() {
        List<LoggerDto> loggers = getAllLoggers();
        String[] la = new String[loggers.size()];
        for(int i=0; i< la.length; ++i) {
          la[i] = loggers.get(i).toString();
         }
         return la;
       }
    
      @ManagedOperation(description = "get log level." )
      public void updateLoggers(String name, String level) {
        updateLoggers(new LoggerDto(name, level));
      }
    
    }
    

    © Naftulin Consulting 2008-2016 All rights reserved.