增加非DEBUG期间日志

This commit is contained in:
mxd
2021-08-21 19:19:45 +08:00
parent a716e60ea2
commit fa57f13a95
11 changed files with 133 additions and 76 deletions

View File

@@ -19,4 +19,6 @@ public enum MessageType {
RESUME_BREAKPOINT,
/* 设置 Session ID */
SET_SESSION_ID,
/* 登录 */
LOGIN
}

View File

@@ -12,6 +12,7 @@ import org.ssssssss.script.MagicScriptDebugContext;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
public class WebSocketSessionManager {
@@ -29,7 +30,7 @@ public class WebSocketSessionManager {
}
public static void remove(MagicConsoleSession session) {
if(session.getId() != null){
if (session.getId() != null) {
remove(session.getId());
}
}
@@ -38,8 +39,29 @@ public class WebSocketSessionManager {
SESSION.remove(sessionId);
}
public static void sendToAll(MessageType messageType, Object... values) {
String content = buildMessage(messageType, values);
sendToAll(content);
// 通知其他机器去发送消息
magicNotifyService.sendNotify(new MagicNotify(instanceId, Constants.NOTIFY_WS_S_C, null, content));
}
private static void sendToAll(String content) {
SESSION.values().stream().filter(MagicConsoleSession::writeable).forEach(session -> sendBySession(session, content));
}
public static void sendBySessionId(String sessionId, MessageType messageType, Object... values) {
MagicConsoleSession session = findSession(sessionId);
String content = buildMessage(messageType, values);
if (session != null && session.writeable()) {
sendBySession(session, content);
} else if (magicNotifyService != null) {
// 通知其他机器去发送消息
magicNotifyService.sendNotify(new MagicNotify(instanceId, Constants.NOTIFY_WS_S_C, sessionId, content));
}
}
private static String buildMessage(MessageType messageType, Object... values) {
StringBuilder builder = new StringBuilder(messageType.name().toLowerCase());
if (values != null) {
for (int i = 0, len = values.length; i < len; i++) {
@@ -52,22 +74,21 @@ public class WebSocketSessionManager {
}
}
}
if (session != null && session.writeable()) {
sendBySession(session, builder.toString());
} else if(magicNotifyService != null){
// 通知其他机器去发送消息
magicNotifyService.sendNotify(new MagicNotify(instanceId, Constants.NOTIFY_WS_S_C, sessionId, builder.toString()));
return builder.toString();
}
public static void sendBySessionId(String sessionId, String content) {
if (sessionId == null) {
sendToAll(content);
} else {
MagicConsoleSession session = findSession(sessionId);
if (session != null) {
sendBySession(session, content);
}
}
}
public static void sendBySessionId(String sessionId, String content){
MagicConsoleSession session = findSession(sessionId);
if (session != null) {
sendBySession(session, content);
}
}
public static void sendBySession(MagicConsoleSession session, String content){
public static void sendBySession(MagicConsoleSession session, String content) {
try {
session.getWebSocketSession().sendMessage(new TextMessage(content));
} catch (IOException e) {
@@ -76,7 +97,10 @@ public class WebSocketSessionManager {
}
public static MagicConsoleSession findSession(String sessionId) {
return SESSION.get(sessionId);
return SESSION.values().stream()
.filter(it -> Objects.equals(sessionId, it.getSessionId()))
.findFirst()
.orElse(null);
}
public static void setMagicNotifyService(MagicNotifyService magicNotifyService) {
@@ -87,12 +111,12 @@ public class WebSocketSessionManager {
WebSocketSessionManager.instanceId = instanceId;
}
public static void createSession(String sessionId, MagicScriptDebugContext debugContext){
MagicConsoleSession consoleSession = SESSION.get(sessionId);
if(consoleSession == null){
public static void createSession(String sessionId, MagicScriptDebugContext debugContext) {
MagicConsoleSession consoleSession = findSession(sessionId);
if (consoleSession == null) {
consoleSession = new MagicConsoleSession(sessionId, debugContext);
SESSION.put(sessionId, consoleSession);
}else{
} else {
consoleSession.setMagicScriptDebugContext(debugContext);
}
}

View File

@@ -18,9 +18,7 @@ public class MagicDebugHandler {
*/
@Message(MessageType.SET_SESSION_ID)
public void setSessionId(MagicConsoleSession session, String sessionId) {
WebSocketSessionManager.remove(session);
session.setId(sessionId);
WebSocketSessionManager.add(session);
session.setSessionId(sessionId);
}
/**

View File

@@ -46,6 +46,7 @@ public class MagicWebSocketDispatcher extends TextWebSocketHandler {
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
WebSocketSessionManager.remove(MagicConsoleSession.from(session));
MagicConsoleSession.remove(session);
}

View File

@@ -0,0 +1,28 @@
package org.ssssssss.magicapi.controller;
import org.ssssssss.magicapi.config.Message;
import org.ssssssss.magicapi.config.MessageType;
import org.ssssssss.magicapi.config.WebSocketSessionManager;
import org.ssssssss.magicapi.exception.MagicLoginException;
import org.ssssssss.magicapi.interceptor.AuthorizationInterceptor;
import org.ssssssss.magicapi.model.MagicConsoleSession;
public class MagicWorkbenchHandler {
private final AuthorizationInterceptor authorizationInterceptor;
public MagicWorkbenchHandler(AuthorizationInterceptor authorizationInterceptor) {
this.authorizationInterceptor = authorizationInterceptor;
}
@Message(MessageType.LOGIN)
public void onLogin(MagicConsoleSession session, String token){
try {
if(!authorizationInterceptor.requireLogin() || authorizationInterceptor.getUserByToken(token) != null){
WebSocketSessionManager.add(session);
}
} catch (MagicLoginException ignored) {
}
}
}

View File

@@ -1,16 +1,7 @@
package org.ssssssss.magicapi.logging;
import org.slf4j.MDC;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import org.ssssssss.magicapi.config.MessageType;
import org.ssssssss.magicapi.config.WebSocketSessionManager;
import org.ssssssss.script.MagicScriptContext;
import org.ssssssss.script.MagicScriptDebugContext;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
public interface MagicLoggerContext {
@@ -28,6 +19,8 @@ public interface MagicLoggerContext {
String sessionId = SESSION.get();
if (sessionId != null) {
WebSocketSessionManager.sendBySessionId(sessionId, MessageType.LOG, logInfo);
}else{
WebSocketSessionManager.sendToAll(MessageType.LOG, logInfo);
}
}

View File

@@ -4,24 +4,27 @@ import org.springframework.web.socket.WebSocketSession;
import org.ssssssss.script.MagicScriptDebugContext;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
public class MagicConsoleSession {
private static final Map<String, MagicConsoleSession> cached = new ConcurrentHashMap<>();
private String id;
private final String id = UUID.randomUUID().toString();
private WebSocketSession webSocketSession;
private MagicScriptDebugContext magicScriptDebugContext;
private String sessionId;
public MagicConsoleSession(WebSocketSession webSocketSession) {
this.webSocketSession = webSocketSession;
}
public MagicConsoleSession(String id, MagicScriptDebugContext magicScriptDebugContext) {
this.id = id;
public MagicConsoleSession(String sessionId, MagicScriptDebugContext magicScriptDebugContext) {
this.sessionId = sessionId;
this.magicScriptDebugContext = magicScriptDebugContext;
}
@@ -29,10 +32,6 @@ public class MagicConsoleSession {
return id;
}
public void setId(String id) {
this.id = id;
}
public WebSocketSession getWebSocketSession() {
return webSocketSession;
}
@@ -53,6 +52,14 @@ public class MagicConsoleSession {
return webSocketSession != null && webSocketSession.isOpen();
}
public String getSessionId() {
return sessionId;
}
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
public static MagicConsoleSession from(WebSocketSession session){
MagicConsoleSession magicConsoleSession = cached.get(session.getId());
if(magicConsoleSession == null){

View File

@@ -199,7 +199,6 @@ export default {
bus.$emit('delete-api', this.info)
}
})
bus.$on('ws_log', rows => this.onLogReceived(rows[0]))
bus.$on('ws_breakpoint', rows => this.onBreakpoint(rows[0]))
bus.$on('ws_exception', args => this.onException(args[0]))
let javaTypes = {
@@ -227,24 +226,6 @@ export default {
})
},
methods: {
onLogReceived(row){
if(this.info){
row.timestamp = utils.formatDate(new Date())
let throwable = row.throwable
delete row.throwable
this.info.ext.logs.push(row)
if (throwable) {
let messages = throwable.replace(/ /g, '&nbsp;').split('\n');
for (let i = 0; i < messages.length; i++) {
this.info.ext.logs.push({
level: row.level,
message: messages[i],
throwable: true
})
}
}
}
},
onException(args){
if (this.info?.ext?.sessionId === args[0]) {
let line = args[2]

View File

@@ -11,39 +11,53 @@
</template>
<script>
import bus from "@/scripts/bus";
import * as utils from "@/scripts/utils";
export default {
name: "MagicLog",
props: {
info: Object
},
mounted() {
},
computed: {
logs() {
return this.info && this.info.ext && this.info.ext.logs || []
data(){
return {
logs: []
}
},
mounted() {
bus.$on('ws_log', rows => this.onLogReceived(rows[0]))
},
methods: {
onLogReceived(row){
row.timestamp = utils.formatDate(new Date())
let throwable = row.throwable
delete row.throwable
row.message = (row.message || '').replace(/ /g, '&nbsp;').replace(/\n/g,'<br>')
console.log(row.message)
this.logs.push(row)
if (throwable) {
let messages = throwable.replace(/ /g, '&nbsp;').split('\n');
for (let i = 0; i < messages.length; i++) {
this.logs.push({
level: row.level,
message: messages[i],
throwable: true
})
}
}
let container = this.$refs.container;
this.$nextTick(() => container.scrollTop = container.scrollHeight)
},
onContextMenu(event) {
this.$magicContextmenu({
event,
menus: [{
label: '清空日志',
onClick: () => this.info && this.info.ext && this.info.ext.logs && this.info.ext.logs.splice(0)
onClick: () => this.logs.splice(0)
}]
})
}
},
watch: {
'info.ext.logs': {
deep: true,
handler(newVal) {
let container = this.$refs.container;
this.$nextTick(() => container.scrollTop = container.scrollHeight)
}
}
}
};
</script>
@@ -56,7 +70,7 @@ export default {
}
.ma-log > div > div {
display: inline-block;
display: inline;
line-height: 20px;
white-space: nowrap;
}

View File

@@ -109,7 +109,10 @@ export default {
} else {
link = link + '/' + contants.BASE_URL
}
this.websocket = new MagicWebSocket(replaceURL(link.replace(/^http/, 'ws') + '/console'))
bus.$on('login', () => {
this.websocket = new MagicWebSocket(replaceURL(link.replace(/^http/, 'ws') + '/console'))
})
bus.$on('ws_open', () => bus.$emit('message', 'login', contants.HEADER_MAGIC_TOKEN_VALUE))
contants.DEFAULT_EXPAND = this.config.defaultExpand !== false
this.config.version = contants.MAGIC_API_VERSION_TEXT
this.config.title = this.config.title || 'magic-api'
@@ -195,7 +198,10 @@ export default {
this.toolbarIndex = 1
}
})
bus.$on('logout', () => this.showLogin = true)
bus.$on('logout', () => {
this.showLogin = true
this.websocket.close()
})
bus.$on('showLogin', () => this.showLogin = true)
this.open()
},

View File

@@ -11,6 +11,9 @@ function MagicWebSocket(url) {
this.socket.send(msgType)
}
})
this.socket.onopen = ()=> {
bus.$emit('ws_open')
}
}
MagicWebSocket.prototype.on = function (msgType, callback) {