feat: add log history

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-06-02 18:11:45 +08:00
parent 80c6b123c1
commit 5b11be11da

View File

@ -43,6 +43,8 @@ export class MiaoConsole extends interfaces.Plugin {
private appender: any
private handler: any
private logCache: string[] = []
@config()
private secret = { token: undefined }
@ -54,6 +56,12 @@ export class MiaoConsole extends interfaces.Plugin {
this.token = Java.type('java.util.UUID').randomUUID().toString()
this.logger.console(`§6已生成随机Token: §3${this.token} §c重启后或重新生成后失效!`)
}
global.eventCenter.on('log', (msg) => {
this.logCache.push(msg)
if (this.logCache.length > 30) {
this.logCache = this.logCache.slice(this.logCache.length - 30, this.logCache.length)
}
})
}
@cmd()
@ -103,13 +111,14 @@ export class MiaoConsole extends interfaces.Plugin {
wait?.cancel()
this.createSocketIOServer()
this.startSocketIOServer()
return
}
if (count++ > 30) {
if (count++ > 5) {
wait?.cancel()
this.logger.console('§cNetty通道注入失败 §4所有功能将无法使用')
return
}
}).later(20).timer(40).submit()
}).later(20).timer(20).submit()
this.rootLogger = this.server.getRootLogger()
}
@ -117,8 +126,13 @@ export class MiaoConsole extends interfaces.Plugin {
addLog4jForward() {
if (this.rootLogger) {
let AbstractAppender = Java.type('org.apache.logging.log4j.core.appender.AbstractAppender')
let Level = Java.type('org.apache.logging.log4j.Level')
let ProxyAppender = Java.extend(AbstractAppender, {
append: (logEvent) => global.eventCenter.emit('log', logEvent.getMessage().getFormattedMessage())
append: (logEvent) => {
if (logEvent.level.intLevel() <= Level.INFO.intLevel()) {
global.eventCenter.emit('log', logEvent.getMessage().getFormattedMessage())
}
}
})
this.appender = new ProxyAppender("ProxyLogger", null, null)
this.appender.start()
@ -141,6 +155,8 @@ export class MiaoConsole extends interfaces.Plugin {
}
}
private LOGBACK_APPENDER_NAME = "NashornProxyAppender"
@enable({ servers: [constants.ServerType.Spring] })
addLogbackForward() {
if (this.rootLogger) {
@ -149,7 +165,8 @@ export class MiaoConsole extends interfaces.Plugin {
append: (logEvent) => global.eventCenter.emit('log', logEvent.getFormattedMessage())
})
this.appender = new ProxyAppender()
this.appender.setName("NashornProxyAppender")
this.appender.start()
this.appender.setName(this.LOGBACK_APPENDER_NAME)
this.appender.setContext(this.rootLogger.getLoggerContext())
this.rootLogger.addAppender(this.appender)
}
@ -187,7 +204,7 @@ export class MiaoConsole extends interfaces.Plugin {
@disable({ servers: [constants.ServerType.Spring] })
removeLogbackForward() {
try {
this.rootLogger.detachAppender("NashornProxyAppender")
this.rootLogger.detachAppender(this.LOGBACK_APPENDER_NAME)
} catch (error) {
console.ex(error)
}
@ -211,14 +228,19 @@ export class MiaoConsole extends interfaces.Plugin {
client.disconnect(true)
return
}
this.logger.console(`§6客户端 §b${client.id} §a请求连接 §4Token: §c********`)
if (this.token != client.handshake.query.token) {
this.logger.console(`§6客户端 §b${client.id} §4无效请求 请提供正确Token后再次连接!`)
this.logger.console(`§6客户端 §b${client.id} §c无效请求 §4请提供正确Token后再次连接!`)
client.emit('unauthorized')
client.disconnect(true)
return
}
this.initWebSocketClient(client)
this.logCache.forEach(msg => client.emit('log', msg))
this.logger.console(`§6客户端 §b${client.id} §a新建连接 ${this.rootLogger ? '启动日志转发' : '§4转发日志启动失败'}...`)
})
}
private initWebSocketClient(client: SocketIOSocket) {
client.on('type', (fn) => {
fn && fn(this.serverType)
client.emit('log', `Currect Server Version is ${this.server.getVersion()}`)
@ -229,7 +251,7 @@ export class MiaoConsole extends interfaces.Plugin {
})
client.on('exec', (code) => {
try {
client.emit('log', this.runCode(code, namespace, client))
client.emit('log', this.runCode(code, client.nsp, client))
} catch (ex) {
client.emit('log', `§4代码执行异常 错误: ${ex}\n${console.stack(ex).join('\n')}`)
}
@ -264,19 +286,33 @@ export class MiaoConsole extends interfaces.Plugin {
client.on('disconnect', () => {
this.logger.console(`§6客户端 §b${client.id} §c断开连接...`)
})
})
}
private runCode(code: string, namespace: any, client: any) {
let tfunc = new Function('namespace', 'client', `
var reflect = require('@ccms/common/dist/reflect');
var tempconcent = '';
function print(text) {
tempconcent += text + "\\n"
let paramNames = [
'namespace',
'client',
'reflect',
'container',
'pluginManager'
]
let params = [
namespace,
client,
reflect,
this.container,
this.pluginManager
]
let tfunc = new Function(
...paramNames,
`var api = require('@ccms/api');
if (this.serverType == "spring") {
var dbm = container.get(api.database.DataBaseManager)
var db = dbm.getMainDatabase()
var df = base.getInstance().getAutowireCapableBeanFactory()
}
var result = eval(${JSON.stringify(code)});
return tempconcent + '§a返回结果: §r'+ result
return '§a返回结果: §r'+ eval(${JSON.stringify(code)});
`)
return this.task.callSyncMethod(() => tfunc.apply(this, [namespace, client])) + ''
return this.task.callSyncMethod(() => tfunc.apply(this, params)) + ''
}
}