Console logging is a mess #3930

This commit is contained in:
rusefillc 2022-02-12 16:46:08 -05:00
parent e6bb6dbecc
commit 8a1b9d9414
8 changed files with 39 additions and 21 deletions

View File

@ -21,7 +21,7 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
public class Autoupdate { public class Autoupdate {
private static final String TITLE = "rusEFI Bundle Updater 20200607"; private static final String TITLE = "rusEFI Bundle Updater 20210212";
private static final String BUNDLE_NAME_FILE = "../bundle_name.txt"; private static final String BUNDLE_NAME_FILE = "../bundle_name.txt";
private static final String AUTOUPDATE_MODE = "autoupdate"; private static final String AUTOUPDATE_MODE = "autoupdate";
private static final String RUSEFI_CONSOLE_JAR = "rusefi_console.jar"; private static final String RUSEFI_CONSOLE_JAR = "rusefi_console.jar";

View File

@ -1,16 +1,22 @@
package com.rusefi.io.tcp; package com.rusefi.io.tcp;
import com.devexperts.logging.Logging;
import java.io.IOException; import java.io.IOException;
import java.net.Socket; import java.net.Socket;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import static com.devexperts.logging.Logging.getLogging;
/** /**
* @author Andrey Belomutskiy * @author Andrey Belomutskiy
* 3/3/14 * 3/3/14
*/ */
public class TcpConnector { public class TcpConnector {
private static final Logging log = getLogging(TcpConnector.class);
public final static int DEFAULT_PORT = 29001; public final static int DEFAULT_PORT = 29001;
public static final String LOCALHOST = "localhost"; public static final String LOCALHOST = "localhost";
@ -72,7 +78,7 @@ public class TcpConnector {
s.close(); s.close();
return true; return true;
} catch (IOException e) { } catch (IOException e) {
System.out.println(new Date() + ": Connection refused in getAvailablePorts(): simulator not running in " + (System.currentTimeMillis() - now) + "ms"); log.info("Connection refused in getAvailablePorts(): simulator not running in " + (System.currentTimeMillis() - now) + "ms");
return false; return false;
} }
} }

View File

@ -49,9 +49,10 @@ class DefaultLogging {
handler.getLevel() == Level.INFO; handler.getLevel() == Level.INFO;
} }
void configureLogFileAndConsole(String log_file) { Map<String, Exception> configureLogFileAndConsole(String log_file) {
configureLogFile(log_file); Map<String, Exception> result = configureLogFile(log_file);
initAndAdd(new ConsoleHandler(), Level.ALL, getRootLogger()); initAndAdd(new ConsoleHandler(), Level.ALL, getRootLogger());
return result;
} }
Map<String, Exception> configureLogFile(String log_file) { Map<String, Exception> configureLogFile(String log_file) {

View File

@ -65,7 +65,7 @@ public class Logging {
* overrides the value of {@link #LOG_FILE_PROPERTY} system property. * overrides the value of {@link #LOG_FILE_PROPERTY} system property.
*/ */
public static void configureLogFile(String log_file) { public static void configureLogFile(String log_file) {
reportErrors(IMPL, IMPL.configureLogFile(log_file)); reportErrors(IMPL, IMPL.configureLogFileAndConsole(log_file));
} }
// ========== Instance ========= // ========== Instance =========

View File

@ -47,8 +47,6 @@ public enum FileLog {
return; return;
} }
new Thread(FileLog::writeReadmeFile).start(); new Thread(FileLog::writeReadmeFile).start();
// a bit strange spot for this invocation for sure
printOsInfo();
} }
private static void writeReadmeFile() { private static void writeReadmeFile() {
@ -61,12 +59,7 @@ public enum FileLog {
} }
} }
private static void printOsInfo() { public static String getOsName() {
MAIN.logLine("OS name: " + getOsName());
MAIN.logLine("OS version: " + System.getProperty(OS_VERSION));
}
private static String getOsName() {
return System.getProperty("os.name"); return System.getProperty("os.name");
} }

