Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add log on disk #8791

Merged
merged 7 commits into from
May 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
36 changes: 35 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,62 @@
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;
}
Map<String, String> 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);
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
2 changes: 2 additions & 0 deletions src/main/resources/tinylog.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ level = info
writer = gui
writer1 = console
writer2 = application insights

exception = strip: jdk.internal