From 080c289a8a29244bd46cf029eefabbe426ddd00b Mon Sep 17 00:00:00 2001 From: Leo Sadovsky Date: Fri, 20 Jan 2023 14:29:41 +0300 Subject: Added the config parameter `logger.rotate.interval = ['day', 'hour']` that regulates the rotation frequency. The default value is 'day' for backwards compatibility --- src/main/java/org/traccar/config/Keys.java | 11 +++++++++++ src/main/java/org/traccar/helper/Log.java | 20 +++++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index 26e922c12..19e4540ee 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -1574,6 +1574,17 @@ public final class Keys { "logger.rotate", List.of(KeyType.CONFIG)); + /** + * Log file rotation interval, the default rotation interval is once a day. + * This option is ignored if 'logger.rotate' = false + * Available options: day, hour + */ + public static final ConfigKey LOGGER_ROTATE_INTERVAL = new StringConfigKey( + "logger.rotate.interval", + List.of(KeyType.CONFIG), + "day"); + + /** * A list of position attributes to log. */ diff --git a/src/main/java/org/traccar/helper/Log.java b/src/main/java/org/traccar/helper/Log.java index e1b201f9f..a8bf66827 100644 --- a/src/main/java/org/traccar/helper/Log.java +++ b/src/main/java/org/traccar/helper/Log.java @@ -56,18 +56,22 @@ public final class Log { private Writer writer; private final boolean rotate; - RollingFileHandler(String name, boolean rotate) { + private final String logFileSuffix; + + RollingFileHandler(String name, boolean rotate, String rotateInterval) { this.name = name; this.rotate = rotate; + this.logFileSuffix = rotateInterval.equalsIgnoreCase("HOUR") ? "yyyyMMddHH" : "yyyyMMdd"; } + @Override public synchronized void publish(LogRecord record) { if (isLoggable(record)) { try { String suffix = ""; if (rotate) { - suffix = new SimpleDateFormat("yyyyMMdd").format(new Date(record.getMillis())); + suffix = new SimpleDateFormat(this.logFileSuffix).format(new Date(record.getMillis())); if (writer != null && !suffix.equals(this.suffix)) { writer.close(); writer = null; @@ -169,7 +173,7 @@ public final class Log { public static void setupDefaultLogger() { String path = null; - URL url = ClassLoader.getSystemClassLoader().getResource("."); + URL url = ClassLoader.getSystemClassLoader().getResource("."); if (url != null) { File jarPath = new File(url.getPath()); File logsPath = new File(jarPath, "logs"); @@ -178,7 +182,7 @@ public final class Log { } path = new File(logsPath, "tracker-server.log").getPath(); } - setupLogger(path == null, path, Level.WARNING.getName(), false, true); + setupLogger(path == null, path, Level.WARNING.getName(), false, true, "DAY"); } public static void setupLogger(Config config) { @@ -187,11 +191,13 @@ public final class Log { config.getString(Keys.LOGGER_FILE), config.getString(Keys.LOGGER_LEVEL), config.getBoolean(Keys.LOGGER_FULL_STACK_TRACES), - config.getBoolean(Keys.LOGGER_ROTATE)); + config.getBoolean(Keys.LOGGER_ROTATE), + config.getString(Keys.LOGGER_ROTATE_INTERVAL)); } private static void setupLogger( - boolean console, String file, String levelString, boolean fullStackTraces, boolean rotate) { + boolean console, String file, String levelString, + boolean fullStackTraces, boolean rotate, String rotateInterval) { Logger rootLogger = Logger.getLogger(""); for (Handler handler : rootLogger.getHandlers()) { @@ -202,7 +208,7 @@ public final class Log { if (console) { handler = new ConsoleHandler(); } else { - handler = new RollingFileHandler(file, rotate); + handler = new RollingFileHandler(file, rotate, rotateInterval); } handler.setFormatter(new LogFormatter(fullStackTraces)); -- cgit v1.2.3 From 1d0d5661ca921a08217f23d4889f530643327847 Mon Sep 17 00:00:00 2001 From: Leo Sadovsky Date: Thu, 26 Jan 2023 20:34:12 +0300 Subject: worked out the review comments: code formatting and naming issues --- setup/default.xml | 1 - src/main/java/org/traccar/config/Keys.java | 1 - src/main/java/org/traccar/helper/Log.java | 8 +++----- 3 files changed, 3 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/setup/default.xml b/setup/default.xml index f2ca45fc3..c00d29384 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -24,7 +24,6 @@ info ./logs/tracker-server.log true - day true 86400 diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index 19e4540ee..093f4298c 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -1584,7 +1584,6 @@ public final class Keys { List.of(KeyType.CONFIG), "day"); - /** * A list of position attributes to log. */ diff --git a/src/main/java/org/traccar/helper/Log.java b/src/main/java/org/traccar/helper/Log.java index a8bf66827..9aaf1cfd3 100644 --- a/src/main/java/org/traccar/helper/Log.java +++ b/src/main/java/org/traccar/helper/Log.java @@ -55,23 +55,21 @@ public final class Log { private String suffix; private Writer writer; private final boolean rotate; - - private final String logFileSuffix; + private final String template; RollingFileHandler(String name, boolean rotate, String rotateInterval) { this.name = name; this.rotate = rotate; - this.logFileSuffix = rotateInterval.equalsIgnoreCase("HOUR") ? "yyyyMMddHH" : "yyyyMMdd"; + this.template = rotateInterval.equalsIgnoreCase("HOUR") ? "yyyyMMddHH" : "yyyyMMdd"; } - @Override public synchronized void publish(LogRecord record) { if (isLoggable(record)) { try { String suffix = ""; if (rotate) { - suffix = new SimpleDateFormat(this.logFileSuffix).format(new Date(record.getMillis())); + suffix = new SimpleDateFormat(template).format(new Date(record.getMillis())); if (writer != null && !suffix.equals(this.suffix)) { writer.close(); writer = null; -- cgit v1.2.3