From c3c47ce7020ce4059da339057dce24fddb59d77e Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Thu, 12 May 2022 23:38:48 +0200 Subject: [PATCH 1/5] Add log on disk --- CHANGELOG.md | 1 + src/main/java/org/jabref/gui/JabRefMain.java | 28 ++++++++++++++++++++ src/main/resources/tinylog.properties | 2 ++ 3 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81d801e29f3..03e58d636f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We added a fetcher for [Biodiversity Heritage Library)](https://www.biodiversitylibrary.org/) [8539](https://github.com/JabRef/jabref/issues/8539) - We added support for multiple messages in the snackbar. [#7340](https://github.com/JabRef/jabref/issues/7340) +- JabRef now writes log files. Linux: `$home/.cache/jabref/logs/version`, Windows: `%APPDATA%\..\Local\harawata\jabref\version\logs`, Mac: `Users/.../Library/Logs/jabref/version` ### Changed diff --git a/src/main/java/org/jabref/gui/JabRefMain.java b/src/main/java/org/jabref/gui/JabRefMain.java index 3bc43bf7517..8ff856ee5ed 100644 --- a/src/main/java/org/jabref/gui/JabRefMain.java +++ b/src/main/java/org/jabref/gui/JabRefMain.java @@ -7,6 +7,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Comparator; +import java.util.Map; import javafx.application.Application; import javafx.application.Platform; @@ -26,15 +27,18 @@ import org.jabref.logic.protectedterms.ProtectedTermsLoader; import org.jabref.logic.remote.RemotePreferences; import org.jabref.logic.remote.client.RemoteClient; +import org.jabref.logic.util.BuildInfo; import org.jabref.migrations.PreferencesMigrations; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.database.BibDatabaseMode; import org.jabref.preferences.JabRefPreferences; import org.jabref.preferences.PreferencesService; +import net.harawata.appdirs.AppDirsFactory; import org.apache.commons.cli.ParseException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.tinylog.configuration.Configuration; /** * JabRef's main class to process command line options and to start the UI @@ -45,10 +49,34 @@ public class JabRefMain extends Application { private static String[] arguments; public static void main(String[] args) { + addLogToDisk(); arguments = args; launch(arguments); } + /** + * This needs to be called as early as possible. After the first log write, it is not possible to alter + * the log configuration programmatically anymore. + */ + private static void addLogToDisk() { + Path directory = Path.of(AppDirsFactory.getInstance().getUserLogDir( + "jabref", + new BuildInfo().version.toString(), + "org.jabref")); + try { + Files.createDirectories(directory); + } catch (IOException e) { + LOGGER.error("Could not create log directory {}", directory, e); + return; + } + Map configuration = Map.of( + "writerFile", "rolling file", + "writerFile.level", "info", + "writerFile.file", directory.resolve("log_{count}.txt").toString(), + "writerFile.latest", directory.resolve("latest.txt").toString()); + Configuration.replace(configuration); + } + @Override public void start(Stage mainStage) { try { diff --git a/src/main/resources/tinylog.properties b/src/main/resources/tinylog.properties index ae34725dfec..6878129ac7b 100644 --- a/src/main/resources/tinylog.properties +++ b/src/main/resources/tinylog.properties @@ -2,3 +2,5 @@ level = info writer = gui writer1 = console writer2 = application insights + +exception = strip: jdk.internal From 14a131cf2c3f4f65c1484d5f4819a10eafc93ddb Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Fri, 13 May 2022 08:52:30 +0200 Subject: [PATCH 2/5] Lazy load LOGGER for Version.java --- src/main/java/org/jabref/gui/JabRefMain.java | 8 +++++++- .../java/org/jabref/logic/util/Version.java | 19 +++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/jabref/gui/JabRefMain.java b/src/main/java/org/jabref/gui/JabRefMain.java index 8ff856ee5ed..8bebe52c4ba 100644 --- a/src/main/java/org/jabref/gui/JabRefMain.java +++ b/src/main/java/org/jabref/gui/JabRefMain.java @@ -44,7 +44,7 @@ * JabRef's main class to process command line options and to start the UI */ public class JabRefMain extends Application { - private static final Logger LOGGER = LoggerFactory.getLogger(JabRefMain.class); + private static Logger LOGGER; private static String[] arguments; @@ -54,6 +54,10 @@ public static void main(String[] args) { launch(arguments); } + private static void initializeLogger() { + LOGGER = LoggerFactory.getLogger(JabRefMain.class); + } + /** * This needs to be called as early as possible. After the first log write, it is not possible to alter * the log configuration programmatically anymore. @@ -66,6 +70,7 @@ private static void addLogToDisk() { try { Files.createDirectories(directory); } catch (IOException e) { + initializeLogger(); LOGGER.error("Could not create log directory {}", directory, e); return; } @@ -75,6 +80,7 @@ private static void addLogToDisk() { "writerFile.file", directory.resolve("log_{count}.txt").toString(), "writerFile.latest", directory.resolve("latest.txt").toString()); Configuration.replace(configuration); + initializeLogger(); } @Override diff --git a/src/main/java/org/jabref/logic/util/Version.java b/src/main/java/org/jabref/logic/util/Version.java index 336f68e05d0..f94472fc17c 100644 --- a/src/main/java/org/jabref/logic/util/Version.java +++ b/src/main/java/org/jabref/logic/util/Version.java @@ -23,7 +23,6 @@ public class Version { public static final String JABREF_DOWNLOAD_URL = "https://downloads.jabref.org"; - private static final Logger LOGGER = LoggerFactory.getLogger(Version.class); private static final Version UNKNOWN_VERSION = new Version(); @@ -46,6 +45,14 @@ public class Version { private Version() { } + /** + * Tinylog does not allow for altering existing loging configuraitons after the logger was initialized . + * Lazy initialization to enable tinylog writing to a file (and also still enabling loggin in this class) + */ + private static Logger getLogger() { + return LoggerFactory.getLogger(Version.class); + } + /** * @param version must be in form of following pattern: {@code (\d+)(\.(\d+))?(\.(\d+))?(-alpha|-beta)?(-?dev)?} (e.g., 3.3; 3.4-dev) * @return the parsed version or {@link Version#UNKNOWN_VERSION} if an error occurred @@ -82,14 +89,14 @@ public static Version parse(String version) { parsedVersion.isDevelopmentVersion = matcher.group("dev") != null; } catch (NumberFormatException e) { - LOGGER.warn("Invalid version string used: " + version, e); + getLogger().warn("Invalid version string used: {}", version, e); return UNKNOWN_VERSION; } catch (IllegalArgumentException e) { - LOGGER.warn("Invalid version pattern is used", e); + getLogger().warn("Invalid version pattern is used", e); return UNKNOWN_VERSION; } } else { - LOGGER.warn("Version could not be recognized by the pattern"); + getLogger().warn("Version could not be recognized by the pattern"); return UNKNOWN_VERSION; } return parsedVersion; @@ -288,7 +295,7 @@ public enum DevelopmentStage { public static DevelopmentStage parse(String stage) { if (stage == null) { - LOGGER.warn("The stage cannot be null"); + getLogger().warn("The stage cannot be null"); return UNKNOWN; } else if (stage.equals(STABLE.stage)) { return STABLE; @@ -297,7 +304,7 @@ public static DevelopmentStage parse(String stage) { } else if (stage.equals(BETA.stage)) { return BETA; } - LOGGER.warn("Unknown development stage: {}", stage); + getLogger().warn("Unknown development stage: {}", stage); return UNKNOWN; } From 3a296d686ff1597343151729435011ced0b360c3 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Sun, 15 May 2022 15:03:14 +0200 Subject: [PATCH 3/5] Update tinylog.properties --- src/main/resources/tinylog.properties | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/resources/tinylog.properties b/src/main/resources/tinylog.properties index 6878129ac7b..60be0c52049 100644 --- a/src/main/resources/tinylog.properties +++ b/src/main/resources/tinylog.properties @@ -3,4 +3,5 @@ writer = gui writer1 = console writer2 = application insights +# More shrunk exception logs. See https://tinylog.org/v2/configuration/#strip-stack-trace-elements for details exception = strip: jdk.internal From de1e6d781e63dcd551e204eea76a8ffb2db734c3 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Sun, 15 May 2022 15:06:43 +0200 Subject: [PATCH 4/5] Switch to "Shared File Writer" --- src/main/java/org/jabref/gui/JabRefMain.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/jabref/gui/JabRefMain.java b/src/main/java/org/jabref/gui/JabRefMain.java index 8bebe52c4ba..8dfe66dfd22 100644 --- a/src/main/java/org/jabref/gui/JabRefMain.java +++ b/src/main/java/org/jabref/gui/JabRefMain.java @@ -74,11 +74,12 @@ private static void addLogToDisk() { LOGGER.error("Could not create log directory {}", directory, e); return; } + // The "Shared File Writer" is explained at https://tinylog.org/v2/configuration/#shared-file-writer Map configuration = Map.of( - "writerFile", "rolling file", + "writerFile", "shared file", "writerFile.level", "info", - "writerFile.file", directory.resolve("log_{count}.txt").toString(), - "writerFile.latest", directory.resolve("latest.txt").toString()); + "writerFile.file", directory.resolve("log.txt").toString(), + "writerFile.charset", "UTF-8"); Configuration.replace(configuration); initializeLogger(); } From 1545423a2ab44648911fb570c4bb4fa154d43a2a Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Sun, 15 May 2022 15:07:15 +0200 Subject: [PATCH 5/5] Improve writer names --- src/main/resources/tinylog.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/tinylog.properties b/src/main/resources/tinylog.properties index 60be0c52049..26696d06933 100644 --- a/src/main/resources/tinylog.properties +++ b/src/main/resources/tinylog.properties @@ -1,7 +1,7 @@ level = info writer = gui -writer1 = console -writer2 = application insights +writerConsole = console +writerAzure = application insights # More shrunk exception logs. See https://tinylog.org/v2/configuration/#strip-stack-trace-elements for details exception = strip: jdk.internal