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

Added option to turn off logger #2366

Merged
merged 2 commits into from
Sep 30, 2024
Merged
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
3 changes: 2 additions & 1 deletion csharp/lib/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public enum Level
Warn = 1,
Info = 2,
Debug = 3,
Trace = 4
Trace = 4,
Off = 5,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think it's more common to use "Disabled"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the built-in naming of the rust logging library we use, I want to keep it consistent

}

/*
Expand Down
3 changes: 3 additions & 0 deletions csharp/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum Level {
Info = 2,
Debug = 3,
Trace = 4,
Off = 5,
}

pub struct Client {
Expand Down Expand Up @@ -146,6 +147,7 @@ impl From<logger_core::Level> for Level {
logger_core::Level::Info => Level::Info,
logger_core::Level::Debug => Level::Debug,
logger_core::Level::Trace => Level::Trace,
logger_core::Level::Off => Level::Off,
}
}
}
Expand All @@ -158,6 +160,7 @@ impl From<Level> for logger_core::Level {
Level::Info => logger_core::Level::Info,
Level::Debug => logger_core::Level::Debug,
Level::Trace => logger_core::Level::Trace,
Level::Off => logger_core::Level::Off,
}
}
}
Expand Down
22 changes: 10 additions & 12 deletions java/client/src/main/java/glide/api/logging/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
public final class Logger {
@Getter
public enum Level {
DISABLED(-2),
Copy link
Collaborator

@barshaul barshaul Sep 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why changing it and not using the existing code? setting disabled with -2 allows you to use if (!(level.getLevel() <= loggerLevel.getLevel())) { in the log function without checking specifically if logger.Off

Copy link
Collaborator Author

@avifenesh avifenesh Sep 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the log, it was already existing the specific check.
In the other, I don't want to check, and I want it to be consistent over all clients. “Special” implantation for one language are not a thing we do. The disabled in java only was supposed to be blocked when it was created so it is implemented for all clients, as I do now, even if the request to fix is for ts only.
Adding a negative number will bring major changes to all other clients, and I think consistency is better than one less if.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And about naming, the tracing crate we use, use off so I want to be consistent.

DEFAULT(-1),
ERROR(0),
WARN(1),
INFO(2),
DEBUG(3),
TRACE(4);
TRACE(4),
OFF(5);

private final int level;

Expand All @@ -54,6 +54,8 @@ public static Level fromInt(int i) {
return DEBUG;
case 4:
return TRACE;
case 5:
return OFF;
default:
return DEFAULT;
}
Expand All @@ -63,10 +65,6 @@ public static Level fromInt(int i) {
@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));
}

Expand All @@ -78,7 +76,7 @@ private static void initLogger(@NonNull Level level, String fileName) {
* 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]
* [DEFAULT, ERROR, WARN, INFO, DEBUG, TRACE, OFF]
* </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
Expand Down Expand Up @@ -140,7 +138,7 @@ public static void log(
initLogger(Level.DEFAULT, null);
}

if (level == Level.DISABLED) {
if (level == Level.OFF) {
Comment on lines -143 to +141
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see my comment above

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As said, it was already there as you see, but even if not, answered above.

return;
}

Expand All @@ -163,7 +161,7 @@ public static void log(
initLogger(Level.DEFAULT, null);
}

if (level == Level.DISABLED) {
if (level == Level.OFF) {
return;
}

Expand Down Expand Up @@ -222,7 +220,7 @@ private static String prettyPrintException(@NonNull Throwable throwable) {
* 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]
* [DEFAULT, ERROR, WARN, INFO, DEBUG, TRACE, OFF]
* </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
Expand All @@ -234,10 +232,10 @@ public static void setLoggerConfig(@NonNull Level level, String fileName) {

/**
* Creates a new logger instance and configure it with the provided log level. The logs will be
* written to stdout.
* written to stdout. To turn off the logger, use <code>setLoggerConfig(Level.OFF)</code>.
*
* @param level Set the logger level to one of <code>
* [DISABLED, DEFAULT, ERROR, WARN, INFO, DEBUG, TRACE]
* [DEFAULT, ERROR, WARN, INFO, DEBUG, TRACE, OFF]
* </code>. If log level isn't provided, the logger will be configured with default
* configuration decided by Glide core.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public class ConnectionWithGlideMockTests extends RustCoreLibMockTestBase {
@BeforeEach
@SneakyThrows
public void createTestClient() {
// TODO: Add DISABLED level to logger-core
Logger.setLoggerConfig(Logger.Level.DISABLED);
Logger.setLoggerConfig(Logger.Level.OFF);
channelHandler =
new ChannelHandler(
new CallbackDispatcher(null),
Expand Down
4 changes: 2 additions & 2 deletions java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ pub extern "system" fn Java_glide_ffi_resolvers_ScriptResolver_dropScript<'local
.unwrap_or(())
}

// TODO: Add DISABLED level here once it is added to logger-core
impl From<logger_core::Level> for Level {
fn from(level: logger_core::Level) -> Self {
match level {
Expand All @@ -350,20 +349,21 @@ impl From<logger_core::Level> for Level {
logger_core::Level::Info => Level(2),
logger_core::Level::Debug => Level(3),
logger_core::Level::Trace => Level(4),
logger_core::Level::Off => Level(5),
}
}
}

impl TryFrom<Level> for logger_core::Level {
type Error = FFIError;
fn try_from(level: Level) -> Result<Self, <logger_core::Level as TryFrom<Level>>::Error> {
// TODO: Add DISABLED level here once it is added to logger-core
match level.0 {
0 => Ok(logger_core::Level::Error),
1 => Ok(logger_core::Level::Warn),
2 => Ok(logger_core::Level::Info),
3 => Ok(logger_core::Level::Debug),
4 => Ok(logger_core::Level::Trace),
5 => Ok(logger_core::Level::Off),
_ => Err(FFIError::Logger(format!(
"Invalid log level: {:?}",
level.0
Expand Down
3 changes: 3 additions & 0 deletions logger_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub enum Level {
Info = 2,
Debug = 3,
Trace = 4,
Off = 5,
}
impl Level {
fn to_filter(&self) -> filter::LevelFilter {
Expand All @@ -64,6 +65,7 @@ impl Level {
Level::Info => LevelFilter::INFO,
Level::Warn => LevelFilter::WARN,
Level::Error => LevelFilter::ERROR,
Level::Off => LevelFilter::OFF,
}
}
}
Expand Down Expand Up @@ -187,5 +189,6 @@ pub fn log<Message: AsRef<str>, Identifier: AsRef<str>>(
Level::Info => log_info(log_identifier, message),
Level::Warn => log_warn(log_identifier, message),
Level::Error => log_error(log_identifier, message),
Level::Off => (),
}
}
3 changes: 3 additions & 0 deletions node/rust-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub enum Level {
Info = 2,
Trace = 4,
Warn = 1,
Off = 5,
}

#[napi]
Expand Down Expand Up @@ -136,6 +137,7 @@ impl From<logger_core::Level> for Level {
logger_core::Level::Info => Level::Info,
logger_core::Level::Debug => Level::Debug,
logger_core::Level::Trace => Level::Trace,
logger_core::Level::Off => Level::Off,
}
}
}
Expand All @@ -148,6 +150,7 @@ impl From<Level> for logger_core::Level {
Level::Info => logger_core::Level::Info,
Level::Debug => logger_core::Level::Debug,
Level::Trace => logger_core::Level::Trace,
Level::Off => logger_core::Level::Off,
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion node/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ const LEVEL = new Map<LevelOptions | undefined, Level | undefined>([
["info", Level.Info],
["debug", Level.Debug],
["trace", Level.Trace],
["off", Level.Off],
[undefined, undefined],
]);
export type LevelOptions = "error" | "warn" | "info" | "debug" | "trace";
export type LevelOptions =
| "error"
| "warn"
| "info"
| "debug"
| "trace"
| "off";

/*
* A singleton class that allows logging which is consistent with logs from the internal rust core.
Expand Down Expand Up @@ -55,6 +62,7 @@ export class Logger {
* 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 fileName argument, will write the logs to files postfixed with fileName. If fileName isn't provided, the logs will be written to the console.
* To turn off the logger, provide the level "off".
*/
public static init(level?: LevelOptions, fileName?: string) {
if (!this._instance) {
Expand All @@ -66,6 +74,7 @@ export class Logger {
* configure the logger.
* the level argument is the level of the logs you want the system to provide (error logs, warn logs, etc.)
* the filename argument is optional - if provided the target of the logs will be the file mentioned, else will be the console
* To turn off the logger, provide the level "off".
*/
public static setLoggerConfig(level: LevelOptions, fileName?: string) {
this._instance = new this(level, fileName);
Expand Down
1 change: 1 addition & 0 deletions python/python/glide/glide.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Level(Enum):
Info = 2
Debug = 3
Trace = 4
Off = 5

def is_lower(self, level: Level) -> bool: ...

Expand Down
11 changes: 7 additions & 4 deletions python/python/glide/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Level(Enum):
INFO = internalLevel.Info
DEBUG = internalLevel.Debug
TRACE = internalLevel.Trace
OFF = internalLevel.Off


class Logger:
Expand Down Expand Up @@ -44,10 +45,11 @@ def init(cls, level: Optional[Level] = None, file_name: Optional[str] = None):
If given a fileName argument, will write the logs to files postfixed with fileName. If fileName isn't provided,
the logs will be written to the console.
Args:
level (Optional[Level]): Set the logger level to one of [ERROR, WARN, INFO, DEBUG, TRACE].
level (Optional[Level]): Set the logger level to one of [ERROR, WARN, INFO, DEBUG, TRACE, OFF].
If log level isn't provided, the logger will be configured with default configuration decided by the Rust core.
file_name (Optional[str]): If providedv the target of the logs will be the file mentioned.
file_name (Optional[str]): If provided the target of the logs will be the file mentioned.
Otherwise, logs will be printed to the console.
To turn off logging completely, set the level to Level.OFF.
"""
if cls._instance is None:
cls._instance = cls(level, file_name)
Expand All @@ -74,9 +76,10 @@ def set_logger_config(
"""Creates a new logger instance and configure it with the provided log level and file name.

Args:
level (Optional[Level]): Set the logger level to one of [ERROR, WARN, INFO, DEBUG, TRACE].
level (Optional[Level]): Set the logger level to one of [ERROR, WARN, INFO, DEBUG, TRACE, OFF].
If log level isn't provided, the logger will be configured with default configuration decided by the Rust core.
file_name (Optional[str]): If providedv the target of the logs will be the file mentioned.
file_name (Optional[str]): If provided the target of the logs will be the file mentioned.
Otherwise, logs will be printed to the console.
To turn off logging completely, set the level to OFF.
"""
Logger._instance = Logger(level, file_name)
3 changes: 3 additions & 0 deletions python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub enum Level {
Info = 2,
Debug = 3,
Trace = 4,
Off = 5,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -247,6 +248,7 @@ impl From<logger_core::Level> for Level {
logger_core::Level::Info => Level::Info,
logger_core::Level::Debug => Level::Debug,
logger_core::Level::Trace => Level::Trace,
logger_core::Level::Off => Level::Off,
}
}
}
Expand All @@ -259,6 +261,7 @@ impl From<Level> for logger_core::Level {
Level::Info => logger_core::Level::Info,
Level::Debug => logger_core::Level::Debug,
Level::Trace => logger_core::Level::Trace,
Level::Off => logger_core::Level::Off,
}
}
}
Expand Down
Loading