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 Debug to BackupManager #9680

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve

- 'Get full text' now also checks the file url. [#568](https://github.com/koppor/jabref/issues/568)
- We refined the 'main directory not found' error message. [#9625](https://github.com/JabRef/jabref/pull/9625)
- `log.txt` now contains debug messages. Debugging needs to be enabled explicitly. [#9678](https://github.com/JabRef/jabref/pull/9678)
- `log.txt` does not contain entries for non-found files during PDF indexing. [#9678](https://github.com/JabRef/jabref/pull/9678)
- We modified the `Add Group` dialog to use the most recently selected group hierarchical context [#9141](https://github.com/JabRef/jabref/issues/9141)


Expand Down
50 changes: 37 additions & 13 deletions docs/code-howtos/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,44 @@ parent: Code Howtos

JabRef uses the logging facade [SLF4j](https://www.slf4j.org). All log messages are passed internally to [tinylog](https://tinylog.org/v2/) which handles any filtering, formatting and writing of log messages.

* Obtaining a logger for a class:
Obtaining a logger for a class:

```java
private static final Logger LOGGER = LoggerFactory.getLogger(<ClassName>.class);
```
```java
private static final Logger LOGGER = LoggerFactory.getLogger(<ClassName>.class);
```

* If the logging event is caused by an exception, please add the exception to the log message as:
Please always use `LOGGER.debug` for debugging.

```java
catch (SomeException e) {
LOGGER.warn("Warning text.", e);
...
}
```
Example:

```java
String example = "example";
LOGGER.debug("Some state {}", example);
```

Enable logging in `tinylog.properties`:

```properties
[email protected] = debug
```

If the logging event is caused by an exception, please add the exception to the log message as:

```java
catch (SomeException e) {
LOGGER.warn("Warning text.", e);
...
}
```

When running tests, `tinylog-test.properties` is used.
It is located under `src/test/resources`. As default, only `info` is logged.
When developing, it makes sense to use `debug` as log level.
One can change the log level per class using the pattern `level@class=debug` is set to `debug`.
In the `.properties` file, this is done for `org.jabref.model.entry.BibEntry`.

## Further reading

SLF4J also support parameterized logging, e.g. if you want to print out multiple arguments in a log statement use a pair of curly braces (`{}`).
Head to <https://www.slf4j.org/faq.html#logging_performance> for examples.

* SLF4J also support parameterized logging, e.g. if you want to print out multiple arguments in a log statement use a pair of curly braces. [Examples](https://www.slf4j.org/faq.html#logging\_performance)
* When running tests, `tinylog-test.properties` is used. It is located under `src/test/resources`. As default, only `info` is logged. When developing, it makes sense to use `debug` as log level. One can change the log level per class using the pattern `level@class=debug` is set to `debug`. In the `.properties` file, this is done for `org.jabref.model.entry.BibEntry`.
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/cli/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private static void addLogToDisk() {
// https://tinylog.org/v2/configuration/#shared-file-writer
Map<String, String> configuration = Map.of(
"writerFile", "shared file",
"writerFile.level", "info",
"writerFile.level", "debug",
"writerFile.file", directory.resolve("log.txt").toString(),
"writerFile.charset", "UTF-8");

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/jabref/gui/collab/entryadd/EntryAdd.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

import com.google.common.base.MoreObjects;

public final class EntryAdd extends DatabaseChange {
private final BibEntry addedEntry;

Expand All @@ -28,4 +30,11 @@ public void applyChange(NamedCompound undoEdit) {
public BibEntry getAddedEntry() {
return addedEntry;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("addedEntry", addedEntry)
.toString();
}
}
10 changes: 10 additions & 0 deletions src/main/java/org/jabref/gui/collab/entrychange/EntryChange.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

import com.google.common.base.MoreObjects;

public final class EntryChange extends DatabaseChange {
private final BibEntry oldEntry;
private final BibEntry newEntry;
Expand Down Expand Up @@ -46,4 +48,12 @@ public void applyChange(NamedCompound undoEdit) {

undoEdit.addEdit(changeEntryEdit);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("oldEntry", oldEntry)
.add("newEntry", newEntry)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

import com.google.common.base.MoreObjects;

public final class EntryDelete extends DatabaseChange {
private final BibEntry deletedEntry;

Expand All @@ -28,4 +30,11 @@ public void applyChange(NamedCompound undoEdit) {
public BibEntry getDeletedEntry() {
return deletedEntry;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("deletedEntry", deletedEntry)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.groups.GroupTreeNode;

import com.google.common.base.MoreObjects;

public final class GroupChange extends DatabaseChange {
private final GroupDiff groupDiff;

Expand Down Expand Up @@ -53,4 +55,11 @@ public void applyChange(NamedCompound undoEdit) {
public GroupDiff getGroupDiff() {
return groupDiff;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("groupDiff", groupDiff)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;

import com.google.common.base.MoreObjects;

public final class MetadataChange extends DatabaseChange {
private final MetaDataDiff metaDataDiff;

Expand All @@ -29,4 +31,11 @@ public void applyChange(NamedCompound undoEdit) {
public MetaDataDiff getMetaDataDiff() {
return metaDataDiff;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("metaDataDiff", metaDataDiff)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;

import com.google.common.base.MoreObjects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -32,4 +33,11 @@ public void applyChange(NamedCompound undoEdit) {
public PreambleDiff getPreambleDiff() {
return preambleDiff;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("preambleDiff", preambleDiff)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.jabref.model.database.KeyCollisionException;
import org.jabref.model.entry.BibtexString;

import com.google.common.base.MoreObjects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -36,4 +37,11 @@ public void applyChange(NamedCompound undoEdit) {
public BibtexString getAddedString() {
return addedString;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("addedString", addedString)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibtexString;

import com.google.common.base.MoreObjects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -40,4 +41,12 @@ public BibtexString getOldString() {
public BibtexString getNewString() {
return newString;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("oldString", oldString)
.add("newString", newString)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibtexString;

import com.google.common.base.MoreObjects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -35,4 +36,11 @@ public void applyChange(NamedCompound undoEdit) {
public BibtexString getDeletedString() {
return deletedString;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("deletedString", deletedString)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibtexString;

import com.google.common.base.MoreObjects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -45,4 +46,12 @@ public BibtexString getOldString() {
public BibtexString getNewString() {
return newString;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("oldString", oldString)
.add("newString", newString)
.toString();
}
}
1 change: 1 addition & 0 deletions src/main/java/org/jabref/gui/dialogs/BackupUIManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private static Optional<ParserResult> showReviewBackupDialog(DialogService dialo

return DefaultTaskExecutor.runInJavaFXThread(() -> {
List<DatabaseChange> changes = DatabaseChangeList.compareAndGetChanges(originalDatabase, backupDatabase, changeResolverFactory);
LOGGER.debug("changes: {}", changes);
DatabaseChangesResolverDialog reviewBackupDialog = new DatabaseChangesResolverDialog(
changes,
originalDatabase, "Review Backup"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,26 @@ public static void shutdown(BibDatabaseContext bibDatabaseContext) {
* In the case of an exception <code>true</code> is returned to ensure that the user checks the output.
*/
public static boolean backupFileDiffers(Path originalPath) {
LOGGER.debug("originalPath path: {}", originalPath);
Path discardedFile = determineDiscardedFile(originalPath);
if (Files.exists(discardedFile)) {
LOGGER.debug("discardedFile {} exists", discardedFile);
try {
Files.delete(discardedFile);
} catch (IOException e) {
LOGGER.error("Could not remove discarded file {}", discardedFile, e);
LOGGER.debug("backupFileDiffers for {}: true", originalPath);
return true;
}
LOGGER.debug("backupFileDiffers for {}: false", originalPath);
return false;
}
return getLatestBackupPath(originalPath).map(latestBackupPath -> {
LOGGER.debug("discardedFile {} does not exist", discardedFile);
Boolean result = getLatestBackupPath(originalPath).map(latestBackupPath -> {
LOGGER.debug("Latest backup path: {}", latestBackupPath);
FileTime latestBackupFileLastModifiedTime;
try {
latestBackupFileLastModifiedTime = Files.getLastModifiedTime(latestBackupPath);
latestBackupFileLastModifiedTime = Files.getLastModifiedTime(latestBackupPath);
} catch (IOException e) {
LOGGER.debug("Could not get timestamp of backup file {}", latestBackupPath, e);
// If we cannot get the timestamp, we do show any warning
Expand All @@ -179,6 +185,8 @@ public static boolean backupFileDiffers(Path originalPath) {
return true;
}
}).orElse(false);
LOGGER.debug("backupFileDiffers for {}: {}", originalPath, result);
return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private void writeToIndex(BibEntry entry, LinkedFile linkedFile) {
}
Optional<Path> resolvedPath = linkedFile.findIn(databaseContext, filePreferences);
if (resolvedPath.isEmpty()) {
LOGGER.warn("Could not find {}", linkedFile.getLink());
LOGGER.debug("Could not find {}", linkedFile.getLink());
return;
}
try {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/jabref/model/database/BibDatabaseContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,17 @@ public static Path getFulltextIndexBasePath() {

public Path getFulltextIndexPath() {
Path appData = getFulltextIndexBasePath();
Path indexPath;

if (getDatabasePath().isPresent()) {
LOGGER.info("Index path for {} is {}", getDatabasePath().get(), appData);
return appData.resolve(String.valueOf(this.getDatabasePath().get().hashCode()));
indexPath = appData.resolve(String.valueOf(this.getDatabasePath().get().hashCode()));
LOGGER.debug("Index path for {} is {}", getDatabasePath().get(), indexPath);
return indexPath;
}

return appData.resolve("unsaved");
indexPath = appData.resolve("unsaved");
LOGGER.debug("Using index for unsaved database: {}", indexPath);
return indexPath;
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/tinylog.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ writerAzure = application insights

# More shrunk exception logs. See https://tinylog.org/v2/configuration/#strip-stack-trace-elements for details
exception = strip: jdk.internal

#[email protected] = debug
[email protected] = debug
[email protected] = debug