Skip to content

Commit

Permalink
Add log on disk (#8791)
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor authored May 15, 2022
1 parent a1a614a commit d542651
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 36 additions & 1 deletion src/main/java/org/jabref/gui/JabRefMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,29 +27,63 @@
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
*/
public class JabRefMain extends Application {
private static final Logger LOGGER = LoggerFactory.getLogger(JabRefMain.class);
private static Logger LOGGER;

private static String[] arguments;

public static void main(String[] args) {
addLogToDisk();
arguments = 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.
*/
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) {
initializeLogger();
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<String, String> configuration = Map.of(
"writerFile", "shared file",
"writerFile.level", "info",
"writerFile.file", directory.resolve("log.txt").toString(),
"writerFile.charset", "UTF-8");
Configuration.replace(configuration);
initializeLogger();
}

@Override
public void start(Stage mainStage) {
try {
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/org/jabref/logic/util/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
7 changes: 5 additions & 2 deletions src/main/resources/tinylog.properties
Original file line number Diff line number Diff line change
@@ -1,4 +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

0 comments on commit d542651

Please sign in to comment.