-
Notifications
You must be signed in to change notification settings - Fork 59
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
Java: Add Logger #1422
Changes from 33 commits
fec36b1
24af480
d3b2f81
e21f773
a45ab3a
604066e
18509b3
a291ad1
73fc219
c998a2c
2c3a1e4
cba7ad2
a7062cd
3a13ee2
6bf8fed
01e6b09
7035243
fcb4026
5094a4e
4b4bb44
405f9a3
19b96cf
1eccd3c
0cfd92b
8c42435
6c9b7ae
23c9957
b851598
c9533fa
593702e
9e75012
cc978ab
16aaeb0
fea98cd
ed7fec6
1ce4d60
fb3b75b
d48f4fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
/** Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 */ | ||
package glide.api.logging; | ||
|
||
import static glide.ffi.resolvers.LoggerResolver.initInternal; | ||
import static glide.ffi.resolvers.LoggerResolver.logInternal; | ||
|
||
import java.util.function.Supplier; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
|
||
/** | ||
* 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 - | ||
* | ||
* <ol> | ||
* <li>By calling <code>Logger.init</code>, which configures the logger only if it wasn't | ||
* previously configured. | ||
* <li>By calling <code>Logger.setLoggerConfig</code>, which replaces the existing configuration, | ||
* and means that new logs will not be saved with the logs that were sent before the call. | ||
* </ol> | ||
* | ||
* If <code>setLoggerConfig</code> wasn't called, the first log attempt will initialize a new logger | ||
* with default configuration decided by Glide core. | ||
*/ | ||
public final class Logger { | ||
@Getter | ||
public enum Level { | ||
DISABLED(-2), | ||
DEFAULT(-1), | ||
ERROR(0), | ||
WARN(1), | ||
INFO(2), | ||
DEBUG(3), | ||
TRACE(4); | ||
|
||
private final int level; | ||
|
||
Level(int level) { | ||
this.level = level; | ||
} | ||
|
||
public static Level fromInt(int i) { | ||
switch (i) { | ||
case 0: | ||
return ERROR; | ||
case 1: | ||
return WARN; | ||
case 2: | ||
return INFO; | ||
case 3: | ||
return DEBUG; | ||
case 4: | ||
return TRACE; | ||
default: | ||
return DEFAULT; | ||
} | ||
} | ||
} | ||
|
||
@Getter private static Level loggerLevel; | ||
|
||
private static void initLogger(@NonNull Level level, String fileName) { | ||
if (level == Level.DISABLED) { | ||
loggerLevel = level; | ||
return; | ||
} | ||
loggerLevel = Level.fromInt(initInternal(level.getLevel(), fileName)); | ||
} | ||
|
||
/** | ||
* Initialize a logger if it wasn't initialized before - this method is meant to be used when | ||
* there is no intention to replace an existing logger. The logger will filter all logs with a | ||
* level lower than the given level. If given a <code>fileName</code> argument, will write the | ||
* logs to files postfixed with <code>fileName</code>. If <code>fileName</code> isn't provided, | ||
* the logs will be written to the console. | ||
* | ||
* @param level Set the logger level to one of <code> | ||
* [DISABLED, DEFAULT, ERROR, WARN, INFO, DEBUG, TRACE] | ||
* </code>. If log level isn't provided, the logger will be configured with default | ||
* configuration decided by Glide core. | ||
* @param fileName If provided, the target of the logs will be the file mentioned. Otherwise, logs | ||
* will be printed to the console. | ||
*/ | ||
public static void init(@NonNull Level level, String fileName) { | ||
if (loggerLevel == null) { | ||
initLogger(level, fileName); | ||
} | ||
} | ||
|
||
/** | ||
* Initialize a logger if it wasn't initialized before - this method is meant to be used when | ||
* there is no intention to replace an existing logger. The logger will filter all logs with a | ||
* level lower than the default level decided by Glide core. Given a <code>fileName</code> | ||
* argument, will write the logs to files postfixed with <code>fileName</code>. | ||
* | ||
* @param fileName The target of the logs will be the file mentioned. | ||
*/ | ||
public static void init(@NonNull String fileName) { | ||
init(Level.DEFAULT, fileName); | ||
} | ||
|
||
/** | ||
* Initialize a logger if it wasn't initialized before - this method is meant to be used when | ||
* there is no intention to replace an existing logger. The logger will filter all logs with a | ||
* level lower than the default level decided by Glide core. The logs will be written to stdout. | ||
*/ | ||
public static void init() { | ||
init(Level.DEFAULT, null); | ||
} | ||
|
||
/** | ||
* Initialize a logger if it wasn't initialized before - this method is meant to be used when | ||
* there is no intention to replace an existing logger. The logger will filter all logs with a | ||
* level lower than the given level. The logs will be written to stdout. | ||
* | ||
* @param level Set the logger level to one of <code>[DEFAULT, ERROR, WARN, INFO, DEBUG, TRACE] | ||
jonathanl-bq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* </code>. If log level isn't provided, the logger will be configured with default | ||
* configuration decided by Glide core. | ||
*/ | ||
public static void init(@NonNull Level level) { | ||
init(level, null); | ||
} | ||
|
||
/** | ||
* Logs the provided message if the provided log level is lower then the logger level. | ||
* | ||
* @param level The log level of the provided message. | ||
* @param logIdentifier The log identifier should give the log a context. | ||
* @param message The message to log. | ||
*/ | ||
public static void log( | ||
acarbonetto marked this conversation as resolved.
Show resolved
Hide resolved
acarbonetto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@NonNull Level level, @NonNull String logIdentifier, @NonNull Supplier<String> message) { | ||
if (loggerLevel == null) { | ||
initLogger(Level.DEFAULT, null); | ||
} | ||
|
||
if (level == Level.DISABLED) { | ||
return; | ||
} | ||
|
||
if (!(level.getLevel() <= loggerLevel.getLevel())) { | ||
return; | ||
} | ||
logInternal(level.getLevel(), logIdentifier, message.get()); | ||
} | ||
|
||
/** | ||
* Creates a new logger instance and configure it with the provided log level and file name. | ||
* | ||
* @param level Set the logger level to one of <code> | ||
* [DISABLED, DEFAULT, ERROR, WARN, INFO, DEBUG, TRACE] | ||
* </code>. If log level isn't provided, the logger will be configured with default | ||
* configuration decided by Glide core. | ||
* @param fileName If provided, the target of the logs will be the file mentioned. Otherwise, logs | ||
* will be printed to stdout. | ||
*/ | ||
public static void setLoggerConfig(@NonNull Level level, String fileName) { | ||
initLogger(level, fileName); | ||
} | ||
|
||
/** | ||
* Creates a new logger instance and configure it with the provided log level. The logs will be | ||
* written to stdout. | ||
* | ||
* @param level Set the logger level to one of <code> | ||
* [DISABLED, DEFAULT, ERROR, WARN, INFO, DEBUG, TRACE] | ||
* </code>. If log level isn't provided, the logger will be configured with default | ||
* configuration decided by Glide core. | ||
*/ | ||
public static void setLoggerConfig(@NonNull Level level) { | ||
setLoggerConfig(level, null); | ||
} | ||
|
||
/** | ||
* Creates a new logger instance and configure it with the provided file name and default log | ||
* level. The logger will filter all logs with a level lower than the default level decided by the | ||
* Glide core. | ||
* | ||
* @param fileName If provided, the target of the logs will be the file mentioned. Otherwise, logs | ||
* will be printed to stdout. | ||
*/ | ||
public static void setLoggerConfig(String fileName) { | ||
setLoggerConfig(Level.DEFAULT, fileName); | ||
} | ||
|
||
/** | ||
* Creates a new logger instance. The logger will filter all logs with a level lower than the | ||
* default level decided by Glide core. The logs will be written to stdout. | ||
*/ | ||
public static void setLoggerConfig() { | ||
setLoggerConfig(Level.DEFAULT, null); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 */ | ||
package glide.ffi.resolvers; | ||
|
||
public class LoggerResolver { | ||
// TODO: consider lazy loading the glide_rs library | ||
static { | ||
NativeUtils.loadGlideLib(); | ||
} | ||
|
||
public static native int initInternal(int level, String fileName); | ||
|
||
public static native void logInternal(int level, String logIdentifier, String message); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
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; | ||
|
@@ -37,6 +38,7 @@ public class ConnectionWithGlideMockTests extends RustCoreLibMockTestBase { | |
@BeforeEach | ||
@SneakyThrows | ||
public void createTestClient() { | ||
Logger.setLoggerConfig(Logger.Level.DISABLED); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why disabled? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We needed to disable logging to get these tests to work. They're only unit tests and shouldn't be using a real logger anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right - this is why we need a DISABLED level logger. |
||
channelHandler = | ||
new ChannelHandler( | ||
new CallbackDispatcher(null), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,7 +136,6 @@ tasks.withType(Test) { | |
events "started", "skipped", "passed", "failed" | ||
showStandardStreams true | ||
} | ||
jvmArgs "-Djava.library.path=${project.rootDir}/target/release" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is no longer needed now that we use the NativeLibLoader. It would've been better to put this in the PR that had the changes to publish to local Maven repository, but that's already been merged. |
||
afterTest { desc, result -> | ||
logger.quiet "${desc.className}.${desc.name}: ${result.resultType} ${(result.getEndTime() - result.getStartTime())/1000}s" | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we dont have DISABLED option in node and python - can you please open another PR that adds this option to the other wrappers too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've started work on this. It's not ready yet and probably won't be in until post-GA, but there's a draft PR up here: Bit-Quill#399
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DISABLED option was added to Java side for testing, since we don't want to log when we have mocks. There's other ways to do it (mocking static objects is annoying, but possible), but this is the best way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jonathanl-bq adding it to redis-rs isn't urgent, you can open an issue and we'll do it post GA