View File

@ -28,6 +28,7 @@ import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener; import javax.swing.event.ChangeListener;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -193,6 +194,8 @@ public class ConsoleUI {
static void startUi(String[] args) throws InterruptedException, InvocationTargetException { static void startUi(String[] args) throws InterruptedException, InvocationTargetException {
FileLog.MAIN.start(); FileLog.MAIN.start();
log.info("OS name: " + FileLog.getOsName());
log.info("OS version: " + System.getProperty(FileLog.OS_VERSION));
getConfig().load(); getConfig().load();
FileLog.suspendLogging = getConfig().getRoot().getBoolProperty(GaugesPanel.DISABLE_LOGS); FileLog.suspendLogging = getConfig().getRoot().getBoolProperty(GaugesPanel.DISABLE_LOGS);

View File

@ -1,5 +1,7 @@
package com.rusefi.maintenance; package com.rusefi.maintenance;
import com.devexperts.logging.Logging;
import com.rusefi.ConsoleUI;
import com.rusefi.FileLog; import com.rusefi.FileLog;
import com.rusefi.models.Utils; import com.rusefi.models.Utils;
import com.rusefi.ui.util.URLLabel; import com.rusefi.ui.util.URLLabel;
@ -14,6 +16,7 @@ import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static com.devexperts.logging.Logging.getLogging;
import static com.rusefi.Launcher.*; import static com.rusefi.Launcher.*;
import static com.rusefi.ui.storage.PersistentConfiguration.getConfig; import static com.rusefi.ui.storage.PersistentConfiguration.getConfig;
@ -24,6 +27,7 @@ import static com.rusefi.ui.storage.PersistentConfiguration.getConfig;
* 8/10/14 * 8/10/14
*/ */
public class VersionChecker { public class VersionChecker {
private static final Logging log = getLogging(ConsoleUI.class);
private static final String JAVA_CONSOLE_TAG = "java_console"; private static final String JAVA_CONSOLE_TAG = "java_console";
private static final String FIRMWARE_TAG = "firmware"; private static final String FIRMWARE_TAG = "firmware";
private static final String VERSIONS_URL = "https://rusefi.com/console/versions.txt"; private static final String VERSIONS_URL = "https://rusefi.com/console/versions.txt";
@ -55,7 +59,7 @@ public class VersionChecker {
URL url = new URL(VERSIONS_URL); URL url = new URL(VERSIONS_URL);
BufferedReader s = new BufferedReader(new InputStreamReader(url.openStream())); BufferedReader s = new BufferedReader(new InputStreamReader(url.openStream()));
FileLog.MAIN.logLine("Reading from " + VERSIONS_URL); log.info("Reading from " + VERSIONS_URL);
String line; String line;
while ((line = s.readLine()) != null) { while ((line = s.readLine()) != null) {
@ -65,9 +69,9 @@ public class VersionChecker {
} }
final Integer javaVersion = parseNotNull(map.get(JAVA_CONSOLE_TAG), "VC value"); final Integer javaVersion = parseNotNull(map.get(JAVA_CONSOLE_TAG), "VC value");
System.out.println(new Date() + ": Server recommends java_console version " + javaVersion + " or newer"); log.info("Server recommends java_console version " + javaVersion + " or newer");
showUpdateWarningIfNeeded("dev console", javaVersion, CONSOLE_VERSION); showUpdateWarningIfNeeded("dev console", javaVersion, CONSOLE_VERSION);
System.out.println(new Date() + ": Server recommends firmware " + map.get(FIRMWARE_TAG) + " or newer"); log.info("Server recommends firmware " + map.get(FIRMWARE_TAG) + " or newer");
String criticalUrl = map.get("critical_url"); String criticalUrl = map.get("critical_url");
if (criticalUrl != null && !criticalUrl.trim().isEmpty()) { if (criticalUrl != null && !criticalUrl.trim().isEmpty()) {

View File

@ -18,7 +18,9 @@ import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.ConsoleAppender; import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.appender.RollingFileAppender; import org.apache.logging.log4j.core.appender.RollingFileAppender;
import org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy; import org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy;
import org.apache.logging.log4j.core.config.AppenderRef;
import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.config.NullConfiguration; import org.apache.logging.log4j.core.config.NullConfiguration;
import org.apache.logging.log4j.core.filter.ThresholdFilter; import org.apache.logging.log4j.core.filter.ThresholdFilter;
import org.apache.logging.log4j.core.layout.AbstractStringLayout; import org.apache.logging.log4j.core.layout.AbstractStringLayout;
@ -54,7 +56,7 @@ class Log4j2Logging extends DefaultLogging {
LoggerContext ctx = (LoggerContext)LogManager.getContext(false); LoggerContext ctx = (LoggerContext)LogManager.getContext(false);
if (ctx.getConfiguration().getConfigurationSource() != NULL_SOURCE) if (ctx.getConfiguration().getConfigurationSource() != NULL_SOURCE)
return Collections.emptyMap(); // do nothing since log4j2 was already configured return Collections.emptyMap(); // do nothing since log4j2 was already configured
return configureLogFile(getProperty(Logging.LOG_FILE_PROPERTY, null)); return configureLogFileAndConsole(getProperty(Logging.LOG_FILE_PROPERTY, null));
} }
private static Map<String, Exception> reconfigure(String logFile) { private static Map<String, Exception> reconfigure(String logFile) {
@ -73,7 +75,7 @@ class Log4j2Logging extends DefaultLogging {
Appender appender = null; Appender appender = null;
if (logFile != null) { if (logFile != null) {
try { try {
appender = createFileAppender("common", logFile, Logging.LOG_MAX_FILE_SIZE_PROPERTY, errors); appender = createFileAppender("common-file", logFile, Logging.LOG_MAX_FILE_SIZE_PROPERTY, errors);
} catch (Exception e) { } catch (Exception e) {
errors.put(logFile, e); errors.put(logFile, e);
} }
@ -126,18 +128,27 @@ class Log4j2Logging extends DefaultLogging {
} }
@Override @Override
void configureLogFileAndConsole(String logFile) { Map<String, Exception> configureLogFileAndConsole(String logFile) {
reconfigure(logFile); Map<String, Exception> result = reconfigure(logFile);
LoggerContext ctx = (LoggerContext)LogManager.getContext(false); LoggerContext ctx = (LoggerContext)LogManager.getContext(false);
Configuration config = ctx.getConfiguration(); Configuration config = ctx.getConfiguration();
Appender appender = ConsoleAppender.newBuilder() Appender appender = ConsoleAppender.newBuilder()
.withName("common") .withName("common-console")
.withLayout(getDetailedLayout()) .withLayout(getDetailedLayout())
.setTarget(ConsoleAppender.Target.SYSTEM_OUT) .setTarget(ConsoleAppender.Target.SYSTEM_OUT)
.build(); .build();
appender.start();
// broken stuff :( while this adds console appender this also kills file appender :( todo: find a way to have both
config.addAppender(appender);
AppenderRef[] refs = new AppenderRef[] { AppenderRef.createAppenderRef(appender.getName(), null, null) };
LoggerConfig loggerConfig = LoggerConfig.createLogger("true", org.apache.logging.log4j.Level.ALL, "logger-console", "true", refs, null, config, null);
loggerConfig.addAppender(appender, null, null);
config.addLogger(LogManager.ROOT_LOGGER_NAME, loggerConfig);
config.getRootLogger().addAppender(appender, DEBUG, null); config.getRootLogger().addAppender(appender, DEBUG, null);
ctx.updateLoggers();
return result;
} }
@Override @Override