100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Logging config enhancements for FileHandler (#2279)
Rich Turnerb045f84eeb9f
diff --git a/container/src/main/java/org/openremote/container/Container.java b/container/src/main/java/org/openremote/container/Container.javaindex 17fd8c65c..f535f7417 100644--- a/container/src/main/java/org/openremote/container/Container.java+++ b/container/src/main/java/org/openremote/container/Container.java@@ -117,6 +117,9 @@ public class Container implements org.openremote.model.Container {SCHEDULED_EXECUTOR = new ContainerScheduledExecutor("ContainerScheduledExecutor", scheduledExecutorThreads);EXECUTOR = new ThreadPoolExecutor(executorThreadsMin, executorThreadsMax, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ContainerThreadFactory("ContainerExecutor"), new ThreadPoolExecutor.CallerRunsPolicy());+ LOG.log(INFO, EXECUTOR);+ LOG.log(INFO, SCHEDULED_EXECUTOR);+if (meterRegistry != null) {SCHEDULED_EXECUTOR = ExecutorServiceMetrics.monitor(meterRegistry, SCHEDULED_EXECUTOR, "ContainerScheduledExecutor");EXECUTOR = ExecutorServiceMetrics.monitor(meterRegistry, EXECUTOR, "ContainerExecutor");diff --git a/container/src/main/java/org/openremote/container/util/LogUtil.java b/container/src/main/java/org/openremote/container/util/LogUtil.javaindex 3d7c7f03b..6bea6cb1d 100644--- a/container/src/main/java/org/openremote/container/util/LogUtil.java+++ b/container/src/main/java/org/openremote/container/util/LogUtil.java@@ -19,6 +19,7 @@*/package org.openremote.container.util;+import org.openremote.container.persistence.PersistenceService;import org.openremote.model.util.Config;import org.openremote.model.util.TextUtil;@@ -30,6 +31,7 @@ import java.nio.file.Paths;import java.util.Locale;import java.util.logging.LogManager;+import static java.lang.System.Logger.Level.INFO;import static org.openremote.model.util.Config.OR_DEV_MODE;/**@@ -47,16 +49,42 @@ public class LogUtil {protected static final System.Logger LOG = System.getLogger(LogUtil.class.getName());public static final String OR_LOGGING_CONFIG_FILE = "OR_LOGGING_CONFIG_FILE";+ public static final String OR_LOGGING_DIR = "OR_LOGGING_DIR";+ public static final String OR_LOGGING_DIR_DEFAULT = "logs";+ public static final String OR_LOGGING_PROPERTY_NAME = "or.logging.dir";public static void initialiseJUL() throws ExceptionInInitializerError {+ // Ensure the standard logging directory exists as JUL FileHandler will not create it+ String loggingDir = System.getenv(OR_LOGGING_DIR);+ Path loggingDirPath;+ if (TextUtil.isNullOrEmpty(loggingDir)) {+ String storageDir = System.getenv(PersistenceService.OR_STORAGE_DIR);+ if (TextUtil.isNullOrEmpty(storageDir)) {+ storageDir = PersistenceService.OR_STORAGE_DIR_DEFAULT;+ }+ loggingDirPath = Paths.get(storageDir, OR_LOGGING_DIR_DEFAULT);+ } else {+ loggingDirPath = Paths.get(loggingDir);+ }+ try {+ Files.createDirectories(loggingDirPath);+ LOG.log(INFO, "OR_LOGGING_DIR: " + loggingDirPath);+ } catch (IOException e) {+ LOG.log(INFO, "Failed to create OR_LOGGING_DIR: " + loggingDirPath, e);+ }+ // Set the path as system property so it can be interpolated in logging properties file+ if (TextUtil.isNullOrEmpty(System.getProperty(OR_LOGGING_PROPERTY_NAME))) {+ System.setProperty(OR_LOGGING_PROPERTY_NAME, loggingDirPath.toString());+ }+// Don't do anything if standard JUL system properties setif (!TextUtil.isNullOrEmpty(System.getProperty("java.util.logging.config.class"))) {- LOG.log(System.Logger.Level.INFO,"Using specified java.util.logging.config.class system property: " + System.getProperty("java.util.logging.config.class"));+ LOG.log(INFO,"Using specified java.util.logging.config.class system property: " + System.getProperty("java.util.logging.config.class"));return;}if (!TextUtil.isNullOrEmpty(System.getProperty("java.util.logging.config.file"))) {- LOG.log(System.Logger.Level.INFO,"Using specified java.util.logging.config.file system property: " + System.getProperty("java.util.logging.config.file"));+ LOG.log(INFO,"Using specified java.util.logging.config.file system property: " + System.getProperty("java.util.logging.config.file"));return;}@@ -78,14 +106,14 @@ public class LogUtil {InputStream configFile = getFileInputStream(Config.getString(OR_LOGGING_CONFIG_FILE, null));if (configFile != null) {- LOG.log(System.Logger.Level.INFO, "Using logging configuration: " + System.getenv(OR_LOGGING_CONFIG_FILE));+ LOG.log(INFO, "Using logging configuration: " + System.getenv(OR_LOGGING_CONFIG_FILE));return configFile;}// Look for the file on the classpathconfigFile = Thread.currentThread().getContextClassLoader().getResourceAsStream(System.getenv(OR_LOGGING_CONFIG_FILE));if (configFile != null) {- LOG.log(System.Logger.Level.INFO,"Using logging configuration from classpath: " + System.getenv(OR_LOGGING_CONFIG_FILE));+ LOG.log(INFO, "Using logging configuration from classpath: " + System.getenv(OR_LOGGING_CONFIG_FILE));return configFile;}}@@ -97,7 +125,7 @@ public class LogUtil {InputStream configFile = org.openremote.model.Container.class.getClassLoader().getResourceAsStream(loggingFile);if (configFile != null) {- LOG.log(System.Logger.Level.INFO,"Using built in logging configuration from classpath: " + loggingFile);+ LOG.log(INFO, "Using built in logging configuration from classpath: " + loggingFile);return configFile;}diff --git a/container/src/main/java/org/openremote/container/util/LoggingFileHandler.java b/container/src/main/java/org/openremote/container/util/LoggingFileHandler.javanew file mode 100644index 000000000..65f03d681--- /dev/null+++ b/container/src/main/java/org/openremote/container/util/LoggingFileHandler.java@@ -0,0 +1,40 @@+/*+ * Copyright 2025, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.container.util;++import java.io.IOException;+import java.util.logging.FileHandler;+import java.util.logging.LogManager;++/**+ * This {@link FileHandler} allows injection of system property into the pattern+ */+public class LoggingFileHandler extends FileHandler {++ private static String pattern() throws IOException {+ String prefix = LoggingFileHandler.class.getName();+ String v = LogManager.getLogManager().getProperty(prefix +".pattern");+ return v.replace("${" + LogUtil.OR_LOGGING_PROPERTY_NAME + "}", System.getProperty(LogUtil.OR_LOGGING_PROPERTY_NAME));+ }++ public LoggingFileHandler() throws IOException {+ super(pattern());+ }+}diff --git a/manager/src/main/java/org/openremote/manager/Main.java b/manager/src/main/java/org/openremote/manager/Main.javaindex 8914f1f89..428b0ebc2 100644--- a/manager/src/main/java/org/openremote/manager/Main.java+++ b/manager/src/main/java/org/openremote/manager/Main.java@@ -29,13 +29,15 @@ public class Main {}public static void main(String[] args) throws Exception {-- Container container = new Container();+ Container container = null;try {+ container = new Container();container.startBackground();} catch (Exception e) {- container.stop();+ if (container != null) {+ container.stop();+ }System.exit(1);}}diff --git a/manager/src/main/resources/logging-debug.properties b/manager/src/main/resources/logging-debug.propertiesindex 34a52bd43..258a21797 100644--- a/manager/src/main/resources/logging-debug.properties+++ b/manager/src/main/resources/logging-debug.properties@@ -1,7 +1,7 @@# Log on STDOUT, filesystem and internal syslog service (monitoring in Manager UI)handlers=java.util.logging.ConsoleHandler, \org.openremote.manager.syslog.SyslogService, \- java.util.logging.FileHandler+ org.openremote.container.util.LoggingFileHandler################################################################################ Console handler@@ -9,7 +9,7 @@ handlers=java.util.logging.ConsoleHandler, \java.util.logging.ConsoleHandler.level=FINE-# Console format, you can colorize this in IntelliJ IDEA "Run Application " console+# Console format, you can colorize this in IntelliJ IDEA "Run Application" console# with the GrepConsole plugin and after copying the configuration file of your# choice to ~/Library/Preferences/IntelliJ*/options/GrepConsole.xml:#@@ -23,17 +23,17 @@ java.util.logging.ConsoleHandler.formatter=org.openremote.container.util.NoTimeL# File handler###############################################################################-java.util.logging.FileHandler.level=ALL+org.openremote.container.util.LoggingFileHandler.level=ALL# Output file and format-java.util.logging.FileHandler.pattern=/storage/logs/openremote.log-java.util.logging.FileHandler.formatter=org.openremote.container.util.LogFormatter+org.openremote.container.util.LoggingFileHandler.pattern=${or.logging.dir}/openremote.log+org.openremote.container.util.LoggingFileHandler.formatter=org.openremote.container.util.LogFormatter# Rotate between 30 log files-java.util.logging.FileHandler.count=30+org.openremote.container.util.LoggingFileHandler.count=30# 10 mb per file-java.util.logging.FileHandler.limit=10000000+org.openremote.container.util.LoggingFileHandler.limit=10000000# Set to false to clear log on restart-java.util.logging.FileHandler.append=true+org.openremote.container.util.LoggingFileHandler.append=true################################################################################ Categories and levelsdiff --git a/manager/src/main/resources/logging.properties b/manager/src/main/resources/logging.propertiesindex 4a67b037f..fd3a0e09d 100644--- a/manager/src/main/resources/logging.properties+++ b/manager/src/main/resources/logging.properties@@ -1,7 +1,7 @@# Log on STDOUT, filesystem and internal syslog service (monitoring in Manager UI)handlers=java.util.logging.ConsoleHandler, \org.openremote.manager.syslog.SyslogService, \- java.util.logging.FileHandler+ org.openremote.container.util.LoggingFileHandler################################################################################ Console handler@@ -23,17 +23,17 @@ java.util.logging.ConsoleHandler.formatter=org.openremote.container.util.NoTimeL# File handler###############################################################################-java.util.logging.FileHandler.level=ALL+org.openremote.container.util.LoggingFileHandler.level=ALL# Output file and format-java.util.logging.FileHandler.pattern=/storage/logs/openremote.log-java.util.logging.FileHandler.formatter=org.openremote.container.util.LogFormatter+org.openremote.container.util.LoggingFileHandler.pattern=${or.logging.dir}/openremote.log+org.openremote.container.util.LoggingFileHandler.formatter=org.openremote.container.util.LogFormatter# Rotate between 30 log files-java.util.logging.FileHandler.count=30+org.openremote.container.util.LoggingFileHandler.count=30# 10 mb per file-java.util.logging.FileHandler.limit=10000000+org.openremote.container.util.LoggingFileHandler.limit=10000000# Set to false to clear log on restart-java.util.logging.FileHandler.append=true+org.openremote.container.util.LoggingFileHandler.append=true################################################################################ Categories and levels