feat: support log forward to client

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-05-13 18:42:41 +08:00
parent 430edacd3f
commit 321cb18cb9

View File

@ -7,6 +7,7 @@ import { plugin, interfaces, cmd } from '@ccms/plugin'
import { inject, ContainerInstance, Container } from '@ccms/container' import { inject, ContainerInstance, Container } from '@ccms/container'
import io, { Server as SocketIOServer, Socket as SocketIOSocket } from '@ccms/websocket' import io, { Server as SocketIOServer, Socket as SocketIOSocket } from '@ccms/websocket'
import * as fs from '@ccms/common/dist/fs' import * as fs from '@ccms/common/dist/fs'
import * as reflect from '@ccms/common/dist/reflect'
const suffixMap = { const suffixMap = {
ts: 'typescript', ts: 'typescript',
@ -31,6 +32,8 @@ export class MiaoConsole extends interfaces.Plugin {
private pipeline: any; private pipeline: any;
private socketIOServer: SocketIOServer; private socketIOServer: SocketIOServer;
private rootLogger: any;
private tempAppender: any;
@cmd() @cmd()
mconsole(sender: any, command: string, args: string[]) { mconsole(sender: any, command: string, args: string[]) {
@ -56,6 +59,28 @@ export class MiaoConsole extends interfaces.Plugin {
if (count > 30) { wait.cancel() } if (count > 30) { wait.cancel() }
count++ count++
}).later(20).timer(40).submit() }).later(20).timer(40).submit()
try {
let server = Java.type('net.minecraft.server.v1_12_R1.MinecraftServer');
this.rootLogger = server.LOGGER.parent;
} catch (error) {
try {
this.rootLogger = reflect.on(org.spongepowered.api.Sponge.getServer()).get('field_147145_h').get();
this.rootLogger = this.rootLogger.parent.parent;
} catch (ex) {
console.error('§6初始化日志代理器失败 §4错误: §c' + ex)
console.ex(ex);
}
}
if (this.rootLogger) {
let AbstractAppender = Java.type('org.apache.logging.log4j.core.appender.AbstractAppender');
let ProxyAppender = Java.extend(AbstractAppender, {
append: (logEvent) => global.eventCenter.emit('log', logEvent.getMessage().getFormattedMessage())
})
this.tempAppender = new ProxyAppender("ProxyLogger", null, null)
this.tempAppender.start();
this.rootLogger.addAppender(this.tempAppender);
this.rootLogger.setAdditive(true);
}
} }
disable() { disable() {
@ -63,6 +88,12 @@ export class MiaoConsole extends interfaces.Plugin {
if (this.container.isBound(io.Instance)) { if (this.container.isBound(io.Instance)) {
this.container.unbind(io.Instance) this.container.unbind(io.Instance)
} }
try {
this.tempAppender.stop();
this.rootLogger.removeAppender(this.tempAppender);
} catch (error) {
console.ex(error);
}
} }
createSocketIOServer() { createSocketIOServer() {
@ -76,6 +107,7 @@ export class MiaoConsole extends interfaces.Plugin {
startSocketIOServer() { startSocketIOServer() {
let namespace = this.socketIOServer.of('/MiaoConsole') let namespace = this.socketIOServer.of('/MiaoConsole')
namespace.on('connect', (client: SocketIOSocket) => { namespace.on('connect', (client: SocketIOSocket) => {
global.eventCenter.on('log', (msg) => client.emit('log', msg))
this.logger.console(`§6客户端 §b${client.id} §a新建连接...`) this.logger.console(`§6客户端 §b${client.id} §a新建连接...`)
client.on('type', (fn) => { client.on('type', (fn) => {
fn && fn(this.serverType) fn && fn(this.serverType)
@ -106,7 +138,6 @@ export class MiaoConsole extends interfaces.Plugin {
} catch (error) { } catch (error) {
this.logger.error(error) this.logger.error(error)
fn('§6插件 §a' + name + ' §4更新异常 错误: ' + error) fn('§6插件 §a' + name + ' §4更新异常 错误: ' + error)
} }
}) })
client.on('error', (error) => { client.on('error', (error) => {