com.github.nomou.log4web.servlet.WatchServlet Maven / Gradle / Ivy
package com.github.nomou.log4web.servlet;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.nomou.log4web.LogWatcher;
import com.github.nomou.log4web.LoggerInfo;
import com.github.nomou.log4web.cfg.LogWatcherConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* TODO DOC ME!.
*
* @author changhe.yang
* @since 20190920
*/
// @WebServlet(value = "/logging/*", loadOnStartup = 1)
public class WatchServlet extends HttpServlet {
private final String resourcePath = "support";
private LogWatcher> watcher;
@Override
public void init() throws ServletException {
final LogWatcherConfig config = new LogWatcherConfig(true, null, null, 500);
watcher = LogWatcher.newRegisteredLogWatcher(config);
}
@Override
protected void doGet(final HttpServletRequest httpRequest, final HttpServletResponse httpResponse) throws ServletException, IOException {
final String path = httpRequest.getPathInfo();
if (null == path) {
final String redirectUrl = httpRequest.getContextPath() + httpRequest.getServletPath() + "/";
httpResponse.sendRedirect(redirectUrl);
return;
}
httpResponse.setCharacterEncoding("UTF-8");
if ("/".equals(path)) {
final String level = httpRequest.getParameter("set");
if (null != level && watcher.getAllLevels().contains(level.toUpperCase())) {
watcher.setThreshold(level.toUpperCase());
}
httpResponse.setContentType("text/html");
writeResource("support/logging.html", httpResponse);
} else if ("/watch".equals(path)) {
doGetHistory(httpRequest, httpResponse);
} else if ("/loggers".equals(path)) {
doGetLoggers(httpRequest, httpResponse);
} else if ("/level".equals(path)) {
final String set = httpRequest.getParameter("set");
final int index = set.indexOf(':');
if (0 < index) {
final String category = set.substring(0, index);
final String level = set.substring(index + 1);
if (watcher.getAllLevels().contains(level.toUpperCase())) {
watcher.setLogLevel(category, level);
httpResponse.getWriter().write("OK");
} else {
httpResponse.getWriter().write("Illegal level");
}
} else {
httpResponse.getWriter().write("Illegal request");
}
} else {
if (path.endsWith(".html")) {
httpResponse.setContentType("text/html");
writeResource(resourcePath + path, httpResponse);
} else if (path.endsWith(".css")) {
httpResponse.setContentType("text/css");
writeResource(resourcePath + path, httpResponse);
} else if (path.endsWith(".js")) {
httpResponse.setContentType("text/javascript");
writeResource(resourcePath + path, httpResponse);
} else if (path.endsWith(".png") || path.endsWith(".jpg") || path.endsWith(".gif") || path.endsWith(".ico")) {
writeResource(resourcePath + path, httpResponse);
}
}
}
/**
* 直接将资源写入响应
*
* @param internalPath 资源内部路径
* @param response 响应对象
*/
protected void writeResource(String internalPath, HttpServletResponse response) throws IOException {
final InputStream in = getClass().getClassLoader().getResourceAsStream(internalPath);
if (null != in) {
try {
int readed;
final byte[] buffer = new byte[1024];
final ServletOutputStream out = response.getOutputStream();
while (-1 < (readed = in.read(buffer))) {
out.write(buffer, 0, readed);
}
out.flush();
} finally {
try {
in.close();
} catch (IOException ignore) {
// ignore
}
}
} else {
response.sendError(404);
}
}
private void doGetHistory(final HttpServletRequest httpRequest, final HttpServletResponse httpResponse) throws ServletException, IOException {
final String sinceStr = httpRequest.getParameter("since");
final long since = resolveLong(sinceStr, 0);
final AtomicBoolean foundRef = new AtomicBoolean(false);
final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy