mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-22 16:41:46 +03:00
[FEATURE] LogAppender
This commit is contained in:
parent
7a1746281d
commit
c2b499b2a9
3 changed files with 77 additions and 2 deletions
|
@ -0,0 +1,70 @@
|
|||
package pro.gravit.launchserver.config.log4j;
|
||||
|
||||
import org.apache.logging.log4j.core.Filter;
|
||||
import org.apache.logging.log4j.core.Layout;
|
||||
import org.apache.logging.log4j.core.LogEvent;
|
||||
import org.apache.logging.log4j.core.appender.AbstractAppender;
|
||||
import org.apache.logging.log4j.core.appender.AppenderLoggingException;
|
||||
import org.apache.logging.log4j.core.config.Property;
|
||||
import org.apache.logging.log4j.core.config.plugins.Plugin;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginElement;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
|
||||
import org.apache.logging.log4j.core.layout.PatternLayout;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@Plugin(name="LogCollect", category="Core", elementType="appender", printObject=true)
|
||||
public class LogAppender extends AbstractAppender {
|
||||
private static volatile LogAppender INSTANCE;
|
||||
private final Set<Consumer<LogEvent>> set = new HashSet<>();
|
||||
public LogAppender(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions, Property[] properties) {
|
||||
super(name, filter, layout, ignoreExceptions, properties);
|
||||
INSTANCE = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void append(LogEvent event) {
|
||||
try {
|
||||
for(Consumer<LogEvent> consumer : set) {
|
||||
consumer.accept(event);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
if(!ignoreExceptions()) {
|
||||
throw new AppenderLoggingException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void addListener(Consumer<LogEvent> consumer) {
|
||||
set.add(consumer);
|
||||
}
|
||||
|
||||
public void removeListener(Consumer<LogEvent> consumer) {
|
||||
set.remove(consumer);
|
||||
}
|
||||
|
||||
public static LogAppender getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
@PluginFactory
|
||||
public static LogAppender createAppender(
|
||||
@PluginAttribute("name") String name,
|
||||
@PluginElement("Layout") Layout<? extends Serializable> layout,
|
||||
@PluginElement("Filter") final Filter filter,
|
||||
@PluginAttribute("otherAttribute") String otherAttribute) {
|
||||
if (name == null) {
|
||||
LOGGER.error("No name provided for MyCustomAppenderImpl");
|
||||
return null;
|
||||
}
|
||||
if (layout == null) {
|
||||
layout = PatternLayout.createDefaultLayout();
|
||||
}
|
||||
return new LogAppender(name, filter, layout, true, Property.EMPTY_ARRAY);
|
||||
}
|
||||
}
|
|
@ -55,7 +55,7 @@ protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) thro
|
|||
try {
|
||||
pair.callback.handle(ctx, msg, context);
|
||||
} catch (Throwable e) {
|
||||
logger.error(e);
|
||||
logger.error("WebAPI Error", e);
|
||||
ctx.writeAndFlush(new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer("Internal Server Error 500".getBytes())), ctx.voidPromise());
|
||||
}
|
||||
isNext = false;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="INFO">
|
||||
<Configuration status="INFO" packages="pro.gravit.launchserver.config.log4j">
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d{HH:mm:ss.SSS} %highlight{[%-5level]}{FATAL=red, ERROR=red, WARN=yellow, INFO=default, DEBUG=green, TRACE=blue} %highlight{%msg}{FATAL=red, ERROR=red, WARN=yellow, INFO=default, DEBUG=green, TRACE=blue}%n" />
|
||||
|
@ -24,16 +24,21 @@
|
|||
<MarkerFilter marker="JANSI" onMatch="DENY" onMismatch="NEUTRAL"/>
|
||||
</Filters>
|
||||
</File>
|
||||
<LogCollect name="logCollector">
|
||||
|
||||
</LogCollect>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="debug">
|
||||
<AppenderRef ref="Console" level="info"/>
|
||||
<AppenderRef ref="MainFile" level="info"/>
|
||||
<AppenderRef ref="logCollector" level="info"/>
|
||||
<AppenderRef ref="DebugFile" level="debug"/>
|
||||
</Root>
|
||||
<Logger name="pro.gravit" level="debug" additivity="false">
|
||||
<AppenderRef ref="Console" />
|
||||
<AppenderRef ref="MainFile"/>
|
||||
<AppenderRef ref="logCollector"/>
|
||||
<AppenderRef ref="DebugFile"/>
|
||||
</Logger>
|
||||
</Loggers>
|
||||
|
|
Loading…
Reference in a new issue