From 8a1b9d94146c8d771ac68a29f8626a360664704f Mon Sep 17 00:00:00 2001 From: rusefillc Date: Sat, 12 Feb 2022 16:46:08 -0500 Subject: [PATCH] Console logging is a mess #3930 --- .../com/rusefi/autoupdate/Autoupdate.java | 2 +- .../java/com/rusefi/io/tcp/TcpConnector.java | 8 ++++++- .../devexperts/logging/DefaultLogging.java | 5 +++-- .../java/com/devexperts/logging/Logging.java | 2 +- .../src/main/java/com/rusefi/FileLog.java | 9 +------- .../src/main/java/com/rusefi/ConsoleUI.java | 3 +++ .../rusefi/maintenance/VersionChecker.java | 10 ++++++--- .../com/devexperts/logging/Log4j2Logging.java | 21 ++++++++++++++----- 8 files changed, 39 insertions(+), 21 deletions(-) diff --git a/java_console/autoupdate/src/main/java/com/rusefi/autoupdate/Autoupdate.java b/java_console/autoupdate/src/main/java/com/rusefi/autoupdate/Autoupdate.java index d19efd5e45..ecab859220 100644 --- a/java_console/autoupdate/src/main/java/com/rusefi/autoupdate/Autoupdate.java +++ b/java_console/autoupdate/src/main/java/com/rusefi/autoupdate/Autoupdate.java @@ -21,7 +21,7 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; 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 AUTOUPDATE_MODE = "autoupdate"; private static final String RUSEFI_CONSOLE_JAR = "rusefi_console.jar"; diff --git a/java_console/io/src/main/java/com/rusefi/io/tcp/TcpConnector.java b/java_console/io/src/main/java/com/rusefi/io/tcp/TcpConnector.java index d9d14640cd..35fe040be5 100644 --- a/java_console/io/src/main/java/com/rusefi/io/tcp/TcpConnector.java +++ b/java_console/io/src/main/java/com/rusefi/io/tcp/TcpConnector.java @@ -1,16 +1,22 @@ package com.rusefi.io.tcp; +import com.devexperts.logging.Logging; + import java.io.IOException; import java.net.Socket; import java.util.Collection; import java.util.Collections; import java.util.Date; +import static com.devexperts.logging.Logging.getLogging; + /** * @author Andrey Belomutskiy * 3/3/14 */ public class TcpConnector { + private static final Logging log = getLogging(TcpConnector.class); + public final static int DEFAULT_PORT = 29001; public static final String LOCALHOST = "localhost"; @@ -72,7 +78,7 @@ public class TcpConnector { s.close(); return true; } 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; } } diff --git a/java_console/logging-api/src/main/java/com/devexperts/logging/DefaultLogging.java b/java_console/logging-api/src/main/java/com/devexperts/logging/DefaultLogging.java index dc1da4a11d..b7acfed7bc 100644 --- a/java_console/logging-api/src/main/java/com/devexperts/logging/DefaultLogging.java +++ b/java_console/logging-api/src/main/java/com/devexperts/logging/DefaultLogging.java @@ -49,9 +49,10 @@ class DefaultLogging { handler.getLevel() == Level.INFO; } - void configureLogFileAndConsole(String log_file) { - configureLogFile(log_file); + Map configureLogFileAndConsole(String log_file) { + Map result = configureLogFile(log_file); initAndAdd(new ConsoleHandler(), Level.ALL, getRootLogger()); + return result; } Map configureLogFile(String log_file) { diff --git a/java_console/logging-api/src/main/java/com/devexperts/logging/Logging.java b/java_console/logging-api/src/main/java/com/devexperts/logging/Logging.java index a082f7a7b8..10afe1c54f 100644 --- a/java_console/logging-api/src/main/java/com/devexperts/logging/Logging.java +++ b/java_console/logging-api/src/main/java/com/devexperts/logging/Logging.java @@ -65,7 +65,7 @@ public class Logging { * overrides the value of {@link #LOG_FILE_PROPERTY} system property. */ public static void configureLogFile(String log_file) { - reportErrors(IMPL, IMPL.configureLogFile(log_file)); + reportErrors(IMPL, IMPL.configureLogFileAndConsole(log_file)); } // ========== Instance ========= diff --git a/java_console/logging/src/main/java/com/rusefi/FileLog.java b/java_console/logging/src/main/java/com/rusefi/FileLog.java index 640fb1218a..119fd18e1a 100644 --- a/java_console/logging/src/main/java/com/rusefi/FileLog.java +++ b/java_console/logging/src/main/java/com/rusefi/FileLog.java @@ -47,8 +47,6 @@ public enum FileLog { return; } new Thread(FileLog::writeReadmeFile).start(); - // a bit strange spot for this invocation for sure - printOsInfo(); } private static void writeReadmeFile() { @@ -61,12 +59,7 @@ public enum FileLog { } } - private static void printOsInfo() { - MAIN.logLine("OS name: " + getOsName()); - MAIN.logLine("OS version: " + System.getProperty(OS_VERSION)); - } - - private static String getOsName() { + public static String getOsName() { return System.getProperty("os.name"); } diff --git a/java_console/ui/src/main/java/com/rusefi/ConsoleUI.java b/java_console/ui/src/main/java/com/rusefi/ConsoleUI.java index 968f7051a1..662b71f05f 100644 --- a/java_console/ui/src/main/java/com/rusefi/ConsoleUI.java +++ b/java_console/ui/src/main/java/com/rusefi/ConsoleUI.java @@ -28,6 +28,7 @@ import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import java.awt.*; import java.awt.event.ActionListener; +import java.io.FileWriter; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; @@ -193,6 +194,8 @@ public class ConsoleUI { static void startUi(String[] args) throws InterruptedException, InvocationTargetException { FileLog.MAIN.start(); + log.info("OS name: " + FileLog.getOsName()); + log.info("OS version: " + System.getProperty(FileLog.OS_VERSION)); getConfig().load(); FileLog.suspendLogging = getConfig().getRoot().getBoolProperty(GaugesPanel.DISABLE_LOGS); diff --git a/java_console/ui/src/main/java/com/rusefi/maintenance/VersionChecker.java b/java_console/ui/src/main/java/com/rusefi/maintenance/VersionChecker.java index 6f75bb2292..afab5f3a42 100644 --- a/java_console/ui/src/main/java/com/rusefi/maintenance/VersionChecker.java +++ b/java_console/ui/src/main/java/com/rusefi/maintenance/VersionChecker.java @@ -1,5 +1,7 @@ package com.rusefi.maintenance; +import com.devexperts.logging.Logging; +import com.rusefi.ConsoleUI; import com.rusefi.FileLog; import com.rusefi.models.Utils; import com.rusefi.ui.util.URLLabel; @@ -14,6 +16,7 @@ import java.util.Date; import java.util.HashMap; import java.util.Map; +import static com.devexperts.logging.Logging.getLogging; import static com.rusefi.Launcher.*; import static com.rusefi.ui.storage.PersistentConfiguration.getConfig; @@ -24,6 +27,7 @@ import static com.rusefi.ui.storage.PersistentConfiguration.getConfig; * 8/10/14 */ public class VersionChecker { + private static final Logging log = getLogging(ConsoleUI.class); private static final String JAVA_CONSOLE_TAG = "java_console"; private static final String FIRMWARE_TAG = "firmware"; 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); BufferedReader s = new BufferedReader(new InputStreamReader(url.openStream())); - FileLog.MAIN.logLine("Reading from " + VERSIONS_URL); + log.info("Reading from " + VERSIONS_URL); String line; while ((line = s.readLine()) != null) { @@ -65,9 +69,9 @@ public class VersionChecker { } 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); - 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"); if (criticalUrl != null && !criticalUrl.trim().isEmpty()) { diff --git a/java_tools/proxy_server/src/main/java/com/devexperts/logging/Log4j2Logging.java b/java_tools/proxy_server/src/main/java/com/devexperts/logging/Log4j2Logging.java index e52c03f38e..822e169faa 100644 --- a/java_tools/proxy_server/src/main/java/com/devexperts/logging/Log4j2Logging.java +++ b/java_tools/proxy_server/src/main/java/com/devexperts/logging/Log4j2Logging.java @@ -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.RollingFileAppender; 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.LoggerConfig; import org.apache.logging.log4j.core.config.NullConfiguration; import org.apache.logging.log4j.core.filter.ThresholdFilter; import org.apache.logging.log4j.core.layout.AbstractStringLayout; @@ -54,7 +56,7 @@ class Log4j2Logging extends DefaultLogging { LoggerContext ctx = (LoggerContext)LogManager.getContext(false); if (ctx.getConfiguration().getConfigurationSource() != NULL_SOURCE) 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 reconfigure(String logFile) { @@ -73,7 +75,7 @@ class Log4j2Logging extends DefaultLogging { Appender appender = null; if (logFile != null) { 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) { errors.put(logFile, e); } @@ -126,18 +128,27 @@ class Log4j2Logging extends DefaultLogging { } @Override - void configureLogFileAndConsole(String logFile) { - reconfigure(logFile); + Map configureLogFileAndConsole(String logFile) { + Map result = reconfigure(logFile); LoggerContext ctx = (LoggerContext)LogManager.getContext(false); Configuration config = ctx.getConfiguration(); Appender appender = ConsoleAppender.newBuilder() - .withName("common") + .withName("common-console") .withLayout(getDetailedLayout()) .setTarget(ConsoleAppender.Target.SYSTEM_OUT) .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); + ctx.updateLoggers(); + return result; } @Override