mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-08-03 19:36:53 -04:00
Add default log4j config file for better performance
This commit is contained in:
parent
72a1d4f186
commit
7fe3e28a2d
@ -1,23 +0,0 @@
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="SysOut" target="SYSTEM_OUT">
|
||||
<XMLLayout/>
|
||||
</Console>
|
||||
<RollingRandomAccessFile name="File" fileName="logs/latest.log" filePattern="logs/%d{yyyy-MM-dd}-%i.log.gz">
|
||||
<PatternLayout pattern="[%d{HH:mm:ss}] [%t/%level]: %msg%n"/>
|
||||
<Policies>
|
||||
<TimeBasedTriggeringPolicy/>
|
||||
<OnStartupTriggeringPolicy/>
|
||||
</Policies>
|
||||
</RollingRandomAccessFile>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="info">
|
||||
<filters>
|
||||
<MarkerFilter marker="NETWORK_PACKETS" onMatch="DENY" onMismatch="NEUTRAL"/>
|
||||
</filters>
|
||||
<AppenderRef ref="SysOut"/>
|
||||
<AppenderRef ref="File"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
@ -20,6 +20,7 @@ package org.jackhuang.hmcl;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.Authenticator;
|
||||
import java.net.PasswordAuthentication;
|
||||
import java.security.GeneralSecurityException;
|
||||
@ -38,6 +39,7 @@ import org.jackhuang.hmcl.api.HMCLog;
|
||||
import org.jackhuang.hmcl.api.ILogger;
|
||||
import org.jackhuang.hmcl.api.PluginManager;
|
||||
import org.jackhuang.hmcl.api.VersionNumber;
|
||||
import org.jackhuang.hmcl.core.MCUtils;
|
||||
import org.jackhuang.hmcl.setting.Settings;
|
||||
import org.jackhuang.hmcl.ui.LogWindow;
|
||||
import org.jackhuang.hmcl.ui.MainFrame;
|
||||
@ -208,6 +210,12 @@ public final class Main {
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
unpackDefaultLog4jConfiguration();
|
||||
} catch(IOException e) {
|
||||
HMCLog.err("Failed to unpack log4j.xml, log window will not work well.", e);
|
||||
}
|
||||
|
||||
MainFrame.showMainFrame();
|
||||
}
|
||||
}
|
||||
@ -216,6 +224,20 @@ public final class Main {
|
||||
MainFrame.INSTANCE.invokeUpdate();
|
||||
}
|
||||
|
||||
public static final File LOG4J_FILE = new File(MCUtils.getWorkingDirectory("hmcl"), "log4j.xml");
|
||||
|
||||
public static void unpackDefaultLog4jConfiguration() throws IOException {
|
||||
LOG4J_FILE.getParentFile().mkdirs();
|
||||
if (LOG4J_FILE.exists()) return;
|
||||
LOG4J_FILE.createNewFile();
|
||||
try (InputStream is = Main.class.getResourceAsStream("/org/jackhuang/hmcl/log4j.xml");
|
||||
FileOutputStream fos = new FileOutputStream(LOG4J_FILE)) {
|
||||
int b;
|
||||
while ((b = is.read()) != -1)
|
||||
fos.write(b);
|
||||
}
|
||||
}
|
||||
|
||||
public static ImageIcon getIcon(String path) {
|
||||
try {
|
||||
return new ImageIcon(Main.class.getResource("/org/jackhuang/hmcl/" + path));
|
||||
|
@ -76,7 +76,11 @@ public class LaunchingUIDaemon {
|
||||
PipedOutputStream os = new PipedOutputStream();
|
||||
monitor.setTag(obj);
|
||||
try {
|
||||
Log4jHandler handler = new Log4jHandler(os);
|
||||
Log4jHandler handler = new Log4jHandler(monitor, os);
|
||||
handler.addForbiddenToken(obj.getLoginResult().getAccessToken(), "<access token>");
|
||||
handler.addForbiddenToken(obj.getLoginResult().getSession(), "<session>");
|
||||
handler.addForbiddenToken(obj.getLoginResult().getUserId(), "<uuid>");
|
||||
handler.addForbiddenToken("Setting user: " + obj.getLoginResult().getUserName(), "<player>");
|
||||
handler.start();
|
||||
} catch(Exception e) {
|
||||
HMCLog.err("", e);
|
||||
|
@ -41,5 +41,11 @@ public class HMCLMinecraftLoader extends MinecraftLoader {
|
||||
|
||||
list.add("-Dminecraft.launcher.version=" + Main.LAUNCHER_VERSION);
|
||||
list.add("-Dminecraft.launcher.brand=" + Main.LAUNCHER_NAME);
|
||||
|
||||
boolean flag = false;
|
||||
for (String s : list) if (s.contains("-Dlog4j.configurationFile=")) flag = true;
|
||||
if (!flag) {
|
||||
list.add("-Dlog4j.configurationFile=" + Main.LOG4J_FILE.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,9 +22,15 @@ import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import org.jackhuang.hmcl.api.HMCLApi;
|
||||
import org.jackhuang.hmcl.api.event.process.JavaProcessStoppedEvent;
|
||||
import org.jackhuang.hmcl.api.func.Consumer;
|
||||
import org.jackhuang.hmcl.ui.LogWindow;
|
||||
import org.jackhuang.hmcl.util.log.Level;
|
||||
import org.jackhuang.hmcl.util.sys.ProcessMonitor;
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
@ -36,19 +42,25 @@ import org.xml.sax.helpers.XMLReaderFactory;
|
||||
*
|
||||
* @author huang
|
||||
*/
|
||||
public class Log4jHandler extends Thread {
|
||||
public class Log4jHandler extends Thread implements Consumer<JavaProcessStoppedEvent> {
|
||||
|
||||
XMLReader reader;
|
||||
ProcessMonitor monitor;
|
||||
PipedInputStream inputStream;
|
||||
PipedOutputStream outputStream;
|
||||
List<Pair<String, String>> forbiddenTokens = new LinkedList<>();
|
||||
|
||||
public Log4jHandler(PipedOutputStream outputStream) throws ParserConfigurationException, IOException, SAXException {
|
||||
/*SAXParserFactory factory = SAXParserFactory.newInstance();
|
||||
factory.setNamespaceAware(true);
|
||||
.*/
|
||||
public Log4jHandler(ProcessMonitor monitor, PipedOutputStream outputStream) throws ParserConfigurationException, IOException, SAXException {
|
||||
reader = XMLReaderFactory.createXMLReader();
|
||||
inputStream = new PipedInputStream(outputStream);
|
||||
this.outputStream = outputStream;
|
||||
this.monitor = monitor;
|
||||
|
||||
HMCLApi.EVENT_BUS.channel(JavaProcessStoppedEvent.class).register((Consumer<JavaProcessStoppedEvent>) this);
|
||||
}
|
||||
|
||||
public void addForbiddenToken(String token, String replacement) {
|
||||
forbiddenTokens.add(new Pair<>(token, replacement));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -56,7 +68,6 @@ public class Log4jHandler extends Thread {
|
||||
try {
|
||||
outputStream.write("<output>".getBytes());
|
||||
outputStream.flush();
|
||||
//reader.parse(inputStream, new Log4jHandlerImpl());
|
||||
reader.setContentHandler(new Log4jHandlerImpl());
|
||||
reader.parse(new InputSource(inputStream));
|
||||
} catch (SAXException | IOException e) {
|
||||
@ -64,10 +75,23 @@ public class Log4jHandler extends Thread {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(JavaProcessStoppedEvent t) {
|
||||
if (t.getSource() == monitor) {
|
||||
try {
|
||||
outputStream.write("</output>".getBytes());
|
||||
outputStream.close();
|
||||
} catch (IOException ignore) { // won't happen
|
||||
throw new Error(ignore);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Log4jHandlerImpl extends DefaultHandler {
|
||||
private final SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
|
||||
|
||||
String message = "", date = "", thread = "", logger = "";
|
||||
String date = "", thread = "", logger = "";
|
||||
StringBuilder message = null;
|
||||
Level l = null;
|
||||
boolean readingMessage = false;
|
||||
|
||||
@ -75,7 +99,7 @@ public class Log4jHandler extends Thread {
|
||||
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
|
||||
switch (localName) {
|
||||
case "log4j_Event":
|
||||
message = "";
|
||||
message = new StringBuilder();
|
||||
Date d = new Date(Long.valueOf(attributes.getValue("timestamp")));
|
||||
date = df.format(d);
|
||||
try {
|
||||
@ -98,7 +122,7 @@ public class Log4jHandler extends Thread {
|
||||
public void endElement(String uri, String localName, String qName) throws SAXException {
|
||||
switch (localName) {
|
||||
case "log4j_Event":
|
||||
println("[" + date + "] [" + thread + "/" + l.name() + "] [" + logger + "] " + message, l);
|
||||
println("[" + date + "] [" + thread + "/" + l.name() + "] [" + logger + "] " + message.toString(), l);
|
||||
break;
|
||||
case "log4j_Message":
|
||||
readingMessage = false;
|
||||
@ -111,12 +135,14 @@ public class Log4jHandler extends Thread {
|
||||
String line = new String(ch, start, length);
|
||||
if (line.trim().isEmpty()) return;
|
||||
if (readingMessage)
|
||||
message += line + C.LINE_SEPARATOR;
|
||||
message.append(line).append(C.LINE_SEPARATOR);
|
||||
else
|
||||
println(line, Level.guessLevel(line));
|
||||
}
|
||||
|
||||
public void println(String message, Level l) {
|
||||
for (Pair<String, String> entry : forbiddenTokens)
|
||||
message = message.replace(entry.key, entry.value);
|
||||
if (LogWindow.outputStream != null)
|
||||
LogWindow.outputStream.log(message, l);
|
||||
}
|
||||
|
25
HMCL/src/main/resources/org/jackhuang/hmcl/log4j.xml
Normal file
25
HMCL/src/main/resources/org/jackhuang/hmcl/log4j.xml
Normal file
@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="SysOut" target="SYSTEM_OUT">
|
||||
<LegacyXMLLayout />
|
||||
<XMLLayout />
|
||||
</Console>
|
||||
<RollingRandomAccessFile name="File" fileName="logs/latest.log" filePattern="logs/%d{yyyy-MM-dd}-%i.log.gz">
|
||||
<PatternLayout pattern="[%d{HH:mm:ss}] [%t/%level]: %msg%n" />
|
||||
<Policies>
|
||||
<TimeBasedTriggeringPolicy />
|
||||
<OnStartupTriggeringPolicy />
|
||||
</Policies>
|
||||
</RollingRandomAccessFile>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="info">
|
||||
<filters>
|
||||
<MarkerFilter marker="NETWORK_PACKETS" onMatch="DENY" onMismatch="NEUTRAL" />
|
||||
</filters>
|
||||
<AppenderRef ref="SysOut"/>
|
||||
<AppenderRef ref="File"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
@ -163,7 +163,11 @@ public abstract class GameLauncher {
|
||||
}
|
||||
}
|
||||
HMCLog.log("Starting process");
|
||||
HMCLog.log(str.toString());
|
||||
String s = StrUtils.makeCommand(str);
|
||||
s = s.replace(result.getAccessToken(), "<access token>");
|
||||
s = s.replace(result.getSession(), "<session>");
|
||||
s = s.replace(result.getUserId(), "<uuid>");
|
||||
HMCLog.log(s);
|
||||
ProcessBuilder builder = new ProcessBuilder(str);
|
||||
if (options.getLaunchVersion() == null || service.baseDirectory() == null)
|
||||
throw new Error("Fucking bug!");
|
||||
|
Loading…
x
Reference in New Issue
Block a user