Use a custom dev time log4j config. Enables color output and improves overall logging. (#186)

* Use a custom dev time log4j config. Enables color output and improves overall logging.
Color output is not enabled by default for eclipse users

* Use the run config to force enable ansi colors

* Improve IDE detection
dev/0.11
modmuss50 2020-04-04 14:18:40 +01:00 committed by GitHub
parent bb68465a8d
commit dff10a5d79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 0 deletions

View File

@ -26,8 +26,11 @@ package net.fabricmc.loom.providers;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -49,6 +52,7 @@ public class LaunchProvider extends DependencyProvider {
public void provide(DependencyInfo dependency, Consumer<Runnable> postPopulationScheduler) throws IOException {
final LaunchConfig launchConfig = new LaunchConfig()
.property("fabric.development", "true")
.property("log4j.configurationFile", getLog4jConfigFile().getAbsolutePath())
.property("client", "java.library.path", getExtension().getNativesDirectory().getAbsolutePath())
.property("client", "org.lwjgl.librarypath", getExtension().getNativesDirectory().getAbsolutePath())
@ -58,9 +62,31 @@ public class LaunchProvider extends DependencyProvider {
.argument("client", "--assetsDir")
.argument("client", new File(getExtension().getUserCache(), "assets").getAbsolutePath());
//Enable ansi by default for idea and vscode
if (new File(getProject().getRootDir(), ".vscode").exists()
|| new File(getProject().getRootDir(), ".idea").exists()
|| (Arrays.stream(getProject().getRootDir().listFiles()).anyMatch(file -> file.getName().endsWith(".iws")))) {
launchConfig.property("fabric.log.disableAnsi", "false");
}
writeLog4jConfig();
FileUtils.writeStringToFile(getExtension().getDevLauncherConfig(), launchConfig.asString(), StandardCharsets.UTF_8);
addDependency("net.fabricmc:dev-launch-injector:" + Constants.DEV_LAUNCH_INJECTOR_VERSION, "runtimeOnly");
addDependency("net.minecrell:terminalconsoleappender:" + Constants.TERMINAL_CONSOLE_APPENDER_VERSION, "runtimeOnly");
}
private File getLog4jConfigFile() {
return new File(getExtension().getDevLauncherConfig().getParentFile(), "log4j.xml");
}
private void writeLog4jConfig() {
try (InputStream is = LaunchProvider.class.getClassLoader().getResourceAsStream("log4j2.fabric.xml")) {
Files.deleteIfExists(getLog4jConfigFile().toPath());
Files.copy(is, getLog4jConfigFile().toPath());
} catch (IOException e) {
throw new RuntimeException("Failed to generate log4j config", e);
}
}
@Override

View File

@ -56,4 +56,5 @@ public class Constants {
public static final String MIXIN_COMPILE_EXTENSIONS_VERSION = "0.3.0.4";
public static final String DEV_LAUNCH_INJECTOR_VERSION = "0.2.0+build.6";
public static final String TERMINAL_CONSOLE_APPENDER_VERSION = "1.2.0";
}

View File

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" packages="com.mojang.util,net.minecrell.terminalconsole.util">
<Appenders>
<!-- System out -->
<Console name="SysOut" target="SYSTEM_OUT">
<PatternLayout>
<LoggerNamePatternSelector defaultPattern="%style{[%d{HH:mm:ss}]}{blue} %highlight{[%t/%level]}{FATAL=red, ERROR=red, WARN=yellow, INFO=green, DEBUG=green, TRACE=blue} %style{(%logger{1})}{cyan} %highlight{%msg%n}{FATAL=red, ERROR=red, WARN=normal, INFO=normal, DEBUG=normal, TRACE=normal}" disableAnsi="${sys:fabric.log.disableAnsi:-true}">
<!-- Dont show the logger name for minecraft classes-->
<PatternMatch key="net.minecraft.,com.mojang." pattern="%style{[%d{HH:mm:ss}]}{blue} %highlight{[%t/%level]}{FATAL=red, ERROR=red, WARN=yellow, INFO=green, DEBUG=green, TRACE=blue} %style{(Minecraft)}{cyan} %highlight{%msg%n}{FATAL=red, ERROR=red, WARN=normal, INFO=normal, DEBUG=normal, TRACE=normal}" disableAnsi="${sys:fabric.log.disableAnsi:-true}"/>
</LoggerNamePatternSelector>
</PatternLayout>
</Console>
<!-- Vanilla server gui -->
<Queue name="ServerGuiConsole" ignoreExceptions="true">
<PatternLayout>
<LoggerNamePatternSelector defaultPattern="[%d{HH:mm:ss} %level] (%logger{1}) %msg%n">
<!-- Dont show the logger name for minecraft classes-->
<PatternMatch key="net.minecraft.,com.mojang." pattern="[%d{HH:mm:ss} %level] %msg%n"/>
</LoggerNamePatternSelector>
</PatternLayout>
</Queue>
<!-- latest.log same as vanilla -->
<RollingRandomAccessFile name="LatestFile" fileName="logs/latest.log" filePattern="logs/%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout>
<LoggerNamePatternSelector defaultPattern="[%d{HH:mm:ss}] [%t/%level] (%logger{1}) %msg%n">
<!-- Dont show the logger name for minecraft classes-->
<PatternMatch key="net.minecraft.,com.mojang." pattern="[%d{HH:mm:ss}] [%t/%level] (Minecraft) %msg%n"/>
</LoggerNamePatternSelector>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<OnStartupTriggeringPolicy />
</Policies>
</RollingRandomAccessFile>
<!-- Debug log file -->
<RollingRandomAccessFile name="DebugFile" fileName="logs/debug.log" filePattern="logs/debug-%i.log.gz">
<PatternLayout pattern="[%d{HH:mm:ss}] [%t/%level] (%logger) %msg%n" />
<!-- Keep 5 files max -->
<DefaultRolloverStrategy max="5" fileIndex="min"/>
<Policies>
<SizeBasedTriggeringPolicy size="500MB"/>
<OnStartupTriggeringPolicy />
</Policies>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Logger level="${sys:fabric.log.level:-info}" name="net.minecraft"/>
<Root level="all">
<AppenderRef ref="DebugFile" level="${sys:fabric.log.debug.level:-debug}"/>
<AppenderRef ref="SysOut" level="${sys:fabric.log.level:-info}"/>
<AppenderRef ref="LatestFile" level="${sys:fabric.log.level:-info}"/>
<AppenderRef ref="ServerGuiConsole" level="${sys:fabric.log.level:-info}"/>
</Root>
</Loggers>
</Configuration>