在泛微ecology9的二次开发中,我们经常需要借助打印日志排查程序运行状况,但每次打开日志都很浪费时间,我们可以把日志展示到页面中,优化本就繁琐的排查过程。
效果图
后端代码
@Path("/user")
public class UserApi {
@GET
@Path("/getCache")
@Produces(MediaType.APPLICATION_JSON)
public String getCache(@QueryParam("type")String type) {
String path;
if ("ecology".equals(type)){
path = System.getProperty("user.dir").replace("Resin","ecology") + "\\log\\ecology";
}else{
path = System.getProperty("user.dir") + "\\log\\jvm-app-0.log";
}
String str = null;
InputStream in = null;
try {
File file = new File(path);
in = new FileInputStream(file);
byte[] bytes = new byte[(int) file.length()];
in.read(bytes);
str = new String(bytes,"GB2312");
if (str.length()>50000){
str = str.substring(str.length() - 50000);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return str;
}
}
前端代码(将该Acache.html文件放入ecology根目录下)
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<title>ecology日志</title>
</head>
<body>
<pre id="pre1"></pre>
<button onclick="flush()">刷新</button>
<select id="select">
<option value="ecology">ecology</option>
<option value="resin">resin</option>
</select>
<input type="text" id="word">
<button onclick="search()">搜索</button>
<script>
flush()
let content;
function flush(){
const pre = document.getElementById('pre1');
pre.innerHTML = '...正在加载中';
const myselect = document.getElementById("select")
let index = myselect.selectedIndex;
let value = myselect.options[index].value;
fetch(`/api/user/getCache?type=${value}`,
{ method: 'GET',headers: new Headers({'Accept': 'application/json'})}
).then(res => {
return res.text()
}).then(res => {
content = res
pre.innerHTML = res;
window.scrollTo(0, document.body.scrollHeight);
})
}
function search(){
let word = document.getElementById('word').value;
if (word){
const pre = document.getElementById('pre1');
pre.innerHTML = content
pre.innerHTML = pre.innerHTML.replaceAll(word, `<span style="background-color: yellow">${word}</span>`)
}
}
</script>
<style>
@import url('https://fonts.googleapis.com/css?family=Roboto&display=swap');
@import url('https://fonts.googleapis.com/css?family=Roboto+Mono&display=swap');
html {
font-family: 'Roboto', sans-serif;
}
pre {
font-family: 'Roboto mono', monospace;
font-size: 0.9rem;
background-color: #D6EAF8;
padding: 10px;
}
</style>
</body>
</html>
系统配置