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

Java: Add Logger #1422

Merged
merged 38 commits into from
Jul 2, 2024
Merged
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
fec36b1
Implement Logger for Java client
jonathanl-bq May 7, 2024
24af480
Run spotless
jonathanl-bq May 7, 2024
d3b2f81
Annotate NonNull for Level in public API
jonathanl-bq May 7, 2024
e21f773
Fix build error
jonathanl-bq May 7, 2024
a45ab3a
Merge branch 'main' into java/dev_lotjonat_logging
jonathanl-bq May 7, 2024
604066e
Fix clippy lint
jonathanl-bq May 7, 2024
18509b3
Update logger
jonathanl-bq May 13, 2024
a291ad1
Fix FFI function names.
Yury-Fridlyand May 13, 2024
73fc219
Remove %n from logs
jonathanl-bq May 13, 2024
c998a2c
Fix Logger test
jonathanl-bq May 13, 2024
2c3a1e4
Address Yury's PR comments
jonathanl-bq May 16, 2024
cba7ad2
Update java/src/lib.rs
jonathanl-bq May 16, 2024
a7062cd
Apply suggestions from code review
jonathanl-bq May 16, 2024
3a13ee2
Update docs and import Logger.Level
jonathanl-bq May 16, 2024
6bf8fed
Split out native function definitions to a new resolver
jonathanl-bq May 17, 2024
01e6b09
Remove static loader
jonathanl-bq May 17, 2024
7035243
Run Spotless
jonathanl-bq May 17, 2024
fcb4026
Change references to Rust core to Glide core
jonathanl-bq May 17, 2024
5094a4e
Spotless again
jonathanl-bq May 17, 2024
4b4bb44
Make minor documentation fixes
jonathanl-bq May 17, 2024
405f9a3
Merge pull request #265 from Bit-Quill/java/dev_lotjonat_logging
jonathanl-bq May 17, 2024
19b96cf
Merge branch 'main' into java/integ_lotjonat_logging
jonathanl-bq May 17, 2024
1eccd3c
Fix failing tests
jonathanl-bq May 17, 2024
0cfd92b
Run spotless
jonathanl-bq May 17, 2024
8c42435
Address PR comments
jonathanl-bq Jun 12, 2024
6c9b7ae
Apply Spotless
jonathanl-bq Jun 12, 2024
23c9957
Minor documentation fixes
jonathanl-bq Jun 12, 2024
b851598
Don't log when DISABLED log level is passed to log method
jonathanl-bq Jun 28, 2024
c9533fa
Resolve merge conflict and add panic handling for logger FFI code
jonathanl-bq Jul 1, 2024
593702e
Fix build errors and handle errors in FFI layer
jonathanl-bq Jul 1, 2024
9e75012
Make logger messages lazily evaluated
jonathanl-bq Jul 1, 2024
cc978ab
Resolve merge conflict
jonathanl-bq Jul 1, 2024
16aaeb0
Run Spotless
jonathanl-bq Jul 1, 2024
fea98cd
Add overload for log method
jonathanl-bq Jul 1, 2024
ed7fec6
Address Logger TODOs in MessageHandler
jonathanl-bq Jul 1, 2024
1ce4d60
Update java/client/src/main/java/glide/connectors/handlers/MessageHan…
jonathanl-bq Jul 1, 2024
fb3b75b
Use suppliers for logging in MessageHandler
jonathanl-bq Jul 1, 2024
d48f4fb
Address PR comments
jonathanl-bq Jul 2, 2024
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
Prev Previous commit
Next Next commit
Address Yury's PR comments
jonathanl-bq committed May 16, 2024
commit 2c3a1e4de65d2815f4d9eed84b6139caedf44261
8 changes: 5 additions & 3 deletions java/client/src/main/java/glide/api/logging/Logger.java
Original file line number Diff line number Diff line change
@@ -6,9 +6,11 @@

/**
* A singleton class that allows logging which is consistent with logs from the internal rust core.
* The logger can be set up in 2 ways - <br>
* 1. By calling <code>Logger.init</code>, which configures the logger only if it wasn't previously configured.<br>
* 2. By calling <code>Logger.setLoggerConfig</code>, which replaces the existing configuration, and means that new logs will not be
* The logger can be set up in 2 ways -
* <ol>
* <li>By calling <code>Logger.init</code>, which configures the logger only if it wasn't previously configured.</li>
* <li>By calling <code>Logger.setLoggerConfig</code>, which replaces the existing configuration, and means that new logs will not be</li>
* </ol>
* saved with the logs that were sent before the call.<br><br>
* If <code>setLoggerConfig</code> wasn't called, the first log attempt will initialize a new logger with default configuration decided
* by the Rust core.
Original file line number Diff line number Diff line change
@@ -7,12 +7,10 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;

import connection_request.ConnectionRequestOuterClass.ConnectionRequest;
import connection_request.ConnectionRequestOuterClass.NodeAddress;
import glide.api.RedisClient;
import glide.api.logging.Logger;
import glide.api.models.exceptions.ClosingException;
import glide.connectors.handlers.CallbackDispatcher;
import glide.connectors.handlers.ChannelHandler;
@@ -29,8 +27,6 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import redis_request.RedisRequestOuterClass.RedisRequest;
import response.ResponseOuterClass.Response;

44 changes: 44 additions & 0 deletions java/integTest/src/test/java/glide/LoggerTests.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package glide;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import glide.api.logging.Logger;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.Scanner;

public class LoggerTests {

private final Logger.Level DEFAULT_TEST_LOG_LEVEL = Logger.Level.WARN;
@@ -26,4 +33,41 @@ public void set_logger_config() {
Logger.setLoggerConfig(DEFAULT_TEST_LOG_LEVEL);
assertEquals(DEFAULT_TEST_LOG_LEVEL, Logger.getLoggerLevel());
}

@SneakyThrows
@Test
public void log_to_file() {
String infoIdentifier = "Info";
String infoMessage = "foo";
String warnIdentifier = "Warn";
String warnMessage = "woof";
String errorIdentifier = "Error";
String errorMessage = "meow";
String debugIdentifier = "Debug";
String debugMessage = "chirp";
String traceIdentifier = "Trace";
String traceMessage = "squawk";

Logger.init(Logger.Level.INFO, "log.txt");
Logger.log(Logger.Level.INFO, infoIdentifier, infoMessage);
Logger.log(Logger.Level.WARN, warnIdentifier, warnMessage);
Logger.log(Logger.Level.ERROR, errorIdentifier, errorMessage);
Logger.log(Logger.Level.DEBUG, debugIdentifier, debugMessage);
Logger.log(Logger.Level.TRACE, traceIdentifier, traceMessage);

File logFolder = new File("glide-logs");
File[] logFiles = logFolder.listFiles((dir, name) -> name.startsWith("log.txt."));
assertNotNull(logFiles);
File logFile = logFiles[0];
logFile.deleteOnExit();
Scanner reader = new Scanner(logFile);
String infoLine = reader.nextLine();
assertTrue(infoLine.contains(infoIdentifier + " - " + infoMessage));
String warnLine = reader.nextLine();
assertTrue(warnLine.contains(warnIdentifier + " - " + warnMessage));
String errorLine = reader.nextLine();
assertTrue(errorLine.contains(errorIdentifier + " - " + errorMessage));
assertFalse(reader.hasNextLine());
reader.close();
}
}