feat: add log history
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
80c6b123c1
commit
5b11be11da
@ -43,6 +43,8 @@ export class MiaoConsole extends interfaces.Plugin {
|
|||||||
private appender: any
|
private appender: any
|
||||||
private handler: any
|
private handler: any
|
||||||
|
|
||||||
|
private logCache: string[] = []
|
||||||
|
|
||||||
@config()
|
@config()
|
||||||
private secret = { token: undefined }
|
private secret = { token: undefined }
|
||||||
|
|
||||||
@ -54,6 +56,12 @@ export class MiaoConsole extends interfaces.Plugin {
|
|||||||
this.token = Java.type('java.util.UUID').randomUUID().toString()
|
this.token = Java.type('java.util.UUID').randomUUID().toString()
|
||||||
this.logger.console(`§6已生成随机Token: §3${this.token} §c重启后或重新生成后失效!`)
|
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()
|
@cmd()
|
||||||
@ -103,13 +111,14 @@ export class MiaoConsole extends interfaces.Plugin {
|
|||||||
wait?.cancel()
|
wait?.cancel()
|
||||||
this.createSocketIOServer()
|
this.createSocketIOServer()
|
||||||
this.startSocketIOServer()
|
this.startSocketIOServer()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if (count++ > 30) {
|
if (count++ > 5) {
|
||||||
wait?.cancel()
|
wait?.cancel()
|
||||||
this.logger.console('§cNetty通道注入失败 §4所有功能将无法使用!')
|
this.logger.console('§cNetty通道注入失败 §4所有功能将无法使用!')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}).later(20).timer(40).submit()
|
}).later(20).timer(20).submit()
|
||||||
this.rootLogger = this.server.getRootLogger()
|
this.rootLogger = this.server.getRootLogger()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,8 +126,13 @@ export class MiaoConsole extends interfaces.Plugin {
|
|||||||
addLog4jForward() {
|
addLog4jForward() {
|
||||||
if (this.rootLogger) {
|
if (this.rootLogger) {
|
||||||
let AbstractAppender = Java.type('org.apache.logging.log4j.core.appender.AbstractAppender')
|
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, {
|
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 = new ProxyAppender("ProxyLogger", null, null)
|
||||||
this.appender.start()
|
this.appender.start()
|
||||||
@ -141,6 +155,8 @@ export class MiaoConsole extends interfaces.Plugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private LOGBACK_APPENDER_NAME = "NashornProxyAppender"
|
||||||
|
|
||||||
@enable({ servers: [constants.ServerType.Spring] })
|
@enable({ servers: [constants.ServerType.Spring] })
|
||||||
addLogbackForward() {
|
addLogbackForward() {
|
||||||
if (this.rootLogger) {
|
if (this.rootLogger) {
|
||||||
@ -149,7 +165,8 @@ export class MiaoConsole extends interfaces.Plugin {
|
|||||||
append: (logEvent) => global.eventCenter.emit('log', logEvent.getFormattedMessage())
|
append: (logEvent) => global.eventCenter.emit('log', logEvent.getFormattedMessage())
|
||||||
})
|
})
|
||||||
this.appender = new ProxyAppender()
|
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.appender.setContext(this.rootLogger.getLoggerContext())
|
||||||
this.rootLogger.addAppender(this.appender)
|
this.rootLogger.addAppender(this.appender)
|
||||||
}
|
}
|
||||||
@ -187,7 +204,7 @@ export class MiaoConsole extends interfaces.Plugin {
|
|||||||
@disable({ servers: [constants.ServerType.Spring] })
|
@disable({ servers: [constants.ServerType.Spring] })
|
||||||
removeLogbackForward() {
|
removeLogbackForward() {
|
||||||
try {
|
try {
|
||||||
this.rootLogger.detachAppender("NashornProxyAppender")
|
this.rootLogger.detachAppender(this.LOGBACK_APPENDER_NAME)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.ex(error)
|
console.ex(error)
|
||||||
}
|
}
|
||||||
@ -211,72 +228,91 @@ export class MiaoConsole extends interfaces.Plugin {
|
|||||||
client.disconnect(true)
|
client.disconnect(true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.logger.console(`§6客户端 §b${client.id} §a请求连接 §4Token: §c********`)
|
|
||||||
if (this.token != client.handshake.query.token) {
|
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.emit('unauthorized')
|
||||||
client.disconnect(true)
|
client.disconnect(true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
this.initWebSocketClient(client)
|
||||||
|
this.logCache.forEach(msg => client.emit('log', msg))
|
||||||
this.logger.console(`§6客户端 §b${client.id} §a新建连接 ${this.rootLogger ? '启动日志转发' : '§4转发日志启动失败'}...`)
|
this.logger.console(`§6客户端 §b${client.id} §a新建连接 ${this.rootLogger ? '启动日志转发' : '§4转发日志启动失败'}...`)
|
||||||
client.on('type', (fn) => {
|
})
|
||||||
fn && fn(this.serverType)
|
}
|
||||||
client.emit('log', `Currect Server Version is ${this.server.getVersion()}`)
|
|
||||||
})
|
private initWebSocketClient(client: SocketIOSocket) {
|
||||||
client.on('command', (cmd) => {
|
client.on('type', (fn) => {
|
||||||
setTimeout(() => this.server.dispatchConsoleCommand(cmd), 0)
|
fn && fn(this.serverType)
|
||||||
client.emit('log', `§6命令: §b${cmd} §a执行成功!`)
|
client.emit('log', `Currect Server Version is ${this.server.getVersion()}`)
|
||||||
})
|
})
|
||||||
client.on('exec', (code) => {
|
client.on('command', (cmd) => {
|
||||||
try {
|
setTimeout(() => this.server.dispatchConsoleCommand(cmd), 0)
|
||||||
client.emit('log', this.runCode(code, namespace, client))
|
client.emit('log', `§6命令: §b${cmd} §a执行成功!`)
|
||||||
} catch (ex) {
|
})
|
||||||
client.emit('log', `§4代码执行异常 错误: ${ex}\n${console.stack(ex).join('\n')}`)
|
client.on('exec', (code) => {
|
||||||
}
|
try {
|
||||||
})
|
client.emit('log', this.runCode(code, client.nsp, client))
|
||||||
client.on('edit', (file: string, fn) => {
|
} catch (ex) {
|
||||||
fn && fn(base.read(file), suffixMap[file.split('.', 2)[1]])
|
client.emit('log', `§4代码执行异常 错误: ${ex}\n${console.stack(ex).join('\n')}`)
|
||||||
})
|
}
|
||||||
client.on('save', (name: string, content: string, fn) => {
|
})
|
||||||
this.logger.console(`§6客户端 §b${client.id} §6请求更新插件 §a${name} §6...`)
|
client.on('edit', (file: string, fn) => {
|
||||||
let file = fs.concat(root, this.pluginFolder, name + '.js')
|
fn && fn(base.read(file), suffixMap[file.split('.', 2)[1]])
|
||||||
if (!fs.exists(file)) { return fn('§6插件 §a' + name + ' §6尚未安装 §c请先创建空文件 或安装插件!') }
|
})
|
||||||
try {
|
client.on('save', (name: string, content: string, fn) => {
|
||||||
base.save(file, content)
|
this.logger.console(`§6客户端 §b${client.id} §6请求更新插件 §a${name} §6...`)
|
||||||
this.pluginManager.reload(name)
|
let file = fs.concat(root, this.pluginFolder, name + '.js')
|
||||||
fn('§6插件 §a' + name + ' §6更新成功!')
|
if (!fs.exists(file)) { return fn('§6插件 §a' + name + ' §6尚未安装 §c请先创建空文件 或安装插件!') }
|
||||||
} catch (error) {
|
try {
|
||||||
this.logger.error(error)
|
base.save(file, content)
|
||||||
fn('§6插件 §a' + name + ' §4更新异常 错误: ' + error)
|
this.pluginManager.reload(name)
|
||||||
}
|
fn('§6插件 §a' + name + ' §6更新成功!')
|
||||||
})
|
} catch (error) {
|
||||||
client.on('ls', (file: string, fn) => {
|
|
||||||
let dir = fs.file(file);
|
|
||||||
if (!dir.isDirectory()) {
|
|
||||||
return fn(undefined, `${file} 不是一个目录!`)
|
|
||||||
}
|
|
||||||
fn(fs.list(dir))
|
|
||||||
})
|
|
||||||
client.on('error', (error) => {
|
|
||||||
this.logger.console(`§6客户端 §b${client.id} §c触发异常: ${error}`)
|
|
||||||
this.logger.error(error)
|
this.logger.error(error)
|
||||||
})
|
fn('§6插件 §a' + name + ' §4更新异常 错误: ' + error)
|
||||||
client.on('disconnect', () => {
|
}
|
||||||
this.logger.console(`§6客户端 §b${client.id} §c断开连接...`)
|
})
|
||||||
})
|
client.on('ls', (file: string, fn) => {
|
||||||
|
let dir = fs.file(file);
|
||||||
|
if (!dir.isDirectory()) {
|
||||||
|
return fn(undefined, `${file} 不是一个目录!`)
|
||||||
|
}
|
||||||
|
fn(fs.list(dir))
|
||||||
|
})
|
||||||
|
client.on('error', (error) => {
|
||||||
|
this.logger.console(`§6客户端 §b${client.id} §c触发异常: ${error}`)
|
||||||
|
this.logger.error(error)
|
||||||
|
})
|
||||||
|
client.on('disconnect', () => {
|
||||||
|
this.logger.console(`§6客户端 §b${client.id} §c断开连接...`)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private runCode(code: string, namespace: any, client: any) {
|
private runCode(code: string, namespace: any, client: any) {
|
||||||
let tfunc = new Function('namespace', 'client', `
|
let paramNames = [
|
||||||
var reflect = require('@ccms/common/dist/reflect');
|
'namespace',
|
||||||
var tempconcent = '';
|
'client',
|
||||||
function print(text) {
|
'reflect',
|
||||||
tempconcent += text + "\\n"
|
'container',
|
||||||
}
|
'pluginManager'
|
||||||
var result = eval(${JSON.stringify(code)});
|
]
|
||||||
return tempconcent + '§a返回结果: §r'+ result
|
let params = [
|
||||||
`)
|
namespace,
|
||||||
return this.task.callSyncMethod(() => tfunc.apply(this, [namespace, client])) + ''
|
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()
|
||||||
|
}
|
||||||
|
return '§a返回结果: §r'+ eval(${JSON.stringify(code)});
|
||||||
|
`)
|
||||||
|
return this.task.callSyncMethod(() => tfunc.apply(this, params)) + ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user