1 |
41837
|
michele.ar
|
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 |
42444
|
michele.ar
|
public class DnetLogAjaxController extends AbstractAjaxController {
|
24 |
41837
|
michele.ar
|
|
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 |
42444
|
michele.ar
|
public @ResponseBody List<LogLine> tail_N(@PathVariable(value = "n") final int n) throws Exception {
|
55 |
|
|
return this.appender.tail_N(n);
|
56 |
41837
|
michele.ar
|
}
|
57 |
|
|
|
58 |
|
|
@RequestMapping(value = "/ui/log/continue/{after}")
|
59 |
42444
|
michele.ar
|
public @ResponseBody List<LogLine> tail_continue(@PathVariable(value = "after") final int after) throws Exception {
|
60 |
|
|
return tail_continue(after, 0);
|
61 |
41837
|
michele.ar
|
}
|
62 |
|
|
|
63 |
42444
|
michele.ar
|
private List<LogLine> tail_continue(final int after, final int recursionLevel) throws Exception {
|
64 |
|
|
final List<LogLine> res = this.appender.tail_continue(after);
|
65 |
|
|
if (res.isEmpty() && recursionLevel < 10) {
|
66 |
|
|
Thread.sleep(10000);
|
67 |
|
|
return tail_continue(after, recursionLevel + 1);
|
68 |
|
|
} else {
|
69 |
|
|
return res;
|
70 |
|
|
}
|
71 |
|
|
}
|
72 |
|
|
|
73 |
41837
|
michele.ar
|
@RequestMapping(value = "/ui/log/level/{level}/{resource}")
|
74 |
42444
|
michele.ar
|
public @ResponseBody boolean updateLogLevel(@PathVariable(value = "level") final String level, @PathVariable(value = "resource") final String resource)
|
75 |
|
|
throws Exception {
|
76 |
41837
|
michele.ar
|
if (resource != null && level != null) {
|
77 |
|
|
LogManager.getLogger(resource).setLevel(Level.toLevel(level));
|
78 |
|
|
}
|
79 |
|
|
return true;
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
}
|