调整日志

This commit is contained in:
bwcx_jzy 2019-07-22 15:18:06 +08:00
parent b631260606
commit df66040da2
6 changed files with 116 additions and 134 deletions

View File

@ -17,6 +17,7 @@
11. 【Server】新增查看项目实际执行的命令行
12. 【Server】新增分发日志
13. 新增清空文件缓存、临时数据缓存
14. 在线查看Jpom运行日志
### 解决BUG、优化功能

View File

@ -22,12 +22,9 @@ import java.util.concurrent.ConcurrentHashMap;
* @author jiangzeyin
* @date 2019/3/16
*/
public class AgentFileTailWatcher extends BaseFileTailWatcher {
private static final ConcurrentHashMap<File, AgentFileTailWatcher> CONCURRENT_HASH_MAP = new ConcurrentHashMap<>();
/**
* 所有会话
*/
private final Set<Session> socketSessions = new HashSet<>();
public class AgentFileTailWatcher<T> extends BaseFileTailWatcher<T> {
private static final ConcurrentHashMap<File, AgentFileTailWatcher<Session>> CONCURRENT_HASH_MAP = new ConcurrentHashMap<>();
private AgentFileTailWatcher(File logFile) throws IOException {
super(logFile);
@ -48,9 +45,9 @@ public class AgentFileTailWatcher extends BaseFileTailWatcher {
if (!file.exists() || file.isDirectory()) {
throw new IOException("文件不存在或者是目录:" + file.getPath());
}
AgentFileTailWatcher agentFileTailWatcher = CONCURRENT_HASH_MAP.computeIfAbsent(file, s -> {
AgentFileTailWatcher<Session> agentFileTailWatcher = CONCURRENT_HASH_MAP.computeIfAbsent(file, s -> {
try {
return new AgentFileTailWatcher(file);
return new AgentFileTailWatcher<>(file);
} catch (Exception e) {
DefaultSystemLog.ERROR().error("创建文件监听失败", e);
return null;
@ -69,8 +66,8 @@ public class AgentFileTailWatcher extends BaseFileTailWatcher {
* @param session 会话
*/
public static void offline(Session session) {
Collection<AgentFileTailWatcher> collection = CONCURRENT_HASH_MAP.values();
for (AgentFileTailWatcher agentFileTailWatcher : collection) {
Collection<AgentFileTailWatcher<Session>> collection = CONCURRENT_HASH_MAP.values();
for (AgentFileTailWatcher<Session> agentFileTailWatcher : collection) {
agentFileTailWatcher.socketSessions.removeIf(session::equals);
if (agentFileTailWatcher.socketSessions.isEmpty()) {
agentFileTailWatcher.close();
@ -84,7 +81,7 @@ public class AgentFileTailWatcher extends BaseFileTailWatcher {
* @param fileName 文件名
*/
public static void offlineFile(File fileName) {
AgentFileTailWatcher agentFileTailWatcher = CONCURRENT_HASH_MAP.get(fileName);
AgentFileTailWatcher<Session> agentFileTailWatcher = CONCURRENT_HASH_MAP.get(fileName);
if (null == agentFileTailWatcher) {
return;
}
@ -101,7 +98,7 @@ public class AgentFileTailWatcher extends BaseFileTailWatcher {
* @param fileName 文件名
*/
static void offlineFile(File fileName, Session session) {
AgentFileTailWatcher agentFileTailWatcher = CONCURRENT_HASH_MAP.get(fileName);
AgentFileTailWatcher<Session> agentFileTailWatcher = CONCURRENT_HASH_MAP.get(fileName);
if (null == agentFileTailWatcher) {
return;
}
@ -119,52 +116,6 @@ public class AgentFileTailWatcher extends BaseFileTailWatcher {
}
/**
* 添加监听会话
*
* @param session 会话
*/
private void add(Session session, String name) {
if (this.socketSessions.add(session) || this.socketSessions.contains(session)) {
LimitQueue<String> limitQueue = this.tailWatcherRun.getLimitQueue();
if (limitQueue.size() <= 0) {
this.send(session, "日志文件为空");
return;
}
this.send(session, StrUtil.format("监听{}日志成功,目前共有{}人正在查看", name, this.socketSessions.size()));
// 开发发送头信息
for (String s : limitQueue) {
this.send(session, s);
}
}
// else {
// this.send(session, "添加日志监听失败");
// }
}
private void send(Session session, String msg) {
try {
SocketSessionUtil.send(session, msg);
} catch (IOException ignored) {
}
}
@Override
protected void sendAll(String msg) {
Iterator<Session> iterator = socketSessions.iterator();
while (iterator.hasNext()) {
Session socketSession = iterator.next();
try {
SocketSessionUtil.send(socketSession, msg);
} catch (Exception e) {
DefaultSystemLog.ERROR().error("发送消息失败", e);
iterator.remove();
}
}
if (this.socketSessions.isEmpty()) {
this.close();
}
}
/**
@ -176,4 +127,4 @@ public class AgentFileTailWatcher extends BaseFileTailWatcher {
// 清理线程记录
CONCURRENT_HASH_MAP.remove(this.logFile);
}
}
}

View File

@ -1,28 +1,92 @@
package cn.keepbx.util;
import cn.hutool.core.util.StrUtil;
import cn.jiangzeyin.common.DefaultSystemLog;
import cn.keepbx.jpom.system.JpomRuntimeException;
import org.springframework.web.socket.WebSocketSession;
import javax.websocket.Session;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* @author bwcx_jzy
* @date 2019/7/21
*/
public abstract class BaseFileTailWatcher {
public abstract class BaseFileTailWatcher<T> {
protected FileTailWatcherRun tailWatcherRun;
protected File logFile;
/**
* 所有会话
*/
protected final Set<T> socketSessions = new HashSet<>();
public BaseFileTailWatcher(File logFile) throws IOException {
this.logFile = logFile;
this.tailWatcherRun = new FileTailWatcherRun(logFile, this::sendAll);
}
protected void send(T session, String msg) {
try {
if (session instanceof Session) {
SocketSessionUtil.send((Session) session, msg);
} else if (session instanceof WebSocketSession) {
SocketSessionUtil.send((WebSocketSession) session, msg);
} else {
throw new JpomRuntimeException("没有对应类型");
}
} catch (IOException ignored) {
}
}
/**
* 有新的日志
*
* @param msg 日志
*/
protected abstract void sendAll(String msg);
private void sendAll(String msg) {
Iterator<T> iterator = socketSessions.iterator();
while (iterator.hasNext()) {
T socketSession = iterator.next();
try {
this.send(socketSession, msg);
} catch (Exception e) {
DefaultSystemLog.ERROR().error("发送消息失败", e);
iterator.remove();
}
}
if (this.socketSessions.isEmpty()) {
this.close();
}
}
/**
* 添加监听会话
*
* @param session 会话
*/
protected void add(T session, String name) {
if (this.socketSessions.contains(session) || this.socketSessions.add(session)) {
LimitQueue<String> limitQueue = this.tailWatcherRun.getLimitQueue();
if (limitQueue.size() <= 0) {
this.send(session, "日志文件为空");
return;
}
this.send(session, StrUtil.format("监听{}日志成功,目前共有{}人正在查看", name, this.socketSessions.size()));
// 开发发送头信息
for (String s : limitQueue) {
this.send(session, s);
}
}
// else {
// this.send(session, "添加日志监听失败");
// }
}
/**
* 关闭

View File

@ -13,6 +13,7 @@ import cn.keepbx.jpom.common.forward.NodeUrl;
import cn.keepbx.jpom.common.interceptor.UrlPermission;
import cn.keepbx.jpom.model.Role;
import cn.keepbx.jpom.model.log.UserOperateLogV1;
import cn.keepbx.jpom.socket.ServiceFileTailWatcher;
import cn.keepbx.jpom.system.WebAopLog;
import cn.keepbx.util.LayuiTreeUtil;
import com.alibaba.fastjson.JSONArray;
@ -74,6 +75,8 @@ public class LogManageController extends BaseServerController {
return JsonMessage.getString(405, "不能删除当天的日志");
}
if (FileUtil.del(file)) {
// 离线上一个日志
ServiceFileTailWatcher.offlineFile(file);
return JsonMessage.getString(200, "删除成功");
}
return JsonMessage.getString(500, "删除失败");

View File

@ -1,18 +1,13 @@
package cn.keepbx.jpom.socket;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.jiangzeyin.common.DefaultSystemLog;
import cn.keepbx.util.BaseFileTailWatcher;
import cn.keepbx.util.LimitQueue;
import cn.keepbx.util.SocketSessionUtil;
import org.springframework.web.socket.WebSocketSession;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@ -22,12 +17,8 @@ import java.util.concurrent.ConcurrentHashMap;
* @author jiangzeyin
* @date 2019/07/21
*/
public class ServiceFileTailWatcher extends BaseFileTailWatcher {
private static final ConcurrentHashMap<File, ServiceFileTailWatcher> CONCURRENT_HASH_MAP = new ConcurrentHashMap<>();
/**
* 所有会话
*/
private final Set<WebSocketSession> socketSessions = new HashSet<>();
public class ServiceFileTailWatcher<T> extends BaseFileTailWatcher<T> {
private static final ConcurrentHashMap<File, ServiceFileTailWatcher<WebSocketSession>> CONCURRENT_HASH_MAP = new ConcurrentHashMap<>();
private ServiceFileTailWatcher(File logFile) throws IOException {
super(logFile);
@ -48,9 +39,9 @@ public class ServiceFileTailWatcher extends BaseFileTailWatcher {
if (!file.exists() || file.isDirectory()) {
throw new IOException("文件不存在或者是目录:" + file.getPath());
}
ServiceFileTailWatcher agentFileTailWatcher = CONCURRENT_HASH_MAP.computeIfAbsent(file, s -> {
ServiceFileTailWatcher<WebSocketSession> agentFileTailWatcher = CONCURRENT_HASH_MAP.computeIfAbsent(file, s -> {
try {
return new ServiceFileTailWatcher(file);
return new ServiceFileTailWatcher<>(file);
} catch (Exception e) {
DefaultSystemLog.ERROR().error("创建文件监听失败", e);
return null;
@ -69,8 +60,8 @@ public class ServiceFileTailWatcher extends BaseFileTailWatcher {
* @param session 会话
*/
public static void offline(WebSocketSession session) {
Collection<ServiceFileTailWatcher> collection = CONCURRENT_HASH_MAP.values();
for (ServiceFileTailWatcher agentFileTailWatcher : collection) {
Collection<ServiceFileTailWatcher<WebSocketSession>> collection = CONCURRENT_HASH_MAP.values();
for (ServiceFileTailWatcher<WebSocketSession> agentFileTailWatcher : collection) {
agentFileTailWatcher.socketSessions.removeIf(session::equals);
if (agentFileTailWatcher.socketSessions.isEmpty()) {
agentFileTailWatcher.close();
@ -78,77 +69,45 @@ public class ServiceFileTailWatcher extends BaseFileTailWatcher {
}
}
/**
* 关闭文件
*
* @param fileName 文件
*/
public static void offlineFile(File fileName) {
ServiceFileTailWatcher<WebSocketSession> agentFileTailWatcher = CONCURRENT_HASH_MAP.get(fileName);
if (null == agentFileTailWatcher) {
return;
}
Set<WebSocketSession> socketSessions = agentFileTailWatcher.socketSessions;
for (WebSocketSession socketSession : socketSessions) {
offline(socketSession);
}
agentFileTailWatcher.close();
}
/**
* 关闭文件读取流
*
* @param fileName 文件名
*/
public static void offlineFile(File fileName, WebSocketSession session) {
ServiceFileTailWatcher agentFileTailWatcher = CONCURRENT_HASH_MAP.get(fileName);
if (null == agentFileTailWatcher) {
ServiceFileTailWatcher<WebSocketSession> serviceFileTailWatcher = CONCURRENT_HASH_MAP.get(fileName);
if (null == serviceFileTailWatcher) {
return;
}
Set<WebSocketSession> socketSessions = agentFileTailWatcher.socketSessions;
Set<WebSocketSession> socketSessions = serviceFileTailWatcher.socketSessions;
for (WebSocketSession socketSession : socketSessions) {
if (socketSession.equals(session)) {
offline(socketSession);
break;
}
}
if (agentFileTailWatcher.socketSessions.isEmpty()) {
agentFileTailWatcher.close();
if (serviceFileTailWatcher.socketSessions.isEmpty()) {
serviceFileTailWatcher.close();
}
}
/**
* 添加监听会话
*
* @param session 会话
*/
private void add(WebSocketSession session, String name) {
if (this.socketSessions.add(session) || this.socketSessions.contains(session)) {
LimitQueue<String> limitQueue = this.tailWatcherRun.getLimitQueue();
if (limitQueue.size() <= 0) {
this.send(session, "日志文件为空");
return;
}
this.send(session, StrUtil.format("监听{}日志成功,目前共有{}人正在查看", name, this.socketSessions.size()));
// 开发发送头信息
for (String s : limitQueue) {
this.send(session, s);
}
}
// else {
// this.send(session, "添加日志监听失败");
// }
}
private void send(WebSocketSession session, String msg) {
try {
SocketSessionUtil.send(session, msg);
} catch (IOException ignored) {
}
}
@Override
protected void sendAll(String msg) {
Iterator<WebSocketSession> iterator = socketSessions.iterator();
while (iterator.hasNext()) {
WebSocketSession socketSession = iterator.next();
try {
SocketSessionUtil.send(socketSession, msg);
} catch (Exception e) {
DefaultSystemLog.ERROR().error("发送消息失败", e);
iterator.remove();
}
}
if (this.socketSessions.isEmpty()) {
this.close();
}
}
/**
* 关闭
*/
@ -158,4 +117,4 @@ public class ServiceFileTailWatcher extends BaseFileTailWatcher {
// 清理线程记录
CONCURRENT_HASH_MAP.remove(this.logFile);
}
}
}

View File

@ -51,6 +51,7 @@
if (obj.data.children) {
return;
}
$("#nowFileName").attr("path", obj.data.path);
$("#nowFileName").text(obj.data.title);
openSocket(obj.data.path);
},
@ -71,9 +72,12 @@
success: function (data) {
layer.msg(data.msg);
if (200 == data.code) {
setTimeout(function () {
location.reload();
}, 2000)
var attr = $("#nowFileName").attr("path");
if (attr == data.path) {
setTimeout(function () {
location.reload();
}, 2000)
}
} else {
reloadTree();
}