1
|
package eu.dnetlib.functionality.modular.ui;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.Enumeration;
|
5
|
import java.util.List;
|
6
|
|
7
|
import javax.annotation.PostConstruct;
|
8
|
|
9
|
import org.apache.commons.logging.Log;
|
10
|
import org.apache.commons.logging.LogFactory;
|
11
|
import org.apache.log4j.Level;
|
12
|
import org.apache.log4j.LogManager;
|
13
|
import org.apache.log4j.Logger;
|
14
|
import org.springframework.stereotype.Controller;
|
15
|
import org.springframework.web.bind.annotation.PathVariable;
|
16
|
import org.springframework.web.bind.annotation.RequestMapping;
|
17
|
import org.springframework.web.bind.annotation.ResponseBody;
|
18
|
|
19
|
import eu.dnetlib.functionality.modular.ui.utils.LogLine;
|
20
|
import eu.dnetlib.functionality.modular.ui.utils.LogUiAppender;
|
21
|
|
22
|
@Controller
|
23
|
public class DnetLogAjaxController {
|
24
|
|
25
|
private LogUiAppender appender;
|
26
|
|
27
|
private static final Log log = LogFactory.getLog(DnetLogAjaxController.class);
|
28
|
|
29
|
@PostConstruct
|
30
|
void init() throws IOException {
|
31
|
this.appender = findLogUiAppender();
|
32
|
}
|
33
|
|
34
|
private LogUiAppender findLogUiAppender() {
|
35
|
final Logger logger = LogManager.getRootLogger();
|
36
|
final Enumeration<?> appenders = logger.getAllAppenders();
|
37
|
while (appenders.hasMoreElements()) {
|
38
|
final Object app = appenders.nextElement();
|
39
|
if (app instanceof LogUiAppender) {
|
40
|
log.info("An istance of LogUiAppender is already registered in log4j configuration.");
|
41
|
return (LogUiAppender) app;
|
42
|
}
|
43
|
}
|
44
|
|
45
|
final LogUiAppender app = new LogUiAppender();
|
46
|
LogManager.getRootLogger().addAppender(app);
|
47
|
|
48
|
log.info("A new LogUiAppender has been registered in log4j configuration.");
|
49
|
|
50
|
return app;
|
51
|
}
|
52
|
|
53
|
@RequestMapping(value = "/ui/log/tail/{n}")
|
54
|
public @ResponseBody
|
55
|
List<LogLine> tail_N(@PathVariable(value = "n") final int n) throws Exception {
|
56
|
return appender.tail_N(n);
|
57
|
}
|
58
|
|
59
|
@RequestMapping(value = "/ui/log/continue/{after}")
|
60
|
public @ResponseBody
|
61
|
List<LogLine> tail_continue(@PathVariable(value = "after") final int after) throws Exception {
|
62
|
return appender.tail_continue(after);
|
63
|
}
|
64
|
|
65
|
@RequestMapping(value = "/ui/log/level/{level}/{resource}")
|
66
|
public @ResponseBody
|
67
|
boolean updateLogLevel(@PathVariable(value = "level") final String level, @PathVariable(value = "resource") final String resource) throws Exception {
|
68
|
if (resource != null && level != null) {
|
69
|
LogManager.getLogger(resource).setLevel(Level.toLevel(level));
|
70
|
}
|
71
|
return true;
|
72
|
}
|
73
|
|
74
|
}
|