Skip to content

Commit

Permalink
Added option to turn off logger
Browse files Browse the repository at this point in the history
Signed-off-by: avifenesh <aviarchi1994@gmail.com>
  • Loading branch information
avifenesh committed Sep 29, 2024
1 parent db28da8 commit 6f8c2c3
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 20 deletions.
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,
}

/*
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
21 changes: 11 additions & 10 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),
DEFAULT(-1),
ERROR(0),
WARN(1),
INFO(2),
DEBUG(3),
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) {
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 @@ -235,9 +233,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.
* 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 All @@ -250,6 +249,7 @@ public static void setLoggerConfig(@NonNull Level level) {
* 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.
*/
Expand All @@ -260,6 +260,7 @@ public static void setLoggerConfig(String 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);
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
1 change: 1 addition & 0 deletions utils/cluster_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"warning": logging.WARNING,
"info": logging.INFO,
"debug": logging.DEBUG,
"off": logging.OFF,
}

GLIDE_HOME_DIR = os.getenv("GLIDE_HOME_DIR") or f"{__file__}/../.."
Expand Down

0 comments on commit 6f8c2c3

Please sign in to comment.