package com.coreoz.plume.admin.services.logs;

import ch.qos.logback.classic.Level;
import com.coreoz.plume.admin.services.logs.bean.LoggerLevel;

import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;

import javax.inject.Singleton;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Singleton
public class AdminLoggerService {

    private final ConcurrentHashMap<String, String> originalLoggerLevels = new ConcurrentHashMap<>();

    public List<LoggerLevel> getLoggerLevels() {
        return ((LoggerContext) LoggerFactory.getILoggerFactory()).getLoggerList()
            .stream()
            .map(logger -> new LoggerLevel(
                logger.getName(),
                logger.getLevel() != null && !logger.getLevel().equals(Level.OFF) ? logger.getLevel().levelStr : "",
                originalLoggerLevels.getOrDefault(logger.getName(), logger.getLevel() != null ? logger.getLevel().levelStr : "")
            ))
            .sorted(Comparator.comparing(LoggerLevel::getLevel)
                .reversed()
                .thenComparing(LoggerLevel::getName))
            .collect(Collectors.toList());
    }

    public void changeLoggerLevel(String name, String level) {
        Level currentLevel = ((LoggerContext) LoggerFactory.getILoggerFactory()).getLogger(name).getLevel();
        if (!originalLoggerLevels.containsKey(name)) {
            originalLoggerLevels.put(name, currentLevel == null || Level.OFF.equals(currentLevel) ? "" : currentLevel.levelStr);
        }
        ((LoggerContext) LoggerFactory.getILoggerFactory()).getLogger(name).setLevel(Level.toLevel(level));
    }
}
@@ -77,16 +77,54 @@
 .Level;%0A
+import ch.qos.logback.classic.Logger;%0A
 import c
@@ -1337,25 +1337,20 @@
    L
-evel currentLevel
+ogger logger
  = (
@@ -1415,27 +1415,16 @@
 er(name)
-.getLevel()
 ;%0A      
@@ -1515,28 +1515,33 @@
 t(name, 
-curren
+logger.ge
 tLevel
+()
  == null
@@ -1561,28 +1561,33 @@
 .equals(
-curren
+logger.ge
 tLevel
+()
 ) ? %22%22 :
@@ -1591,20 +1591,25 @@
 %22 : 
-curren
+logger.ge
 tLevel
+()
 .lev
@@ -1638,75 +1638,14 @@
     
-((LoggerContext) LoggerFactory.getILoggerFactory()).getLogger(name)
+logger
 .set

Clean up