LoggerController.java
package org.genesys.server.mvc.admin;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/admin/logger")
@PreAuthorize("hasRole('ADMINISTRATOR')")
public class LoggerController {
private int defaultPageSize = 100;
@RequestMapping(method = RequestMethod.GET, value = "/")
public String adjustLogger(Model model, @RequestParam(value = "page", defaultValue = "1") int page) {
// first element on page
int firstelement = (page - 1) * defaultPageSize;
List<Logger> allLoggers = getAllLoggers();
List<Logger> loggers = new ArrayList<>();
// copy part of array
for (int i = 0; i < defaultPageSize && i < allLoggers.size(); i++) {
loggers.add(allLoggers.get(firstelement + i));
}
PageImpl<Logger> pagedData = new PageImpl<Logger>(loggers, PageRequest.of(page - 1, defaultPageSize), allLoggers.size());
model.addAttribute("loggers", pagedData);
return "/admin/logger/index";
}
@RequestMapping(method = RequestMethod.GET, value = "/{loggerName}")
public String adjustLoggerPage(Model model, @PathVariable(value = "loggerName") String loggerName) {
LoggerContext context = (LoggerContext) LogManager.getContext(false);
Logger logger;
if (loggerName == null || "root".equalsIgnoreCase(loggerName))
logger = context.getRootLogger();
else
logger = context.getLogger(loggerName);
model.addAttribute("logger", logger);
model.addAttribute("appenders", context.getConfiguration().getAppenders().values());
return "/admin/logger/edit";
}
@RequestMapping(method = RequestMethod.POST, value = "/changeLoger")
public String changeLogger(@RequestParam(value = "loggerLevel") String loggerLevel, @RequestParam(value = "loggerName") String loggerName) {
LoggerContext context = (LoggerContext) LogManager.getContext(false);
Configuration config = context.getConfiguration();
LoggerConfig loggerConfig;
if (loggerName == null || "root".equalsIgnoreCase(loggerName)) {
loggerConfig = config.getRootLogger();
} else {
loggerConfig = config.getLoggerConfig(loggerName);
}
if (loggerConfig != null) {
loggerConfig.setLevel(loggerLevel == null ? null : Level.toLevel(loggerLevel));
context.updateLoggers();
}
return "redirect:/admin/logger/";
}
private List<Logger> getAllLoggers() {
List<Logger> loggers = new ArrayList<>();
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
Configuration config = loggerContext.getConfiguration();
config.getLoggers().keySet().forEach(loggerName -> loggers.add(loggerContext.getLogger(loggerName)));
loggers.sort((o1, o2) -> {
// root logger is on the top
if (loggerContext.getRootLogger() == o1) {
return -1;
}
if (loggerContext.getRootLogger() == o2) {
return 1;
}
// otherwise sort by name
return o1.getName().compareTo(o2.getName());
});
return loggers;
}
@RequestMapping(method = RequestMethod.POST, value = "/addLoger")
public String addLogger(@RequestParam(value = "nameNewLogger") String nameNewLogger, @RequestParam("loggerLevel") String loggerLevel, Model model) {
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
Configuration config = loggerContext.getConfiguration();
org.apache.logging.log4j.Level level = org.apache.logging.log4j.Level.toLevel(loggerLevel);
LoggerConfig loggerConfig = new LoggerConfig(nameNewLogger, level, false);
Appender appender = config.getRootLogger().getAppenders().values().stream().findFirst().orElse(null);
loggerConfig.addAppender(appender, level, null);
config.addLogger(nameNewLogger, loggerConfig);
loggerContext.updateLoggers();
return "redirect:/admin/logger/";
}
}